0

I am working on program where a list of options is displayed to the user and he would then enter an integer to specify which option he wants to select.Now I have pre-empted the situation where the user might enter an integer value apart from the valid ones. But if he enters any other value, say a character or a string, the program goes into an infinite loop with the list of options being printed infinitely. How can i rectify this? I mean, I should be able to give my user an error when for a predefined integer variable he enters a value that is not an integer.

Any help appreciated. Here is a sample code.

do{
    printf("Select appropriate option.\n");
    printf("Press 1 to do this");
    printf("Press 2 to do that.\n");
    printf("Press 3 to exit the program.\n");
    scanf("%d",&choice);
    if(choice!=1 && choice!=2 && choice!=3)
        printf("You entered an invalid choice. Please enter again.\n");
    switch(choice){
        case 1:
            //do this
             break
        case 2:
            //do that
            break;
        case 3:
            exit(1);
           }}


while(choice!=3);

So basically when a user enters a non-integer value for choice I want the program to notify the user and ask him for another input.

4

4 回答 4

1

It cannot be done with direct scanf into an integer variable. Such scanf will not only accept 1.23, it will also accept 1abcde and other inputs. scanf (and other conversion functions) reads as much as it can in accordance with the requested format. It stops when it finds something that does not satisfy format requirements and simply leaves it untouched.

If you want to perform this sort of analysis, you have to read the input as string and then parse and analyze that string manually.

A C-style code sketch (since you insist on C-style code, despite having tagged it as [C++]) might look as follows

char buffer[100]; /* 100 should be enough for everyone :) */
int n = scanf("%s", buffer);
if (n < 1)
  /* ERROR, can't read */;

char *end;
long choice = strtol(buffer, &end, 10); 
if (end == buffer || *end != '\0' || errno == ERANGE)
  /* ERROR, bad format */;

/* ... */
于 2013-09-22T05:30:27.423 回答
0

scanf will not consume any non-digits when converting %d. It will return 0 because it didn't convert anything, and the "bad input" will still be there waiting to be consumed. You have to consume it in some way to be ready for a valid input.

(also note you're excluding 3 in your if before testing for it in your switch)

于 2013-09-22T05:25:52.133 回答
0
  1. Use iostream - see http://www.cplusplus.com/reference/iostream/
  2. Having said that if you insist on using scanf - check the return value - i.e. read http://linux.die.net/man/3/scanf
于 2013-09-22T05:35:47.143 回答
0

isdigit will check for any input that is a digit(number) type. For this include header ctype.h.

Also terminate your program using exit(0) if input is incorrect. For this include header stdlib.h.

#include<ctype.h>
#include<stdlib.h>

char c;
scanf("%c", &c);
if (isdigit(c)) 
{

case statement...
...
...
}
else
{
 printf("Wrong input");
 exit(0);
}
于 2013-09-22T06:22:00.657 回答