Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C programming - scanf function -Help needed.?
I'm creating a simple minigame software in Dos mode.
I used the switch statement to switch between games.
First of all, I'll give you the few code lines that I'm having trouble with.
case 1:
case1label: //the label to mark the position
system("cls");
printf("\n\nThis is the Math Quiz game. \n\n 1] Continue:\n 2] Back to Main menu:\n");
scanf("%d",&choice);//getting user input to select the choice
if(choice==1){
mathquiz();
}
else if(choice==2){
goto start;
}
else {
printf("\nPlease enter your choice correctly:");
delay(1200);
goto case1label;
}
break;
now this code works perfectly if the user enters an integer value as the choice. But when he enters a character (char) or any other key, the whole program crashes. please tell me what's wrong here. or what can I do to make it work.
**Please note that I'm new to programming (still a basic level student).
**this program is written in C.
4 Answers
- cjaLv 78 years agoFavorite Answer
For good user input validation in C, you should use fgets and sscanf, as shown in my example below. Also, as another responder suggested, there is no reason to use goto; in general it is not good programming practice. (http://www.cs.utexas.edu/~EWD/ewd02xx/EWD215.PDF)
#include <stdio.h>
#define MAX_LINE_LEN 32
#define MENU_MIN 1
#define CONTINUE MENU_MIN
#define MENU_MAX 2
#define MAIN_MENU MENU_MAX
#define QUIT (MENU_MAX + 1)
typedef enum { false = 0, true } bool;
const char *menu =
"\nThis is the Math Quiz game.\n"
" 1] Continue\n"
" 2] Back to Main menu\n"
" 3] Quit";
void mathquiz(void);
int getInt(int minValid, int maxValid);
int main(int argc, char *argv[]) {
int menuSel = 0;
while (menuSel != QUIT) {
puts(menu);
switch(menuSel = getInt(MENU_MIN, QUIT)) {
case CONTINUE:
mathquiz();
break;
case MAIN_MENU:
puts(menu);
break;
case QUIT:
puts("bye");
break;
default:
puts("?");
break;
}
}
return 0;
}
void mathquiz() {
puts("**\n** The math quiz...\n**");
}
int getInt(int minValid, int maxValid) {
static char line[MAX_LINE_LEN];
static char junk[MAX_LINE_LEN];
bool inputOk;
int z;
for (inputOk = false; inputOk == false; ) {
printf("> ");
fgets(line,MAX_LINE_LEN,stdin);
inputOk = ((sscanf(line,"%d%s",&z,junk) == 1) &&
(z >= minValid) && (z <= maxValid));
if (inputOk == false) {
printf("invalid input, try again:\n");
}
}
return z;
}
#if 0
Sample run:
This is the Math Quiz game.
1] Continue
2] Back to Main menu
3] Quit
> 1
**
** The math quiz...
**
This is the Math Quiz game.
1] Continue
2] Back to Main menu
3] Quit
> x
invalid input, try again:
>
invalid input, try again:
> 2
This is the Math Quiz game.
1] Continue
2] Back to Main menu
3] Quit
This is the Math Quiz game.
1] Continue
2] Back to Main menu
3] Quit
> 3
bye
#endif
- ?Lv 58 years ago
I can tell you where the problem is. In scanf your formatting string is
"%d"
which means scanf is waiting for an integer. This means that if anything else is entered, it crashes with an error. One way around this is to instead tell scanf to read a string of characters. This is done with
"%s"
and make sure to change count from an integer to a string of characters. You can then create a loop to check if each character in the string is an integer from 0 to 9 (if you know regular expressions, this is very easy, else it might be a little tougher but possible). If the string contains any other characters, ask the user to enter a new input. If it only contains numbers it is a valid integer so parse it to an int using atoi. I provided two links below, one is a reference for scanf if you have any other questions about it, the other is a reference to how to use the atoi function.
Source(s): http://www.cplusplus.com/reference/cstdio/scanf/ http://www.codingunit.com/c-reference-stdlib-h-fun... - Anonymous8 years ago
If you want to make a robust program that can handle incorrect input from the user, you need to use scanf to accept incorrect input from the user. You can read in the input as a string of characters and decide if it was correct or incorrect input, and scanf a new answer if the other answer was incorrect.
- 8 years ago
The previous answers were spot on! I just wish to add a side note that NEVER use the goto function and try not to use system commands unless really necessary.