Hi all,
I'm having trouble with a function that is supposed to update records. I have copied it below:
The trouble I have when I run the program is a little difficult for me to describe, so I copied a snapshot of it below:Code:void updaterecord(FILE *fptr) { int record; int choice; char newname[30]; char *nlptr; int newquantity; double newprice; struct inventory tool = {0, "", 0, 0.0}; printf("Enter record to update(1-100): "); scanf("%d", &record); fseek(fptr, (record - 1) * sizeof(struct inventory), SEEK_SET); fread(&tool, sizeof(struct inventory), 1, fptr); if(tool.recordnum == 0){ printf("Record has no information.\n"); }/*end if*/ else{ printf("%-12d%-20s%-12d%12.2f\n", tool.recordnum, tool.toolname, tool.quantity, tool.cost); printf("\nEnter 1 to update name.\n" "2 to update quantity.\n" "3 to update cost.\n" "^Z to finish update.\nChoice? "); while((choice = getchar()) != EOF){ switch(choice){ case '1': printf("\n\nEnter new name: "); scanf("%s", newname); strcpy(tool.toolname, newname; if ((nlptr = strchr(newname, '\n')) != NULL){ *nlptr = '\0'; } break; case '2': printf("\n\nEnter new quantity: "); scanf("%d", &newquantity); tool.quantity = newquantity; break; case '3': printf("\n\nEnter increase (+) or reduction (-) of cost: "); scanf( "%lf", &newprice); tool.cost += newprice; break; default: printf("Incorrect choice\n"); break; }/*end switch*/ }/*end while*/ printf("%-12d%-20s%-12d%12.2f\n", tool.recordnum, tool.toolname, tool.quantity, tool.cost); fseek(fptr, (record - 1) * sizeof(struct inventory), SEEK_SET); fwrite(&tool, sizeof(struct inventory), 1, fptr); }/*end else*/ }
My first question is, why does the program display "Incorrect choice" when I haven't, and second, how can I copy the new name properly? I've read the FAQ's about copying strings, and as far as I can tell, I have the right code. Thanks for the help.Press 1 to begin a new file, 2 to use existing data: 2
Enter your choice
1 - store a formatted text file of the inventory called
"storeinv.txt" for printing
2 - update a record
3 - add a new record
4 - delete a record
5 - end program
? 2
Enter record to update(1-100): 1
1 Buzz 7 12.99
Enter 1 to update name.
2 to update quantity.
3 to update cost.
^Z to finish update.
Choice? Incorrect choice
1
Enter new name: Buzz Saw
Incorrect choice
Incorrect choice
Incorrect choice
Incorrect choice
Incorrect choice
^Z
1 Buzz 7 12.99
Enter your choice
1 - store a formatted text file of the inventory called
"storeinv.txt" for printing
2 - update a record
3 - add a new record
4 - delete a record
5 - end program
?



LinkBack URL
About LinkBacks


