select option c ++

0

How can I make the user select an option?

----- Menu ------

read

open

close

I have this, but it does not work:

char menu;

cin>>menu;

while(menu != 'l' && menu != 'a' && menu != 'c'){
    cin>>opcion;
}
    
asked by Jon 30.12.2017 в 18:57
source

2 answers

2

I recommend you do it with a do-while, since you need it to run at least once:

char op;
do{
   cout << "MENU" << endl;
   cout << "1.- Leer" << endl;
   cout << "2.- Abrir" << endl;
   cout << "3.- Cerrar" << endl;
   cout << "Su opcion: ";
   cin >> op;
   switch(op){
   case '1': // LO QUE QUIERES QUE PASE
   break;
   case '2': // LO QUE QUIERES QUE PASE
   break;
   case '3': cout << "Saliendo del programa...";
   break;
   default: cout << "Opción invalida";
   break;
 }
}while(op!='3');

To avoid errors I put the variable that the user enters an option like char so that if the user enters a letter, that infinite loop is not left. You can also change the numbers by letters if you want them to be executed when entering a letter.

    
answered by 31.12.2017 в 04:35
1

Usually menus of this type are inside a while that is broken when the user enters the close option. Assuming that what you want in this type of menu maybe this will help you

int opt;

while(true)
{
    std::cout << " ----- Menu --------\n" <<
              " 1- leer \n 2-escribir \n 3-cerrar\n";
    std::cin >> opt;

    if(opt == 1)
        std::cout << "leer seleccionado \n";
    else if(opt == 2)
        std::cout << "escibir seleccionado\n";
    else if(opt == 3)
        break; // aqui rompe el lazo
    else
        std::cout << "opcion invalida\n";
}
    
answered by 30.12.2017 в 20:16