How do I make it say "BYE" or "EXIT" after the user inputs "n"? ( I am very new to c++)

How do I make it say "BYE" or "EXIT" after the user inputs "n"? ( I am very new to c++)

Posted Feb 28, 2022, 10:34 PM UTC

166
odds_or_evans•

after the do while loop terminates, you would just add another cout statement to print it.

so right before you do the return 0; write

cout << “BYE”;

or something like that

6
Tobyb01001•

You could scrap the do while loop and just use an infinite while loop and check input like so

char userInput;

while (true)
{
    std::cout << "Enter something (n/y): ";
    std::cin >> userInput;

    if (userInput == 'n')
    {
        std::cout << "BYE" << std::endl;
        break;
    }
}
5
StarmanInDisguise•

using while (true) is dangerous and unnecessary. The do while works just fine.

1
wished_you•

Why ? Do while is perfect for the job.

1

your code us good.

you can actually put an if statement after:

cin >> again;

also your code is currently workin if user inputs Y or y.

try to implement a function that only accepts lowercase 'y' or 'n' as answers and throws a warning if the users answer is different than those.

so instead of being cin>>again;

it should be like

user_choice = again()

then the if statement will check if userchoice is 'n' and print "Bye" or 'Exit'

also userchoice should be a char type variabile and so should be the return value of the function again.

5
Ludant•

Your program never exits the loop unless user typed 'n'. So if they type 'n' they exit the loop and should see your "good bye" message. Just add

cout << "Good bye" << endl;

after do while loop but before return statement;

2