# Thread: Denying a decimal value

1. ## Denying a decimal value

Ello. I just made a program with 4 choices at a menu. However, if I enter a decimal like "1.5" the program just loops over and over. I'm not really sure how to I guess "deny" a decimal value, I've tried a few things but they didn't work. Here's my code:
Code:
{
printf("1. Play the guessing game.\n");
printf("2. Play the square game.\n");
printf("4. Quit.\n");

while(1)
{
int choice = 0;

scanf("%d", &choice);

if(choice < 1 || choice > 4)//If the user-inputted choice is not a correct value on the menu, return an error message.
{
printf("Sorry, that is not a valid choice. Please try again.\n");
}

else
{
return choice;
break; //If the user-inputted choice is a correct value on the menu, break out of the loop.
}
}
}

2. Move line 11 to be line 3.

choice is an integer if you enter 1.5 the .5 part gets lost and it should see just the 1 ... there may be problems in other parts of your code causing it to loop. What happens if you enter a 1?

Also you should be checking the return value of scanf()...
Code:
int choice = 0;

while (1)
{
if (scanf("%d",&choice) && choice >0 && choice < 5)
return choice;

puts("Sorry invalid choice");
}

3. Any other value that's within limits does what it's supposed to do, if the value is a whole number outside the limits, it asks for a new value until a correct value is taken. Only decimals are screwing this up :l.

4. Originally Posted by shukiren
Any other value that's within limits does what it's supposed to do, if the value is a whole number outside the limits, it asks for a new value until a correct value is taken. Only decimals are screwing this up :l.
That's why you need to check the return value of scanf()... don't be lazy... look it up!

5. Nvm >.<. Wasn't reading the program details close enough. Says: "The users will enter the correct type of information requested at all times." Woo. Thanks for the help anyway.

6. But I'm still gonna look into this cause I'm curious.