I have a problem with floating point variables with structures. When I try to enter a floating point number, it seems to put it in the firstName string array. I don't know why, the code and output is posted below. Please help if you can, I would greatly appriciate it.
Output:
Josh Haynes 0.00
3.45 Haire 0.00
Code:
// char firstName[20]
// char lastName[20]
// float gpa
// int ID
void Manual_Input()
{
int count, loop_counter;
printf ("How many students do you want to enter?: ");
scanf ("%d", &count);
student_array = (students *) malloc (count * sizeof(students));
for (loop_counter = 0; loop_counter < count; loop_counter++)
{
printf ("Enter first name: ");
scanf ("%s", student_array->firstName);
printf ("Enter last name: ");
scanf ("%s", student_array->lastName);
printf ("Enter GPA: ");
scanf ("%.2f", student_array->gpa);
student_array->ID = loop_counter;
student_array++;
}
student_array -= count;
printf ("Number of students are %d.\n", count);
for (loop_counter = 0; loop_counter < count; loop_counter++)
{
printf ("%s", student_array->firstName);
printf (" %s ", student_array->lastName);
printf ("%.2f\n", student_array->gpa);
student_array++;
}
free (student_array);
return;
}