I'm working on a program that records students grades. The user inputs first name, last name, student ID, grade, and assignment name. I'm supposed to have the input from the user separated by being tabbed rather than a space. But when I execute the program the output in the txt file isn't correct.
#include <stdio.h>
int main(void){
FILE *cfPtr; // cfPtr = clients.txt file pointer
// fopen opens file. Exit program if unable to create file
if ((cfPtr = fopen("grades.txt", "w")) == NULL) {
puts("File could not be opened.");
}
else {
puts("Enter the students first & last name, ID, grade, and assignment.");
puts("Enter EOF to end input.");
printf("%s", "? ");
char firstName[30]; // student first name
char lastName[30]; // student last name
unsigned int stuID; // student ID
double grade; // student grade
char assignment[10]; // student assignment name
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
// write first & last name, student ID, grade, and assignment name into file with fprintf
while(!feof(stdin)) {
fprintf(cfPtr, "%s\t%s\t%d\t%.2f\t%s\t\n", firstName, lastName, stuID, grade, assignment);
fflush(cfPtr);
printf("%s", "? ");
scanf("%5s%5s%d%lf%5s", firstName, lastName, &stuID, &grade, assignment);
} // end while
fclose(cfPtr); // fclose closes file
} // end else
} // end main
Heres what happens when I execute it:
Enter the students first & last name, ID, grade, and assignment.
Enter EOF to end input.
? Freddy Krueger 1234 22.33 test1
? ? Jason Vorhees 1235 33.00 test1
? ? The Hulk 1236 2.95 test1
? Bat Man 1237 100.00 test1
? ^Z
[1]+ Stopped ./seqw
grade.txt file output:
Fredd y 0 0.00 ttime
Krueg er 1234 22.33 test1
Jason Vorhe 1234 22.33 test1
es 1235 33 0.00 test1
The Hulk 1236 2.95 test1
Bat Man 1237 100.00 test1
What am I doing wrong here? And where did ttime come from? And it's strange how it's tabbing part of the last name and making it another input.