简体   繁体   中英

how to control input data format (C)?

anyone knows an efficient way to check out the format of an scanf'ed data?

eg if I try to read an integer and I type a character, how would you do to tell the program that is not correct?

You can check if scanf() succeeds, it returns the number of successful conversions it performed.

You should always check this, before relying on the result since if it failed the variable(s) might contain undefined data leading to undefined results if referenced.

You can use if to check, and re-try with a different conversion specifier on failure:

if(scanf("%d", &x) == 1)
  printf("got integer %d\n", x);
else if(scanf("%c", &y) == 1)
  printf("got character '%c'\n", y);
else /* more attempts */

Of course it can become troublesome if there are "sub-matches", so the order can matter. It's also way better to split the input processing into two steps for the above:

  1. Read a full line of input using fgets()
  2. Use sscanf() to parse the line

That way you avoid problems due to the input being streamed in:

char line[128];

if(fgets(line, sizeof line, stdin) != NULL)
{
  int x;
  char y;

  if(sscanf(line, "%d", &x) == 1)
    printf("got integer %d\n", x);
  else if(sscanf(line, "%c", &y) == 1)
    printf("got character '%c'\n", y);
}

Note that if you wanted to scan for both an integer and a float, it can still become troublesome since a typical float (such as "3.1416" ) begins with what is a legal integer. For those cases you can use the strtoXXX() family of functions, which let you check the remainder after doing the conversion.

As you have mentioned in the question that you are playing with numbers and chars only there is a very simple solution as follows

//while reading a char
scanf("%c",&temp);
if(!((temp >=  65 && temp <= 90) || (temp >= 97 && temp <= 122)))
printf("Only characters are allowed!\n");

hope this helps!

scanf("%s", &c);
if(!atoi(c)) puts("You have entered a character");
if(atoi(c) != 0) puts("You have entered an integer");
Scanner sc = new Scanner (System.in);

try {

       // assume that the input from the user is not an integer, 
       // in that case the program cannot convert the input (which is a String) into
       // an integer. Because of this situation it'll jump to the 'catch' part of the 
       // program and execute the code. 

       int input = Integer.valueOf(sc.nextInt);

       // if the input is an integer lines below the above code will be executed. 


       // Ex. "int x = ( input + 10 ) "


}

catch (Exception ex) {

        System.out.println("Invalid input, please retry!");

        // if you want to get more information about 
        // the error use the 'ex' object as follows.

        System.out.println(ex);


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM