How to prevent users from entering letters or numbers?

I have a simple problem:

Here is the code:

#include<stdio.h> main(){ int input; printf("Choose a numeric value"); scanf("%d",&input); } 

I want the user to enter only numbers ... Therefore, it should be something like this:

 #include<stdio.h> main(){ int input; printf("Choose a numeric value"); do{ scanf("%d",&input); }while(input!= 'something'); } 

My problem is that I do not know what to replace with "something" ... How can I prevent users from entering alphabetic characters?

EDIT

I only have something interesting:

 #include<stdio.h> main(){ int input; printf("Choose a numeric value"); do{ scanf("%d",&input); }while(input!= 'int'); } 

Adding "int" will continue as I enter the numbers, I tried "char", but that didn't work. Of course, something for the alphabets is correct ?: S Please answer!

Thanks for your help!

+4
source share
4 answers

The strtol library strtol converts the string representation of a number to its equivalent integer value and also sets a pointer to the first character that does not match a valid number.

 #include <stdio.h> #include <stdlib.h> #include <ctype.h> ... int value; char buffer[SOME_SIZE]; char *chk; do { printf("Enter a number: "); fflush(stdout); if (fgets(buffer, sizeof buffer, stdin) != NULL) { value = (int) strtol(buffer, &chk, 10); /* assume decimal format */ } } while (!isspace(*chk) && *chk != 0); 

If chk points to something other than a space or line terminator (0), then the string is not a real integer constant. For floating point input, use strtod .

+4
source

You cannot forbid the user to enter anything that he wants - you can ignore everything that he / she enters so that you do not "want".

A typical example is reading a line with fgets , then scanning line by line and verifying that all input was digits with isdigit . If these are all numbers, then convert to an integer; otherwise throw it away and get input again.

Alternatively use strtol for conversion. It sets a pointer to the end of the data, which it can convert to a number; in this case, you (apparently) want it to point to the end of the line.

If you don't mind intolerable code, you can read one character at a time and discard anything except numbers (for example, using getch on Windows).

+4
source

You should not use scanf to read in numbers - see http://www.gidnetwork.com/b-63.html

Use fgets instead.

However, if you must use scanf , you can do this:

  #include <stdio.h> int main() { char text[20]; fputs("enter some number: ", stdout); fflush(stdout); if ( fgets(text, sizeof text, stdin) ) { int number; if ( sscanf(text, "%d", &number) == 1 ) { printf("number = %d\n", number); } } return 0; } 
0
source

Personally, I read the input to the buffer and looked at this line for my number.

char buffer [100];
float value
do {
scanf ("% s", buffer);
} while (sscanf (buffer, "% f", & value)! = 1)

This will loop until the first thing the user enters into the string is a number. The input can be anything, but only passes by this block when the first number entered is a number.
input example:
43289 (value 43289)
43.33 (value - 43.44)
392gifah (value 392)
ajfgds432 (continues the loop)

0
source

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


All Articles