0

I hope you can help me.

I wrote the following code so that when selecting option 1, the user registers the name of a plant (eg Maguey de sol), but when executing the program it does not allow me to enter the name and returns me to the main menu.

What can I do to solve this problem?

#include<iostream>
#include <locale.h>
#include <string>

using namespace std;

int main(){
    
    setlocale(LC_CTYPE, "Spanish"); // Español
    
    int menu;
    string nombre;
    
    // == Menú principal ==
    do{
        
        cout << "\n \t OPUNTIA: CUIDADO DE AGAVÁCEAS, CACTÁCEAS Y CRASULÁCEAS" << endl;
        cout << "\t Registra tu planta y planea sus cuidados \n" << endl;
        cout << "\t 1. Nombre" << endl;
        cout << "\t 2. Familia" << endl;
        cout << "\t 3. Terminar registro \n" << endl;
        
        cin >> menu;
        
        // Submenú 1 == REGISTRO DE NOMBRE ==
        if( menu == 1 ){
            
            cout << "\n \t Ingresa el nombre de la planta \n \n" << endl;
            getline(cin, nombre);
            
            cout << "Continúa con el registro de " << nombre << "\n \n" << endl;
            
        // Submenú 2 == REGISTRO DE FAMILIA ==
        }else if( menu == 2 ){
            
            cout << "Ok" << endl;
        
        // == REGISTRO COMPLETO ==  
        }else if( menu == 3 ){
            cout<<"\n \t ¡Registro completo! \n \n";
            
        // == OPCIONES INVÁLIDAS ==
        }else{
            cout<<"\n \t ¡Elige una opción válida! \n \n" << endl;
        }       
    }while( menu != 3 );
    
    return 0;
}
  • Mixing `std::cin` and `std::getline()` requires some extra work on your part. Your material should have covered this. – sweenish Dec 09 '21 at 15:34

0 Answers0