Please somebody help me when I use a function to populate my structure i get diffrent values from what i entered I have been stocked here for a while please somebody help me 🙏
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct myEMP
{
char *empName[20];
int *empID;
float *NofHworked;
int *payRate;
}myEMP;
char *Names;
myEMP getInfo(myEMP
employee) {
printf("Name:");
strcpy(employee.empName,fgets(
&Names,20,stdin) );
printf("ID :");
scanf("%d",&employee.empID);
printf("No of hours
worked :");
scanf("%f",&employee.NofHworked);
printf("Pyrate :");
scanf("%d",&employee.payRate);
return employee;
}
int main(){
int N;
myEMP *empINFO;
getInfo(*empINFO);
printf("Name \t Id \t
hours \t payrate \n" );
printf("%s \t %d \t %.2f \t
%d",empINFO-
>empName,empINFO-
>empID,empINFO-
>NofHworked,empINFO-
>payRate);
return 0;
}
I tried runing the code with
name: John Doe Id :01 No of hours worked : 06 Payrate :25
This is what I get
Name: â-#[|ìv Id: -1961038717 Hours: 0.00 Payrate:-1065106551
I dont know what to do since i am new to c somebody please help🙏🙏
Well, there are multiple issues with your code: The first one is that your code is very hard to read because it is not indented properly. I took the liberty to indent it for you:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct myEMP {
char* empName[20];
int* empID;
float* NofHworked;
int* payRate;
} myEMP;
char* Names;
myEMP getInfo(myEMP employee) {
printf("Name:");
strcpy(employee.empName, fgets(&Names, 20, stdin));
printf("ID :");
scanf("%d", &employee.empID);
printf("No of hours worked :");
scanf("%f", &employee.NofHworked);
printf("Pyrate :");
scanf("%d", &employee.payRate);
return employee;
}
int main() {
int N;
myEMP* empINFO;
getInfo(*empINFO);
printf("Name \t Id \t hours \t payrate \n");
printf("%s \t %d \t %.2f \t %d",empINFO->empName, empINFO->empID, empINFO->NofHworked, empINFO->payRate);
return 0;
}
The second thing is: You should decide on the signature of the function getInfo. IMHO, there are two possibilities:
Use void getInfo(myEMP* employee) as signature.
Use myEMP getInfo() as signature.
The first one will modify the employee which is stored at the memory address you provide, whereas the second one will return a new employee.
If you really want to use the function you have now, you have to improve your main function at least with regard to the following two points:
malloc the memory for empINFO.
Use the return value that getInfo provides.
However, I suggest redesigning the getInfo function.
Please note that I have not tested your code, so there is a good chance that there are even more issues.
Related
I just want to quickly note that im a third semester CS student so im fairly new to C and I just really want help on a project im creating for myself.
Okay so basically im trying to have it so that I create a question that the code asks the user such as "Type in your name, age, and house" (im trying to create this harry potter quiz for my gf). So I want to store the user information within an array inside of this structure I created. I want it so that the user puts down their information and it stores it in the structure and then later the user can add another person to compare their results to and itll store this users data as well and whenever you want to print it all itll print it. Im hoping this makes sense so please feel free to ask questions but ill
#include<stdio.h>
#include <stdlib.h>
#include "sortinghatquiz.h"
#define maxNum 100
int main_sorting_quiz()
{
sorting_hat_menu();
sorting_hat_switch();
}
int sorting_hat_menu()
{
char array[]={"."};
for (int i = 0; i < 50; i++)
{
printf("%c\n", array[0]);
}
printf("\n\n\t\t\tWelcome to the Hogwarts Sorting Hat Quiz!!!\n");
printf("\t\t\t________________________________________\n");
printf("\t\t\t1 - Start the quiz!\n\t\t\t2 - Find Existing Quiz!\n\t\t\t3 - Compare results with another user!\n\t\t\t4 - Return to main menu!\n\n");
}
int sorting_hat_switch()
{
int choice_quiz,x;
printf("\n\t\t\tChoose your option: ");
scanf("%d", &choice_quiz);
switch (choice_quiz)
{
case 1:
start_quiz(x);
break;
case 2:
find_quiz();
break;
case 3:
compare_results();
break;
default:
break;
}
}
int start_quiz(quizinfo *i)
{
quizinfo A[maxNum] = {};
for (i = 0; i < 4; i++)
{
start_quiz_menu(i);
return i;
}
printf("\nHeres the first persons data %s, %s, %s\n", A[0].name, A[0].age, A[0].house);
printf("\nHeres the second persons data %s, %s, %s\n", A[1].name, A[1].age, A[1].house);
printf("\nHeres the third persons data %s, %s, %s\n", A[2].name, A[2].age, A[2].house);
printf("\nHeres the fourth persons data %s, %s, %s\n", A[3].name, A[3].age, A[3].house);
//Type in your name
//Type in your age
//Type in your desired house (this wont influence your decision)
//All of this data needs to be stored within structure
}
int start_quiz_menu(quizinfo *x)
{
int i;
quizinfo A[maxNum] = {};
printf("\t\t\tType your name in: ");
scanf("%s", A[i].name);
printf("\n\t\t\tType in your age: ");
scanf("%s", A[i].age);
printf("\n\t\t\tType in your desired house to see if you think you know yourself: ");
scanf("%s", A[i].house);
printf("\n\n\t\t\tHello %s, at the age of %s you have begun your quest to see which hogwarts house you are placed in.\n\t\t\tLets see if %s really is your true house.....\n", A[i].name, A[i].age, A[i].house);
}
int find_quiz()
{
printf("Find Quiz Test 1");
}
int compare_results()
{
printf("Compare results Test 1");
}
Im experimenting with libraries so its all connected and compiled into one main.c file so heres the .h file for this piece of code
#ifndef _SORTINGHATQUIZ_H_
#define _SORTINGHATQUIZ_H_
typedef struct
{
char name[100];
int age[10];
char house[100]
}quizinfo;
int main_sorting_quiz();
int sorting_hat_menu();
int start_quiz(quizinfo *i);
int start_quiz_menu(quizinfo *x);
int find_quiz();
int compare_results();
#endif
Also like I said, im fairly new to this so If theres anything I can optimize to make more efficient then I would love to hear some advice. Thanks!
struct account
{
struct //A structure inside a structure
{
char lastName[10];
char firstName[10];
} names; //structure is named as 'name'
int accountNum;
double balance;
};
int main()
{
struct account record;
int flag = 0;
do
{
nextCustomer(&record);
if ((strcmp(record.names.firstName, "End") == 0) && //only when the first name entered as "End"
(strcmp(record.names.lastName, "Customer") == 0)) //and last name entered as "Customer", the loop stops
flag = 1;
if (flag != 1)
printCustomer(record);
}
while (flag != 1);
}
void nextCustomer(struct account *acct)
{
printf("Enter names (firstName lastName):\n");
//scanf("%s, %s", acct->firstName, acct->lastName); //have no idea why first and last name cant be found although im aware thats its a structure inside a structure
printf("Enter account number:\n");
//scanf("%d", acct->accountNum);
printf("Enter balance:\n");
scanf("%f", acct->balance);
}
void printCustomer(struct account acct)
{
printf("Customer record: \n");
printf("%d", acct.accountNum); //can't seem to retrieve data from the strcture
}
Hi guys, i am new to c, and i managed to hardcode data on structures, and print our their respective value. Currently, i am working on using a pointer to store the respective data, as well as function to print out their data. Can anyone help me on why i cant store and retrieve my data? I dont need the answers, just the logic flow is sufficient.
Followin changes are needed to your nextCustomer function, you are directly accessing firstName and lastName using acct, but they are present inside names
so you have to use acct->names.firstName and acct->names.lastName
void nextCustomer(struct account *acct)
{
printf("Enter names (firstName lastName):\n");
scanf("%s%s", acct->names.firstName, acct->names.lastName); //have no idea why first and last name cant be found although im aware thats its a structure inside a structure
printf("Enter account number:\n");
scanf("%d", &acct->accountNum);
printf("Enter balance:\n");
scanf("%lf", &acct->balance);
}
void printCustomer(struct account acct)
{
printf("Customer record: \n");
printf("F: %s\n", acct.names.firstName);
printf("L: %s\n", acct.names.lastName);
printf("A: %d\n", acct.accountNum); //can't seem to retrieve data from the strcture
printf("B: %lf\n", acct.balance);
}
this for adding new costumer.
typedef struct date{
int d,m,y;
}date;
typedef struct client{
int *id;
char *nom [30];
char *prenom [30];
date date;
char *adresse[30] ;
char *tel [30];
}client;
client cl;
void ajouter (){
int age;
printf("give first name:\n");
scanf("%s",&cl.nom);
printf("give last name:\n");
scanf("%s",&cl.prenom);
printf("give adresse:\n");
scanf("%s",&cl.adresse);
printf("give tel num:\n");
scanf("%s",&cl.tel);
printf("give date of birth :\n");
scanf("%d %d %d",&cl.date.d, &cl.date.m, &cl.date.y);
cl.id=1;
if(2019-cl.date.y<18)
printf("refuse");
else
printf(" succes\n");
printf("your informations:\n");
printf(" first name: %s \n last name: %s \n adresse: %s \n tel num: %s \n date of birth : %d/%d/%d \n ID: 0000000%d",cl.nom,cl.prenom,cl.adresse,cl.tel,cl.date.d,cl.date.m,cl.date.y,cl.id);
cl.id++;
}
the problem is when i add the 1st costumer i dont know how to store the informations
i want to save it without using files.
i am beginner and i know maybe its wrong what am doing
but am trying to do my best to make this code
int main()
{
int a,age;
char t[100];
printf("----------- menu ------------\n");
printf("add customer : 1\n");
printf("remove customer : 2\n");
printf("search for customer : 3\n");
scanf("%d",&a);
switch(a){
case(1):ajouter(a);break;
}
return 0;
}
i didnt finish it yet
Salut,
If I understood well, you wish to store a client's data in an array in RAM.
I believe you are using C, hence vectors are not really an option.
So instead, you have either the option of having an array of fixed size, which could be useful in certain situations but generally not, or having a dynamic array, which can be kind of hard to understand when begginning but you shouldn't have much trouble.
In the case a fixed-size array works for you, here's an example of how you'd do it:
//Declaring the client array
#define nClientsTableau 100
int nClients = 0;
client clients[nClientsTableau];
...
//Adding a new client, keep in mind it's just an example
ajouter();
clients[nClients] = cl;
nClients++;
if(nClients == nClientsTableau) printf("Max amount of clients reached");
Then for the second option, building a dynamic array. Different ways of doing it exist, here is a simple way to perform it for your code:
//Declaring the client array
client* clients = NULL;
client* clients2 = NULL;
int nClients = 0
//Adding a new client
ajouter();
nClients++;
clients2 = (client*)realloc(clients, nClients * sizeof(client));
clients = clients2; //NOTE: You should probably check for clients2 not being NULL first
clients[nClients - 1] = cl;
//Ending the program
free(clients);
I sincerely hope this helps. Apart from that, I'd like to tell you that your code is excellent for a beginner, just try having some naming conventions.
For a project I have to do, I have to list a set of classes, have the user select which class to use, and print out a weekly schedule for them for the semester. (Same program as the first question I asked.) However, I seem to run into a problem when I try to print out a weekly schedule. (The program is pretty lengthy, at least with the experience I have in C.)
struct course
{
int index;
char name[7];
char day[4];
int hours,houre,mins,mine;
char ap[3];
int credit;
};
struct course whcl[]={ {0,"MATH1","MWF",7,8,30,50,"AM",5},
{1,"MATH2","MWF",9,10,00,20,"AM",5},
{2,"CHEM1","MW ",2,6,30,50,"PM",5},
{3,"PHYS4","TTH",4,6,00,45,"PM",4},
{4,"ENGR1","M ",9,10,30,20,"AM",1},
{5,"ENGR2","TTH",10,12,00,15,"PM",3},
{6,"ENGR3","MW ",11,12,00,15,"PM",3}};
int choice[15],i,j,k,num,z,s;
void printout(int z); //(To be put in when I fix the function)
int main(void)
{
char l[8][3]={{"st"},{"nd"},{"rd"},{"th"},{"th"},{"th"},{"th"},{"th"}};
printf(" Fall Schedule\n");
printf("Index Course Day Time Credit\n");
printf("-------------------------------------------\n");
for(i=0;i<7;i++)
{
printf(" %i %s %s %i%i:%i%i-%i%i:%i%i%s %i\n",
whcl[i].index,whcl[i].name,whcl[i].day,
whcl[i].hours/10,whcl[i].hours%10,
whcl[i].mins/10,whcl[i].mins%10,
whcl[i].houre/10,whcl[i].houre%10,
whcl[i].mine/10,whcl[i].mine%10,
whcl[i].ap,whcl[i].credit);
}
printf("How many classes would you like to take?: ");
scanf("%i",&num);
for(i=0;i<num;i++)
{
printf("Select the %i%s class using the index: ",i+1,l[i]);
scanf("%i",&choice[i]);
}
printf("The classes you have selected are:\n");
printf("Index Course Day Time Credit\n");
printf("-------------------------------------------\n");
for(i=0;i<num;i++)
{
s=choice[i];
printf(" %i %s %s %i%i:%i%i-%i%i:%i%i%s %i\n",
whcl[s].index,whcl[s].name,whcl[s].day,
whcl[s].hours/10,whcl[s].hours%10,
whcl[s].mins/10,whcl[s].mins%10,
whcl[s].houre/10,whcl[s].houre%10,
whcl[s].mine/10,whcl[s].mine%10,
whcl[s].ap,whcl[s].credit);
}
printf("Your weekly schedule for Fall is:\n");
printf(" Time Monday Tuesday Wednesday Thursday Friday\n");
printout(z);
return 0;
}
void printout(int z)
{
int start,starti,end,endi,num;
int slot[25][6];
for(i=0;i<24;i++)
for(j=0;j<5;j++)
slot[i][j]=99;
for(i=0;i<num;i++)
{
if ((whcl[choice[i]].day)=="MWF")//I think the problem is here.
{
start=whcl[choice[i]].hours*60+whcl[choice[i]].mins;
end=whcl[choice[i]].houre*60+whcl[choice[i]].mine;
starti=(start-450)/30;
endi=(end-450)/30;
for(j=starti;j<=endi;j++)
slot[j][1]=slot[j][3]=slot[j][6]=whcl[choice[i]].index;
}
}
for(i=0;i<24;i++)
{
printf("%i%i:%i%i-%i%i:%i%i ",
(450+(i-1)*30)/60/10,(450+(i-1)*30)/60%10,
(450+(i-1)*30)%60/10,(450+(i-1)*30)%60%10,
(450+(i-1)*30+30)/60/10,(450+(i-1)*30+30)/60%10,
(450+(i-1)*30+30)%60/10,(450+(i-1)*30+30)%60%10);
for(j=0;j<4;j++)
{
if (slot[i][j]!=99) //Use Note here
printf(" %s ",whcl[choice[i]].name);
else
printf("");
}
printf("\n");
}
return;
}
When I print out the schedule, the only thing that comes up is the time. Everything else is blank. I think it's due to my trying to replace the slot array with something other than 99. If you plan on running this program, please use 2 the amount of classes you want to take, and use 0 and 1 for the index on the class choice. (I don't have any if statements and whatnot to take into account the other classes the user might have chosen.) Here's a photo of what I'm trying to do for my program. http://postimg.org/image/3tlgtwu9h/ I used paint to put in the boxes on the schedule to visually see the different arrays as I was coding.
Note: If you change the if statement to [i][j]==99 You can see the "Class" being printed on the table, however it fills up the entire array slot, which confirms my thought that I messed up on trying to replace data in the array. Also, I filled it up with 99 to make 99 associated with blank spaces.
if ((whcl[choice[i]].day)=="MWF") //I think the problem is here.
Correct, you need to use strcmp to compare strings, not ==
Try:
if (strcmp(whcl[choice[i]].day,"MWF") == 0)
Equality as == will check if pointers are the same so you could have:
char * a = "MTW"; char *b = a and then a == b would be true
For my assignment I am to create a structure that allows the user to enter student info (ID, DOB, & Phone number). I have no problem doing this that is quite simple. Now I need to search through that enter info using the student ID to display that students corresponding DOB and phone number, this is the problem that I am having trouble working with. If you see any other problems with my program please let me know what is wrong and why I should change so I can learn from my mistakes.
Thank you.
#include <stdio.h>
#include <stdlib.h>
struct infoStruct
{
int studentID;
int year;
int month;
int day;
int phone;
int end;
};
int main (void)
{
int students = 0;
int infoArray [students];
struct infoStruct info;
int studentID;
int year;
int month;
int day;
int phone;
int end;
while (info.end != -1) {
students = students + 1;
printf("Enter student information (ID, day, month, year, phone)\n");
printf("Enter -1 following the phone number to end the process to continue enter 0\n");
scanf("%d %d %d %d %d %d", &info.studentID, &info.day, &info.month, &info.year, &info.phone, &info.end);
}
if (info.end = -1){
printf("You entered %d student(s)\n", students);
}
//Student Search
printf("Please enter the student ID of the student your looking for\n.");
scanf("%d", info.studentID);
printf(" DOB: %d %d %d, Phone: %d", info.month, info.day, info.year, info.phone);
}
info.end is not initialized before while (info.end != -1). Initiliaze all your variable (studentID...) and structure.
if (info.end = -1) is an assignment !
Use : if (info.end == -1) I prefer to use if (-1 == info.end) (if you had use : only = instead of == you would have get an error). (Yoda trick ^^)
And you have to use an array of struct in order to save every student (because you are continuously erasing the previous student information).
It's your homework, I won't do the work for you ;)
I'll leave most of the coding to you, as this is homework, but here is what you need to change to get this to work.
First of all, if you want to store multiple students info is going to need to be an array
static int MAX_STUDENTS = 50;
struct infoStruct info[MAX_STUDENTS];
and then you scan each student into a seperate part of the struct
scanf("%d %d %d %d %d %d", &info[students-1].studentID, &info[students-1].day, &info[students-1].month, &info[students-1].year, &info[students-1].phone, &info[students-1].end);
then you need to ensure that the end condition is checked properly (check the latest info[x].end)
It would also be wise to check you still have some room in the array before trying to add more.
with those done you are storing the students correctly.
as for searching, you need to scan the id to search into a seperate int.
then loop through the array (info[x]) and search each element's studentID against the search ID. When you have a match, print it out.
Edit:
its also worth considering storing the phone number as a "string" rather than an int, alot of phone numbers start with "0", but an int would remove the 0.
(so the phone number 012345, would become, 12345)