How to prevent users from entering incorrect data?

The program asks for a number. The program should loop the scanf() function if the user enters anything else but numbers.

The code,

  do{ printf("Enter row number\n"); scanf("%d",&row); }while(row>='a' && row<='z'); 

But the code above does not work. I get an error when entering a letter. I tried to manipulate around him, and it all ends endlessly.

Any suggestions?

Also, how can I tell the C Compiler not to interrupt the loop if the input is not INTEGER?

0
source share
2 answers

You ignore the return from scanf() , which tells you if the conversion information ( %d ) was accurate or not. If this was inaccurate, you need to perform error recovery, which is not very convenient with scanf() . Most people use the β€œread the input line and then parse it” approach, where error recovery is easier.


I understand that return values ​​are necessary for checking errors, but how do I scan their numbers or letters? Can I say if (input! = (Integers)) or something like that?

This is why people do not use scanf() . If you get a string of data into the buffer (an array of characters), you can check the contents of the array as often as you like. If you use scanf() , you have no reliable ability to process the data until after scanf() decides that it has an error.

Functions (usually also available as macros) in <ctype.h> allow you to classify characters. The functions in <stdlib.h> provide robust conversions from strings to integers of various types.

So you can think of doing something like:

 char buffer[256]; while (fgets(buffer, sizeof(buffer), stdin)) { ...check contents of buffer using isdigit() etc... ...or just plough ahead with... long value; char *end; errno = 0; value = strtol(buffer, &end, 0); if (errno != 0 || (*end != '\0' && !isspace(*end)) ...diagnose problems... } 

This code is a bit out of my league at the moment .. is there an easier way?

Well, I suppose you can use atoi() instead of strtol() , which simplifies error handling (because it is less accurate):

 char buffer[256]; while (fgets(buffer, sizeof(buffer), stdin)) { int value = atoi(buffer); if (value == 0) { puts("zero read - exiting loop"); break; } } 

It is not much easier. I do not know what part of the previous decision you considered outside of you. The alternatives, it seems to me, are very confusing, including reading one character at a time and saving numbers and discarding numbers:

 char buffer[256]; char *dst = buffer; int c; int value; while ((c = getchar()) != EOF) { if (isdigit(c)) { *dst++ = c; /* Ignoring buffer overflow - bad! */ } else if (isspace(c)) { *dst = '\0'; value = atoi(buffer); break; } else { printf("Unexpected character '%c'!\n", c); } } 

Etcetera. There are various problems in this code - for example, resetting the pointer after an erroneous character and eliminating buffer overflows, as well as accessing signs on numbers and ... well, all kinds of things that I prefer to leave to subroutines, for example, fgets() and strtol() .

+3
source

You need to read the characters and then determine if the character is numeric and convert as needed. You will also want to put a check on some special character, such as β€œq” or something, to take you out of the loop when you finish typing characters.

0
source

Source: https://habr.com/ru/post/1309221/


All Articles