Repeat a menu and prompt if the user entered invalid choice in C -



Repeat a menu and prompt if the user entered invalid choice in C -

void menu(){ printf("\n"); printf("1. convert integers in decimal number scheme binary numbers \n"); printf("2. compute consecutive square root look \n"); printf("3. solve quadratic equation \n"); printf("4. print fun \n"); printf("q. quit\n \n"); printf(" come in choice: "); } main () { char choice; { menu(); scanf("%c", &choice); switch (choice){ case '1': ... case '2': .... case '3': ... case '4': .... default: printf("wrong choice. please come in again: "); break; } } while (choice != 'q'); }

here general idea, can't prompt wrong selection , repeat menu. when come in wrong choice, output follows: example, entered 5:

come in choice: 5 wrong choice, please come in again: 1. convert integers in decimal number scheme binary numbers 2. compute consecutive square root look 3. solve quadratic equation 4. print fun q. quit come in choice: (this input)

take @ below changes: alter scanf()

scanf(" %c",&choice);

a space before %c create sure special characters including newline ignored.without everytime there newline in buffer , scanf reads , see doesn't work expected.

after please create sure 1 time default case nail need break while() loop.

{ menu(); scanf(" %c", &choice); switch (choice){ case '1': break; case '2': break; case '3': break; case '4': break; default: { printf("wrong choice. please come in again: "); break; } } } while (choice != 'q');

c menu switch-statement choice

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -