-1

my program is all finished but I want to know how to add a "Play again" feature that uses "Yes" or "No" input to decide whether to run the program again or to end it. I am a little unsure of where to place it in my program since I use a handful of while loops and if/else statements. Any insight would be appreciated, thanks! Here is my code:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main() // Setting up the code to follow.
{
    srand(static_cast<unsigned int>(time(0)));

    int Weapon_Selection;  // Creating variables for the player and computer's weapon selection.
    int Comp_Weapon;
    bool Battle = true;    // The variable to begin the battle.

    while(Battle)          // The main while loop to run the program code.
    {
        int Player_Health = 100; // More variables for both the player and the computer.
        int Comp_Health = 100;
        int Cannon_Ammo = 3;
        int Grenade_Ammo = 4;
        int Comp_Cannon_Ammo = 3;
        int Comp_Grenade_Ammo = 4;

        while(Player_Health > 0 && Comp_Health > 0) // The while loop to continue running the program while both combatants are alive.
        {
            bool Invalid = true;
            bool Comp_Invalid = true;


            while(Invalid)
            {
                cout << "Select a weapon using number keys 1, 2, and 3.\n"; // Weapon selection input from the player.
                cout << "1. Cannon (Ammo left: " << Cannon_Ammo << ")\n";
                cout << "2. Grenade (Ammo left: " << Grenade_Ammo << ")\n";
                cout << "3. Rifle\n";
                cout << "\n";
                cout << "You select: ";
                cin >> Weapon_Selection;

                srand(static_cast<unsigned int>(time(0)));

                if (Weapon_Selection == 1 && Cannon_Ammo > 0)
                {
                    Cannon_Ammo -= 1;
                    Invalid = false;
                    int Player_Dmg = rand() % 5 + 10; // Randomly generated damage range.
                    cout << "You caused " <<  Player_Dmg << " cannon damage to your enemy!\n";
                    Comp_Health -= Player_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                    break;
                }

                else if (Weapon_Selection == 2 && Grenade_Ammo > 0)
                {
                    Grenade_Ammo -= 1;
                    Invalid = false;
                    int Player_Dmg = rand() % 5 + 7;
                    cout << "You caused " << Player_Dmg << " grenade damage to your enemy!\n";
                    Comp_Health -= Player_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                    break;
                }

                else if (Weapon_Selection == 3)
                {
                    Invalid = false;
                    int Player_Dmg = rand() % 5 + 3;
                    cout << "You caused " << Player_Dmg << " rifle damage to your enemy!\n";
                    Comp_Health -= Player_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                    break;
                }

                else
                    cout << "You are out of ammo or you have entered an invalid number." << "\n"; // Output for wrong number input or no ammo.
                    cout << "\n";
            }


            if (Comp_Health <= 0) // Break point if the computer dies in battle.
                break;

            while(Comp_Invalid) // The computer's random weapon selection.
            {
                Comp_Weapon = rand() % 3 + 1;

                if (Comp_Weapon == 1 && Comp_Cannon_Ammo > 0)
                {
                    Comp_Cannon_Ammo -= 1;
                    Comp_Invalid = false;
                    int Comp_Dmg = rand() % 5 + 10;  // Randomly generated damage range for the computer.
                    cout << "Your enemy used a cannon and caused " << Comp_Dmg << " damage to you!\n";
                    Player_Health -= Comp_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                }

                else if (Comp_Weapon == 2 && Comp_Grenade_Ammo > 0)
                {
                    Comp_Grenade_Ammo -= 1;
                    Comp_Invalid = false;
                    int Comp_Dmg = rand() % 5 + 7;
                    cout << "Your enemy used a grenade and caused " << Comp_Dmg << " damage to you!\n";
                    Player_Health -= Comp_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                }

                else
                {
                    Comp_Invalid = false;
                    int Comp_Dmg = rand() % 5 + 3;
                    cout << "Your enemy used a rifle and caused " << Comp_Dmg << " damage to you!\n";
                    Player_Health -= Comp_Dmg;
                    cout << "Your health is " << Player_Health << "\n";
                    cout << "The computer's health is " << Comp_Health << "\n";
                    cout << "\n";
                }

            }
        }
        if(Player_Health < 0) // End of battle output, regardless of who wins or loses.
            cout << "You have been defeated by your enemy!" << "\n" << endl;

        else
            cout << "Congratulations, you beat your enemy!" << "\n" << endl;


    }
    cin.ignore(2);
    return 0;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Zjm4192
  • 103
  • 2
  • 9
  • 4
    Just add another loop. Better yet use functions and then in main use a loop that calls the game logic function and then all main needs to do is ask to play again. – NathanOliver Jul 20 '16 at 15:35
  • How would I go about doing that? I'm a novice, I know what the functions do but I never know where to place them correctly. I was able to do this through a lot of trial and error :) – Zjm4192 Jul 20 '16 at 15:38
  • 1
    Trail and error is a wonderful way to learn. However I suggest you get yourself a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as that will really help to learn the language. – NathanOliver Jul 20 '16 at 15:40

1 Answers1

0

To illustrate @NathanOliver's comment:

int main(void)
{
  bool play_again = true;
  while (play_again)
  {
    Play_Game();
    std::cout << "\nWant to play again (y / n)?";
    char answer;
    std::cin >> answer;
    if (tolower(answer) != 'y')
    {
        play_again = false;
    }
  }
  return 0;
}

You could also use a do-while loop as well.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • So that loop would be the first thing under my int main()? then all of my code above would fall within it? Also, what is the Play_Game variable for? And I am not familiar with what a "tolower" function is. – Zjm4192 Jul 20 '16 at 19:27
  • The `Play_Game` is a function, not a variable. It is the function that plays your game. Look up in any good C++ reference the [tolower function](http://en.cppreference.com/w/cpp/locale/ctype/tolower). – Thomas Matthews Jul 20 '16 at 22:27