Thread: Denying a decimal value

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    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:
    int menu()
    {
        printf("Please enter your choice from one of the following options.\n");
        printf("1. Play the guessing game.\n");
        printf("2. Play the square game.\n");
        printf("3. View your current score.\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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shukiren View Post
    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. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    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. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    But I'm still gonna look into this cause I'm curious.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help In C hex to decimal
    By hapyharra in forum C Programming
    Replies: 2
    Last Post: 11-12-2009, 06:54 AM
  2. how to convert decimal to hexa decimal in C/
    By kalamram in forum C Programming
    Replies: 4
    Last Post: 09-03-2007, 07:39 AM
  3. hex to decimal
    By caroundw5h in forum C Programming
    Replies: 5
    Last Post: 11-11-2004, 10:48 AM
  4. abc to decimal
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 03:08 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM