-
Exiting
I'm having a problem with my program exiting when I want it to. I want it to exit the program completely if 'q' is pressed, yet it goes on to the 'menu' function even if 'q' is pressed....Can someone help me with getting the program to exit completely if q is pressed. Here is the code I have and any help would be appreciated.
Code:
if (Filename!="junk.txt")
{
printf("File does not exist\n");
printf("Press (c) to create one, or (q) to quit: ");
scanf("%d",&answer);
switch(answer)
{
case 'c' : WriteRecordsToFile(Guests);break;
case 'q' : exit(0);
}
}
TotalRecords = ReadRecordsFromFile(Guests);
Menu(Guests);
-
Code:
scanf("%d",&answer); /* expects answer to be an int */
/* 'q' is not successfully converted to an int */
switch(answer)
{
case 'c' : WriteRecordsToFile(Guests);break;
case 'q' : exit(0);
}
I believe you want one of the following two, depending on answer's type.
Code:
int answer;
answer = getchar();
Code:
char answer;
scanf("%c",&answer);
Both should check return values before continuing on to use answer. (This question is a result of not doing so.)
-
Try to read a value as a character ( %c ) instead.
Are you sure you're not having CAPS LOCK pressed?
-
Should have known
Thanks for your help. I should have know it was something simple...I've got it working now.