anonymous
2013-07-03 20:25:15 UTC
The assignment is the following:
Dynamic Memory Allocation
Create a structure to represent a new datatype called StudentInfo. Student info will contain three members, an integer called Id, an instance of a date structure called birthdate [you’ll need to create a date structure for this], and a third integer to store a phone number.
Inside of your program, ask the user how many students they need to enter records for. Once you have completed this, dynamically allocate enough memory to store the student records (basically, create an array of your structure, but use malloc to allocate the memory. Do not use the syntax “int values[5]” to create the array). Use a loop to prompt the user to enter the data for all of the student information.
Finally, create a loop that scans for and then prints out the ID number for the oldest student. You must construct your loop in such a way that it can handle more than three students.
*Hint: before trying to write the code for this, think about the problem. Draw it out. Make it with pseudo code. If you try to write the code to accomplish this without some sort of a plan, it will be significantly more difficult.
This is the first time I have had troubles with C, I feel like I missed a lecture or two, the videos I have are confusing and my books don't go into great detail on this.
Here is my code:
#include
#include
//define structure to store students birth date
struct date
{
int month;
int day;
int year;
};
//define structure to store student info and date structure for birth date
struct studentInfo
{
int iD;
struct date birthDate;
int phone;
};
int main (void)
{
//declare and initialize variables
int recNum = 0; //number of records
struct studentInfo * records = NULL; //struct pointer, array
//request user input and store in recNum for record amount
printf("\nHow many students do you need to enter records for?:");
scanf ("%d",&recNum);
//dynamically allocate memory
records = (struct studentInfo*)malloc((sizeof(struct studentInfo)*recNum));
//loop through records and request/store values from user
int count;
int studentNum=1;
for(count=0;count
printf("Please enter the following for student number %d\n",studentNum);
//request and store student ID
printf("Student ID#:");
scanf ("%d",&records[count].iD);
//request and store student phone number
printf("Student phone# (numbers only, 10 digits):");
scanf ("%d",&records[count].phone);
//error checking, check if phone number is 10 digits
int phoneCount = 0;
int phoneCopy = records[count].phone;
while(phoneCopy != 0)
{
phoneCopy /= 10;
phoneCount++;
}
if (phoneCount != 10)
{
printf("The number you have entered is not 10 digits, please re-enter:");
scanf ("%d",&records[count].phone);
}
//request and store student birthdate
printf("Student birthday (mm/dd/yyyy):");
scanf("%d/%d/%d",&records[count].birthdate.month,&records[count].birthdate.day,&records[count].birthdate.year);
//test stuff
printf("Student number %d has an ID of %d and a phone number of %d\n",studentNum,records[count].iD,records[count].phone);
studentNum++;
}
return 0;
}
I'm getting a error that says struct studentInfo has no member named birthdate. I've tried different variations I have found out on the web and in my books, this is the second major snag I have ran into with the program. I really feel like I missed a lesson or something here, maybe I need to review pointers. Please help.
ps. I really don't now why I use the line below, I just thought I would use it after watching my instructors videos
struct studentInfo * records = NULL;
It doesn't make much sense to me. I'm not quite sure how declaring a pointer of type struct studentInfo magically lets us use records as an array???