0
I am making an options menu, with input option 1 or 0 and I want to appear an error when not typed 1 or 0, and so on until the user chooses a valid option and so the program redirects the user to the chosen screen, I managed to structure the code using goto, however I wonder if there was some way to use some loop instead of goto
, I couldn’t and I only have goto
.
void main()
{
int p1, s2, s3;
system("clear");
printf("Digite:\n[1] Para acessar o menu\n[0] Para sair do programa\n\n>>> ");
scanf("%i", &p1);
switch (p1)
{
case 0:
exit:
system("clear");
printf("Saindo\n\n");
break;
//Pagina do Menu
case 1:
menu:
system("clear");
printf("Menu\n\n");
break;
default:
do{
system("clear");
printf("Comando invalido!\n\nDigite:\n[1] Para acessar o menu\n[0] Para sair do programa\n\n>>> ");
scanf("%i", &p1);
}while (p1!=0 && p1!=1);
if (p1==0){
goto exit;
}
if (p1==1) {
goto menu;
}
}
}
Read on Edsger W. Dijkstra: Go To Statement Considered Harmful.
– Augusto Vasques