Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to make a question in c languange multiple choice

+8 votes
asked May 22, 2025 by arjee bryant paragele (290 points)

1 Answer

+3 votes
answered May 22, 2025 by Peter Minarik (101,340 points)

You could do something like this:

#include <stdio.h>

int main()
{
    char choice;
    do
    {
        printf("What is your favourite food?\n");
        printf("\t1. Pizza?\n");
        printf("\t2. Burger\n");
        printf("\t3. Nachos\n");
        printf("? ");
        scanf(" %c", &choice);
    } while ('1' > choice || choice > '3');
    
    printf("Your picked: %c\n", choice);

    return 0;
}

The SPACE in the format string(" %c") before the "%c" means to read and ignore any white space character (e.g., newline from the last input).

commented Jun 27, 2025 by Jerry Jeremiah (2,040 points)
This: while ('1' > choice || choice > '3');
Should be: while ('1' < choice || choice > '3');
commented Jun 30, 2025 by Peter Minarik (101,340 points)
No, it shouldn't.

`while ('1' > choice || choice > '3')`

means that choice is less than 1 or greater than 3, that is, it falls into the invalid ranges, so we keep repeating the question.

In your suggestion,  `while ('1' < choice || choice > '3');` means that choice is larger than 1 or larger than 3, which translates to choice is larger than 1. Basically, only 1 could be an acceptable answer, not 2 or 3.

But I appreciate people reading what I wrote and trying to fix it. ;)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...