I'm writing a program so that a user can create a text file that stores a list of their dream cars, garage inventory, any list of vehicles. I use fgets and sscanf to ask the user for the make, then year, then model, then value, then color. But for some reason it skips the user input stage of getting the model and jumps to asking for the value, but still displays the printf of "Enter car model: " above the printf for entering the value. Below is the excerpt from my code containing my fgets/sscanf functions and below that will be an example of the output so you can see what the issue is if (ha, if) my description wasn't very clear.
if (!listappend){
printf("File nonexistent/inaccessible.");
return 1;
}
memset(colorin, 0, sizeof(colorin));
memset(makein, 0, sizeof(makein));
memset(modelin, 0, sizeof(modelin));
memset(yearin, 0, sizeof(yearin));
memset(valuein, 0, sizeof(valuein));
printf("\nEnter car make: ");
fgets(makein, sizeof(makein), stdin);
sscanf(makein, "%[^\n]",makein);
fprintf(listappend, "\n%s", makein);
printf("\nEnter car manufacture year: ");
fgets(yearin, sizeof(yearin), stdin);
sscanf(yearin, "%d", &yearinf);
printf("\nEnter car model: ");
fgets(modelin, sizeof(modelin), stdin);
sscanf(modelin, "%[^\n]",modelin);
fprintf(listappend, "\n%s", modelin);
printf("\nEnter approximate car value: $");
fgets(valuein, sizeof(valuein),stdin);
sscanf(valuein, "\n%f", &valueinf);
fprintf(listappend, "\n%d %'.2f", yearinf, valueinf);
printf("\nEnter car color: ");
fgets(colorin, sizeof(colorin), stdin);
sscanf(colorin, "%[^\n]",colorin);
fprintf(listappend, "\n%s", colorin);
fclose(listappend);
Sample output:
|-|-|-|-|-|-|-|-|-|-|Car-lection List|-|-|-|-|-|-|-|-|-|-|
----------------------------------------------------------
[A]dd New Car [V]iew List
[M]enu [D]eveloper Info
[C]lear List [Q]uit
Option?
A
Enter car make: Honda
Enter car manufacture year: 2000
Enter car model:
Enter approximate car value: $5000
Enter car color: Blue
Option?
V
Entry Year Color Make Model Color Approx. Value
1| 2000 Blue Honda, approximate value: $5,000.00
As you can see, the user should be able to input the car model, but the program immediately asks for the approximate car value, skipping the chance to input the car model information.
And here is the cat of car_list.txt, the text file I save the strings and values to to be read:
cat car_list.txt
Honda
2000 5,000.00
Blue
I'm new to C programming so if it's a silly mistake I apologize for wasting your time, but it's been driving me crazy. Thank you in advance for your help!
No comments:
Post a Comment