0

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?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Ali
  • 9,997
  • 20
  • 70
  • 105

5 Answers5

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

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

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'.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • I never study about `strtol` yet if so is this mean there is no possibility to succeed this task? – Ali Nov 19 '11 at 14:05
  • 1
    You can try something like scanf( "%u%1s", &d, s ), but if the user enters "34\n", then the scanf will hang. In many years of writing C, I have only ever used scanf in exercises in university. It has no purpose. Any problem for which scanf is suitable is a problem that should be solved in a different language. – William Pursell Nov 19 '11 at 14:08
  • Well I guess now my problem is that I'm not allow to use something that I never been study for since this is an assignment and if I use something else I guess my professor will deduct my mark off the problem is that he expect us to get the input from the user and at the same time if the user input any character then return the message and loop them back to input again... I have succeed with the negative number but not with the character :( – Ali Nov 19 '11 at 14:10
  • 1
    @Ali: that's unfortunate, because there's really no good way to do this with `scanf`. If you want to reject bad inputs like `3.4` or `12b` or anything else that begins with a digit but isn't a valid integer, you will *have* to read the input as text and then either use `strtol` or check and convert each character yourself. – John Bode Nov 19 '11 at 14:32
2

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

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()

Community
  • 1
  • 1
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • What made you think its not?? – COD3BOY Nov 19 '11 at 14:22
  • Well the problem is that I'm still new to C language and never study about the stdlib.h and I'm still at the very beginning of the foundation of C. – Ali Nov 19 '11 at 14:31
  • 1
    Well, we all are here to study, my case too is not much different! :) See I found a new thing and updated in ans. – COD3BOY Nov 19 '11 at 14:33
  • Well true but I'm afraid my professor going to deduct my mark because using new stuff that he never taught us to. but thanks anyway I'm going to look into it now. – Ali Nov 19 '11 at 14:35
  • @Ali please note one thing, don't change your question from time to time, this makes things harder for those who answered for the earlier question.Please keep this in mind, before asking a question make sure what you want to ask. Cheers! – COD3BOY Nov 19 '11 at 14:36
  • sorry I though I'm still in the same question about the character but I just put more codes in my post so that people can really see what I'm trying to achieve ... – Ali Nov 19 '11 at 14:37
  • 1
    Well,Tell your professor its from SO and ask him to come to SO , so that he too can learn new things :D , kidding! – COD3BOY Nov 19 '11 at 14:38
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

Shahbaz
  • 46,337
  • 19
  • 116
  • 182