Build a Text Adventure C++ Output something when i == 0?

HI guys! I am trying to make a program that gives the user 5 tries to enter a code or the enitre universe will implode… hihi… Im doing this with a for loop. The problem I have tho is that I cant for my life find a way to tell the user that the universe has imploded when he or she reaches the end of their “tries”…

What i’ve been able to do so far is for the loop to count down the tries from 5 - 0 decrementing by 1 every time. In example. you have 4 more tries… you have 3 more tries… and so on, and I have also managed to give the right output when the user enters the right code. But how do I make the terminal print "the universe died when i == 0 ? i’ll paste my code down below :slight_smile:

 for (int i = 5; i > 0; i--) {
    std::cout << " Please enter the code.\n";
    std::cin >> code;
    if (code != 1453000173) {
      std::cout << "you got " << i - 1 << " tries left.\n";
    } else if (code == 1453000173) {
      std::cout << "Congratulations, you just saved the entire univerese,\n press: 1 to speak to the creater of the univerese\n";

There’s a couple of ways to do this.

One simple way would be to extend the range of your loop by 1 and have a base case that breaks out of the loop before any of the other code block is executed.

1 Like

thanks for pointing me in the right direction :slight_smile: however, now my program wont let the user put the right code in and still dies even if the sequense of numbers is right when there is one try left :slight_smile: but i think i will solve that in time. Thanks :slight_smile: