With my code below how would I either ignore or remove decimal numbers? I tried typecasting it but that messed up my code. I can't think of any way to make a loop to accept only integers either :(. I don't understand why the scanf is accepting decimal numbers when I am using %d and I declared my variables as ints. Any ideas would be greatly appreciated :).

#include <stdio.h>

int main (void)
{
    int x , y;
    int z;
    
    printf("Please enter a value for x:\n");

    printf("x: ");
    scanf("%d" , &x);
    //x = (int) x;
    while((x < 0) || (getchar() != '\n'))
    {
        printf("Please enter interger greater than 0\n");
        printf("Please enter a value for x:\n");
        printf("x: ");
        scanf("%d" , &x);
    }

    printf("Please enter a value for y:\n");
    printf("y: ");
    scanf("%d" , &y);
    //y = (int) y;
    while((y < 0) ||(getchar() != '\n'))
    {
        printf("Please enter interger greater than 0\n");
        printf("Please enter a value for y:\n");
        printf("y: ");
        scanf("%d" , &y);
    }

    return 0;
}

scanf is a notoriously difficult function to use, especially for beginners.

try using fgets in conjunction with atoi instead.

char strInput[MAX_LEN_INPUT];
int value;

fgets(strInput, MAX_LEN_INPUT-1, stdin);
value = atoi(strInput);

once you figure out this basic method, use strtol instead of atoi, and you can make a pretty bulletproof input routine


.

Edited 6 Years Ago by jephthah: n/a

scanf is a notoriously difficult function to use, especially for beginners.

try using fgets in conjunction with atoi instead.

char strInput[MAX_LEN_INPUT];
int value;

fgets(strInput, MAX_LEN_INPUT-1, stdin);
value = atoi(strInput);

once you figure out this basic method, use strtol instead of atoi, and you can make a pretty bulletproof input routine


.

How would I do that with my code that I have? What you showed me in that code is really confusing to me.

instead of using "scanf(...."
use the two lines that i showed you.

the first one (fgets) gets the input... it takes input from the standard input (stdin, typically the keyboard), looks at the first "MAX_LEN_INPUT-1" characters, and stores them in the character string "strInput"

the second line converts the string to an integer.

you could also do floatVal = atof(strInput); to get a floating point input.

(you will need to define MAX_LEN_INPUT as some value...

okay look, try this and see what i mean:

#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN_INPUT      30

int main (void)
{
   char strInput[MAX_LEN_INPUT];
   int value_i;
   float value_f;

   printf("enter an integer value:\n");
   fgets(strInput, MAX_LEN_INPUT-1, stdin); 
   value_i = atoi(strInput);
   printf("you entered: %d\n",value_i);


   printf("enter a floating point value:\n");
   fgets(strInput, MAX_LEN_INPUT-1, stdin); 
   value_f = atof(strInput);
   printf("you entered: %f\n",value_f);

   return 0;
}

im just typing this out freehand, this is not bulletproof code by any means, but is a good place for a beginner to start with concepts.

It's actually much more difficult than jephthah described.

In order to make sure the user types only the correct thing, you need to look at all the characters entered and test each character for validity (called parsing). Then you can decide to convert the values into integers and floats or tell the user his input is in the wrong format.

Although scanf() is not recommended as mentioned above, sscanf() is a good way to do the conversion after you've verified the input is in the proper format.

This article has been dead for over six months. Start a new discussion instead.