How to restrict the user to only input whole number and refuse any other characters in scanf?

最后都变了- 提交于 2021-02-08 10:20:13

问题


I'm trying to get user input and allowing them to only input a whole number which is also positive.

I'm trying to figure out this task

printf("Enter the first and second number in whole number: ");
scanf("%d,%d", &first, &second);

if (input != '\n') //notice this line I'm trying to check if is a character//
    printf("Error! CHARACTER NOT ACCEPTED!!\n");

else if (First <= 0)
    printf("Error! First must be a positive value!\n");

else if (Second <= 0)
    printf("Error Second must be a positive value!\n");

The code above will check for two things. One, if the user has input the characters I want. Second, if the number is positive.

How do I realize these checks?


回答1:


You cannot restrict the characters the user inputs. You should read whatever the user gives you and decide what to do with it.

If you want to use scanf, it could be like this:

int ret = scanf("%d", &num);

scanf's return value shows the number of %s it successfully read. For example:

12      <-- this input returns 1, num will be 12
asd     <-- this input returns 0, num will be untouched
123xf   <-- this input returns 1, num will be 123

In the last example, xf will stay in input and further scanfs would read it.

So for the first if, you simply check if the return value of scanf is 1. If not, then you couldn't read a number from input.

For the second if, check if num <= 0




回答2:


Read a whole line (with getline), then convert it to a number, perhaps using strtol (and testing the end pointer).

Or use the %n format for scanf and test the number (of successful conversions) returned by scanf




回答3:


Do not use scanf! Just read the input and then call strtoul with a second argument and check that the value returned in the second argument is '\0'.




回答4:


Read the input as text, then convert to an integer using strtol:

int val;
char inbuf[SIZE]; // where SIZE is long enough to handle expected inputs
...
if (fread(inbuf, sizeof inbuf, stdin) != NULL)
{
  char *chk;
  val = (int) strtol(inbuf, &chk, 10);
  if (!isspace(*chk) && *chk != 0)
    printf("%s is not a valid integer value\n", inbuf);
}

strtol will read the string in inbuf and convert it to the equivalent integer value. chk will point to the first character in inbuf that isn't converted. If that character is not whitespace or 0, then the string was not a valid integer.




回答5:


I suppose you need something like this :

#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char szInput [256];

   printf ("Enter a number: ");
   fgets ( szInput, 256, stdin );
   i = atoi (szInput);
   if(i<0)
   printf("Error! Input must be a positive value!");


   else{

        //Do the rest

       }
   return 0;
}

Note that the atoi() expects the string representation of a decimal, in case if it receives a character it returns a 0 so you need not take care of characters anymore!

EDIT :

With some research I found that its better to use Strtol() than atoi(). See this for clarification : Why not to use atoi()



来源:https://stackoverflow.com/questions/8194521/how-to-restrict-the-user-to-only-input-whole-number-and-refuse-any-other-charact

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!