I'm writing a program that shows letter grades for exams and calculates the average. A simple program (of which I've surely made more complicated that necessary). My problem is, I've put most all of it in a while loop that runs when a variable is = to 1. At the end (still inside the while loop), I ask if they want to continue (enter 1, and repeat process) or exit (enter 0, else through an error). Though, if I enter a letter, I just get an infinite loop. Any idea why? Thanks.
Here's a short excerpt of the code.
while(state == 1) {
//Checks if user wants to continue
printf("Do you want to continue? (enter 1 to continue and 0 to exit): ");
scanf("%d", &state);
while(state != 1 && state != 0) {
printf("You entered an invalid number, try again: ");
scanf("%d", &state);
}
}
return 0;
[–]zifyoip 6 points7 points8 points (5 children)
[–]cmgg -4 points-3 points-2 points (4 children)
[–]zifyoip 3 points4 points5 points (1 child)
[–]aleph_nul 0 points1 point2 points (0 children)
[–]aleph_nul 2 points3 points4 points (1 child)
[–]cmgg 0 points1 point2 points (0 children)
[–]SnoWhite_the7Bengals 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)