Infinite loop

Does anybody know what might be causing this while() loop to go on indefinatly when a char is entered? It works fine for an int but a char will cause the printf statement to repeat. Any help would be greatly appreciated.

#include <stdio.h>

#define STORE_NAME "Sierra Sporting Goods"

int main(void){
int response = 0;


printf("\t%s\n\n", STORE_NAME);
printf("1 = Add a record\n");
printf("2 = Report\n");
printf("3 = Delete a record\n");
printf("4 = Change a record\n");
printf("5 = Quit\n");

while(response != 5 ){
scanf("%d", &response);
printf("\nplease enter 5 when done\n");
}

return 0;
}

Last edited on
You need the user to enter the number 5 (as char) to exit the loop but the check is if it is numerical 5.
Your while statement must be while(response!='5'){ to check for alphanumerical 5.
Also the first time that it checks the while the response doesn;t have any value. Consider using a do-while loop.
Last edited on
Topic archived. No new replies allowed.