Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
if and else user verification issue
int main()
{
    int account_or_not;
    account obj;
    menu menu_obj;

    cout << "Do you have an account? 1 for yes or 2 for no " << endl;
    cin >> account_or_not;


    if (account_or_not == 1)
    {
        menu_obj.user_menu();
    }
    else if (account_or_not == 2)
    {
        obj.create_account();
    }
    else
    {
        cout << "You entered a value other than 1 or 2 please try again "<< endl;
        cin >> account_or_not;
    }
  
    return 0;
}

This is what is in the main of my code, and im not sure if I am being dumb, but I have tried a switch case and if else and in the else (or default for switch) when I check what the user has entered was wrong and prompt them to enter it again the program ends when they enter the correct value. E.g if 3 was entered it would tell the user to try again, then when 1 or 2 is entered the program ends. Any ideas, thanks in advance.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This is due to the fact that you are not looping until the user enters the right input. You can use do-while loop to do so!

Refer this for more info on do-while loops in C++.

over 4 years ago · Santiago Trujillo Relatório

0

Let's go in tiny steps. First, let's ask the user only once to correct their input and let's further assume that the user must get it right on the second attempt. You should first check for valid input and then use it, not the other way around:

cin >> account_or_not;
if (account_or_not != 1 && account_or_not != 2) 
{
    cout << "You entered a value other than 1 or 2 please try again "<< endl;
    cin >> account_or_not;
}

// assume input is now valid...
if (account_or_not == 1)
{
    menu_obj.user_menu();
}
else if (account_or_not == 2)
{
    obj.create_account();
}

This will only ask for corrected input once. If you want to keep asking until the user entered correct input you can use a loop:

cin >> account_or_not;
while (account_or_not != 1 && account_or_not != 2) 
{
    cout << "You entered a value other than 1 or 2 please try again "<< endl;
    cin >> account_or_not;
}

// assume input is now valid...
if (account_or_not == 1)
{
    menu_obj.user_menu();
}
else if (account_or_not == 2)
{
    obj.create_account();
}

Now this asks for valid input repedeatly until the user enters 1 or 2. However, it does not correctly handle the case of the user entering foo or user input is the worst that can happen to a program. If the user input cannot be parsed as an int then std::cin is in an error state that you need to recover from before reading more. I said I want to make tiny steps, and I don't have to repeat what has been written elsewhere, so I refer you to this Q&A for further reading: Good input validation loop using cin - C++.

over 4 years ago · Santiago Trujillo Relatório

0

Below is a working example using while loop. If the user enter 1 or 2 then do some work in the respective condition block and then break out of the while loop. On the other hand if the user enters 3 then ask the user for input again.

#include<iostream>

int main()
{
    int account_or_not;
    //account obj;
    //menu menu_obj;

    std::cout << "Do you have an account? 1 for yes or 2 for no " << std::endl;
    std::cin >> account_or_not;


    while(true)
    {
    if (account_or_not == 1)
    {
       // menu_obj.user_menu();
       std::cout<<"inside 1"<<std::endl;
       //do something and then break
       break;
    }
    else if (account_or_not == 2)
    {
        //obj.create_account();
        std::cout<<"inside 2"<<std::endl;
        //do something and then break
        break;
    }
    else
    {
        std::cout << "You entered a value other than 1 or 2 please try again "<< std::endl;
        std::cin >> account_or_not;
    }
    }
    return 0;
}

This does what you want. Try it out with different input like 1,2 or 3.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda