Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3441024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:30:07+00:00 2026-05-18T08:30:07+00:00

I want to create a simple menu in C program that accepts single character.

  • 0

I want to create a simple menu in C program that accepts single character. The menu will be like this:

  1. [S]how
  2. [E]xit

If the user enter ‘1’,’s’ or ‘S’ the program will print “Hello” and prompt again for the input
else if the user enters ‘2’,E or ‘E’ the program ends.
else it should print “invalid input” and prompt again.

I am able to create the program but the problem is that when user enters 12, 13, 14, 15, 16…..so on starting with 1, it shows Hello and same for other options.

My code is:

#include <stdio.h>

void clearBuffer();


int main() {

    int i = 0;
    char selection;

    do
    {
        printf("\t1. [S]how\n");
        printf("\t2. [E]xit\n");
        printf("Enter your selection from the number or character noted above: ");
        scanf("%s", &selection);
        clearBuffer();

        if (selection == '1' || selection == 's' || selection == 'S')
            printf("Hello");
        else if (selection == '2' || selection == 'E' || selection == 'x')
            i = 0;

 } while(i != 0);



}

void clearBuffer()
{   

    while(getchar() != '\n');

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:30:08+00:00Added an answer on May 18, 2026 at 8:30 am

    You could use strlen, which is part of the standard C library, to check the length of the string returned by scanf and reject entries longer than one character:

    if (strlen(selection) > 1)
    {
        printf("Invalid selection.");
    }
    

    Alternatively, I think you could use getchar() to accept just a single character from the user, which means they wouldn’t have to press enter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a component that will appear as a navigation menu for
I want to create an simple menu that is ordered by how many products
I want to create a simple http proxy server that does some very basic
I want to create a simple bit of JS code that creates an image
I want to create a very simple HTML/AJAX based GUI for a Python program.
Let's say that you want to create a dead simple BlogEditor and, one of
I've found a great little program that will allow me to add user friendly
I want to create a simple menu function which can call it example get_menu()
Is is possible to create a custom menu item within eclipse that will perform
I want to create a simple editor like Notepad++ with simple functionality... I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.