-3

I just have a loop that returns to the start if a invalid option has been entered

    do{                     
        printf("Please enter a option, Press 'q' to quit\n"); 
        scanf("%d",&option);

        if(option == 1){
            option1(...);
            break;
        }else if(option == 2){
            option1(...);
            break;
        }else if(option == 3){
            option1(...);
            break;
        }else if(option == false){
            printf("exit\n");
            exit(EXIT_SUCCESS);
        }else{
            continue;
        }
    } while(true);

My problem is

If I run it and enter a input eg: '5' it returns to the start of the do statement and I have to enter another input.

If I enter 'q' to quit the loop goes crazy and keeps repeating itself.

3
  • possible duplicate of Using scanf to accept user input Commented Oct 20, 2014 at 10:00
  • What type is option? And why are you comparing it to false? Commented Oct 20, 2014 at 10:01
  • 1
    You could read them as characters (char) and compare them to characters '1', '2', 'q'. Beware newline characters. Commented Oct 20, 2014 at 10:06

2 Answers 2

3

If you want to quit the program using q, you should declare variable option as char.


For example:

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

int main()
{
    char option;
    do
    {                     
        printf("Please enter a option, Press 'q' to quit\n"); 
        scanf(" %c",&option);

        if(option == '1')
        {
            //option1(...);
            printf("%c",option);
            break;
        }
        else if(option == '2')
        {
            //option1(...);
            printf("%c",option);
            break;
        }
        else if(option == '3')
        {
            //option1(...);
            printf("%c",option);
            break;
        }
        else if(option == 'q')
        {
            printf("exit\n");
            exit(EXIT_SUCCESS);
        }
        else
        {
            continue;
        }
    } while(1);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

-1

The 'q' is not read with scanf("%d".... option therefore probably has none of the values switched over, so that the default case is executed which repeats the loop. That's what you see. Remedy:

  1. Any non-numeric input means break: Just leave everything as is but change continue to break and exit the loop.

  2. Catch non-numerical input and handle it. The scanf manual page (http://linux.die.net/man/3/scanf) says:

    These functions return the number of input items successfully matched and assigned

    You could check the scanf return value and, if 0, try to scanf the letter in another statement; if 'q', break.

  3. Read a char right away (seems cleanest) and switch over the char, not the int value represented by it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.