c - How to repeat a menu (ask for user input again) after switch got a wrong input character -
i have menu, 1 of options exit program, if user types character other 1 2 3 4 5 6 still exits programme or stops running it. want after input of wrong character menu apears 1 time again , user can type again. want hapen infinitly if user types wrong characters infinitly. lot!
#include<stdio.h> #include<stdlib.h> int main(void) { char opcao; printf("1 - \n"); printf("2 -\n"); printf("3 - \n"); printf("4 - \n"); printf("5 - \n"); printf("6 - terminar programa\n"); printf("introduza sua opcao:\n"); scanf("%c",&opcao); switch(opcao){ case'1': printf("funcionalidade nao disponivel."); break; case'2': printf("funcionalidade nao disponivel."); break; case'3': printf("funcionalidade nao disponivel."); break; case'4': printf("funcionalidade nao disponivel."); break; case'5': printf("funcionalidade nao disponivel."); break; case'6': exit(0); default: printf("invalid input, please type again"); // want, how?(now nowadays menu again... break; } homecoming 0; }
use do...while
loop code looks this:
#include<stdio.h> #include<stdlib.h> int main(void) { char opcao; printf("1 - \n"); printf("2 -\n"); printf("3 - \n"); printf("4 - \n"); printf("5 - \n"); printf("6 - terminar programa\n"); printf("introduza sua opcao:\n"); do{ //loop scanf(" %c",&opcao); //discards blanks , reads first non-whitespace character switch(opcao){ case'1': case'2': case'3': case'4': case'5': printf("funcionalidade nao disponivel."); break; case'6': exit(0); default: printf("invalid input, please type again:"); // want, how?(now nowadays menu again... } }while(opcao<'1' ||opcao>'6'); //loop until `opcao` less '1' or greater '6' homecoming 0; }
c menu switch-statement
No comments:
Post a Comment