Menu with switch creates loop
Jun 30, 2020 at 10:32am UTC
Hello, I am new programming and I have a problem that I cannot solve. I am making a menu with switch with void functions so that they only print a message when the case option is selected. The code compiles well, but the problem is that when I select any menu option, the message is printed over and over as if it were a loop that is not closed and I don't know why if void does not return and the case break is declared . Please help me, I just want the message to be printed once.
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void Play(void );
void Instruciones(void );
void Creditos(void );
void Salir(void );
void f_menu(){
int opcion;
cout<<"\n\n\t\t\t\t PLAY" <<endl;
cout<<"\n\n\t\t\t\tINSTRUCCIONES" <<endl;
cout<<"\n\n\t\t\t\t CREDITOS" <<endl;
cout<<"\n\n\t\t\t\t SALIR" <<endl;
cout<<"\n\nPRESIONE 5 PARA RETROCEDER" <<endl;
cin>>opcion;
do {
if (opcion==5){
cout<<"Chau." ;
break ;
}
switch (opcion){
case 1: Play();break ;
case 2: Instruciones();break ;
case 3: Creditos();break ;
case 4: Salir();break ;
default : cout<<"Opcion incorrecta." <<endl; break ;
}
}while (true );
}
void Play(){
cout<<"El juego empezo..." <<endl;
}
void Instruciones(){
cout<<"El jugador debe..." <<endl;
}
void Creditos(void ){
cout<<"By..." <<endl;
}
void Salir(){
cout<<"Salio..." <<endl;
}
int main()
{
f_menu();
return 0;
}
Last edited on Jun 30, 2020 at 10:33am UTC
Jun 30, 2020 at 11:02am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void Play(void );
void Instruciones(void );
void Creditos(void );
void Salir(void );
void f_menu()
{
int opcion{0};
while (opcion != 5)
{
cout<<"1 PLAY" <<endl;
cout<<"2 INSTRUCCIONES" <<endl;
cout<<"3 CREDITOS" <<endl;
cout<<"4 SALIR" <<endl;
cout<<"5 PRESIONE 5 PARA RETROCEDER" <<endl;
cin>>opcion;
switch (opcion)
{
case 1:
Play();
break ;
case 2:
Instruciones();
break ;
case 3:
Creditos();
break ;
case 4:
Salir();
break ;
case 5:
return ;
default :
cout<<"Opcion incorrecta." <<endl;
break ;
}
}
return ;
}
void Play()
{
cout<<"El juego empezo..." <<endl;
}
void Instruciones()
{
cout<<"El jugador debe..." <<endl;
}
void Creditos(void )
{
cout<<"By..." <<endl;
}
void Salir()
{
cout<<"Salio..." <<endl;
}
int main()
{
f_menu();
return 0;
}
Topic archived. No new replies allowed.