这是我的代码,我的陈述是当 while 循环开始运行时显示选项并在第一次迭代时扫描选择,但在第二次迭代中不会再次分配选择,并且会记住以前的选择。问题是什么 ?(我使用的是VS2012)
while (!done){
int choice;
printf("\n------- STUDENT INFORMATION SYSTEM MAIN MENU --------\n");
printf("1-Load students from the database\n");
printf("2-Print existing students on the screen\n");
printf("3-Add a new student\n");
printf("4-Delete an existing student\n");
printf("5-Find an existing student\n");
printf("6-Quit\n");
printf("====> Choice? ");
scanf("%d", &choice);
switch(choice){
case 1:
LoadStudentsFromDatabase();
printf("Students loaded from database successfully\n");
break;
case 2:
PrintExistingStudentsOnTheScreen();
break;
case 3:
printf("\nFirstName: "); scanf("%s", s.firstName);
printf("LastName: "); scanf("%s", s.lastName);
printf("ID: "); scanf("%d", &s.id);
printf("Gpa: "); scanf("%f", &s.gpa);
printf("Department: "); scanf("%d", &s.department);
AddStudent(&s);
printf("1 student added\n");
break;
case 4:
printf("\nID? "); scanf("%d", &id);
if (DeleteStudent(id)){
printf("Student deleted successfully\n");
} else {
printf("Failed to delete the student. Does not exist?\n");
} /* end-else */
break;
case 5:
printf("\nID? "); scanf("%d", &id);
ps = FindStudent(id);
if (ps == NULL){
printf("Student not found\n");
} else {
char *depts[] = {"CS", "EE", "IE", "CE", "ME"};
printf("+--------------------+--------------------+------+------+------+\n");
printf("| FirstName | LastName | ID | GPA | Dept |\n");
printf("+--------------------+--------------------+------+------+------+\n");
printf("|%20s|%20s|%6d|%6.2f|%6s|\n", ps->firstName, ps->lastName, ps->id, ps->gpa, depts[ps->department]);
printf("+--------------------+--------------------+------+------+------+\n");
} //end-else
break;
case 6:
done = 1;
break;
default:
printf("!!!!!!!!!! Invalid choice. Try again :-))\n");
break;
} /* end-switch */
} /* end-while */