0

I am using c and I am a newbie.

I want to get a input (1 or 2 or 3) and I will give the user suggest;

printf("please do it\n");
printf("1. \n");
printf("2. \n");
printf("3. \n");
char opt;
scanf("%c"&opt");

if opt is not 1 or 2 or 3 then

printf("error\n");
printf("please re do it");

and all is in a while(true) loop until user enter the enter(new line charactor) to exit;

and how to do it?

I tried to create a function.

void get_order(char opt){
    switch(opt){
        case '1':break;
        case '2':break;
        case '3':break;
        default:
        printf("error\n");
        printf("please re do it"):
        char option;
        scanf("%c",&option);
        get_order(option);
    }
}

but it not work. thank you.

6
  • 3
    You state that this is in a while(true) loop, but there is no such loop in your code. You should provide a complete example (including a main and all included files). See stackoverflow.com/help/minimal-reproducible-example Commented Dec 20, 2021 at 14:23
  • 2
    Also read sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html Commented Dec 20, 2021 at 14:23
  • 2
    That is not a good approach, your code is using recursion, it will consume memory more and more while the user don't enter the correct input. Use a loop instead. Commented Dec 20, 2021 at 14:29
  • 2
    If your compiler lets scanf("%c"&opt"); through, turn on more warnings or change to a better compiler. Commented Dec 20, 2021 at 14:31
  • Related question: Validate the type of input in a do-while loop C In my answer to that question, I provided a function get_int_from_user. That function may be useful for you, in the sense that it keeps reprompting the user for input until the user enters a valid number. However, the part about verifying that this number is between 1 and 3 would have to be added by you. Commented Dec 20, 2021 at 14:40

1 Answer 1

2

That is not a good approach, your code is using recursion, it will consume memory more and more while the user don't enter the correct input. Use a loop instead. Your code should look like this:

#include <stdio.h>

int main() {
    printf("please do it\n");
    printf("1. \n");
    printf("2. \n");
    printf("3. \n");
    char opt;
    scanf("%c", &opt); //correct scanf
    scanf("%*c"); //consume the line break
    while(!(opt == '1' || opt == '2' || opt == '3')) {
        printf("error\n");
        printf("please re do it\n");
        scanf("%c", &opt); //correct scanf
        scanf("%*c"); //consume the line break
    }   
    return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

The line scanf("%*c"); will not consume the newline character if the user entered more than one character of input.
Better to use scanf( " %c", &opt ) to skip over any leading whitespace. Of course, better to not use scanf at all for this.
what should I use if not use scanf?@JohnBode
You can use other types of input handlers, but they are a bit harder to use. Like getline, gets and some others.
@riquefr you should not recommend gets.
|

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.