Can't figure out how to write a loop that asks for user's name 3 times

Hello! I just started learning c++ and reached the lopps challenge project. I wanted to write a small Pokemon game. The idea is for the Prof to ask for your name. And like in any Pokemon game, you would have to confirm that that really is your name. You would have 3 tries. It just doesn’t work. If I press ‘Y’ the question still pops up. And if I press ‘N’ the confirmation statement pops up…I don’t know what I’m doing wrong. Anyway, here’s the code:

#include

using namespace std;

int main() {

cout << "                                               ,'\\ \n";
cout << "              _.----.         ____           ,'  _\\   ___    ___      ____ \n";
cout << "          _,-'        `.     |    |  /`.     \\,-'    |   \\  /   |    |     \\   |`.\n";
cout << "          \\        __    \\   '-.  | /   `.   ___     |    \\/    |    '-.    \\  |   |\n";
cout << "           \\.     \\  \\   |  __  |  |/    ,',' _  `.  |          |  __   |     \\|   |\n";
cout << "              \\    \\/    /,'  _ `.     ,' /  / / /   |           ,'  _ `.      |   |\n";
cout << "               \\      ,-'/  /  \\ |   ,'   |  \\/ /, `.|          /  /  \\  |  |      |\n";
cout << "                \\     \\  |  \\_/   |   `-. \\     `'  /|  |    |  |  \\_/   |  |\\     |\n";
cout << "                 \\     \\  \\      /       `-.`.___,-' |  |\\ / |   \\      /   | |    |\n";
cout << "                  \\     \\  `.__,'   |`-.,    `|      |__|    |  | `.__,'|   | |    |\n";
cout << "                   \\_..-'       |__ |    `-., |              '-.|       '-. |  |   |\n";
cout << "                                             `'                               '-._ |\n";
cout << "==========================================  TEXT VERSION  ===========================================\n\n";

// ---------------------------------------------- WELCOME MESSAGE ------------------------------------------------

cout << "Hello there! Welcome to the world of Pokemon.\n";
cout << "My name is Derp. People call me the Professor Derp.\n";
cout << "Choose a Pokemon, go on an adventure, you know the drill...\n";

string name;
int tries = 0;
char confirm;

do {
cout << “What is your name?\n\n”;
getline(cin, name);

cout << "Champ " << name << " is that right? (Y/N)\n";
cin >> confirm;

if (confirm == 'y' || confirm == 'Y') {

    cout << "No way! You are the great " << name << " everyone's been talking about\n";

} else {
    tries++;
    cout << "Hmm...Alright. What is your name?\n";
}

} while (tries <= 3);

if (tries > 3) {

    cout << "You better get some rest and come back later\n";
}

Any help will be greatly appreciated!

You could put a break; to exit the loop in the if-block for confirm == 'y'
or you could set tries to be something 3 or larger there so that the loop doesn’t run again.

1 Like

Ty! Will do that later when I get home.