XCode C++ Program not running proper loops - c++

I am having issues with my program and am looking for some help. My program is supposed to randomly choose who gets to go first, the user or the computer. The program does that just fine, when I input the weapon I want to use it does the proper calculations and subtracts the right amount of randomly generated damage from the computer's health and vice versa if the computer gets to go first. Whether it is the user or the computer going first, after you press enter the program just stops. It is supposed to keep going until either the user or the computer's health reaches zero. I was wondering if I could get some assistance as to where my code is preventing this from happening. Any help would be appreciated, I am a novice with the C++ language. Thank you!
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main() // Setting the "stage" for the code to follow.
{
int Player_Health = 100; // Creating the necessary variables for the program.
int Comp_Health = 100;
int Turn;
int Weapon_Selection;
int Comp_Weapon;
int Cannon_Dmg;
int Grenade_Dmg;
int Rifle_Dmg;
int Player_Cannon_Ammo = 3;
int Player_Grenade_Ammo = 4;
int Comp_Cannon_Ammo = 3;
int Comp_Grenade_Ammo = 4;
srand(static_cast<unsigned int>(time(0)));
Turn = rand() % 2; // Randomizing who gets to go first.
while(Player_Health >= 0 && Comp_Health >= 0)
{
if(Turn == 0) // Player gets to go first.
{
cout << "You get to go first!\n";
cout << "Select a weapon using number keys 1, 2, or 3.\n";
cout << "1. Cannon\n";
cout << "2. Grenade\n";
cout << "3. Rifle\n";
cout << "\n";
cout << "You select: ";
cin >> Weapon_Selection;
while((Weapon_Selection < 1 || Weapon_Selection > 3) || (Weapon_Selection == 1 && Player_Cannon_Ammo == 0)
|| (Weapon_Selection == 2 && Player_Grenade_Ammo == 0))
{
cout << "Please enter a valid option.\n";
cout << "You select: ";
cin >> Weapon_Selection;
}
switch(Weapon_Selection)
{
case 1: // Player chooses to shoot the cannon.
Cannon_Dmg = 10 + rand() % 6;
Comp_Health = Comp_Health - Cannon_Dmg;
cout << "You caused " << Cannon_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout <<" The computer's health is " << Comp_Health << endl;
Player_Cannon_Ammo = Player_Cannon_Ammo - 1;
break;
case 2: // Player chooses to lob a grenade.
Grenade_Dmg = 7 + rand() % 6;
Comp_Health = Comp_Health - Grenade_Dmg;
cout << "You caused " << Grenade_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Player_Grenade_Ammo = Player_Grenade_Ammo - 1;
break;
case 3: // Player chooses to shoot the rifle.
Rifle_Dmg = 3 + rand() % 6;
Comp_Health = Comp_Health - Rifle_Dmg;
cout << "You caused " << Rifle_Dmg << " damage to your enemy.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
break;
}
}
else // Computer gets to go first.
{
Comp_Weapon = rand() % 3;
switch(Comp_Weapon) // Computer randomly selects a weapon.
{
case 1:
Cannon_Dmg = 10 + rand() % 6;
Player_Health = Player_Health - Cannon_Dmg;
cout << "Your enemy used a cannon and caused " << Cannon_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Comp_Cannon_Ammo = Comp_Cannon_Ammo - 1;
break;
case 2:
Grenade_Dmg = 7 + rand() % 6;
Player_Health = Player_Health - Grenade_Dmg;
cout << "Your enemy used a grenade and caused " << Grenade_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
Comp_Grenade_Ammo = Comp_Grenade_Ammo - 1;
break;
case 3:
Rifle_Dmg = 3 + rand() % 6;
Player_Health = Player_Health - Rifle_Dmg;
cout << "Your enemy used a rifle and caused " << Rifle_Dmg << " damage to you.\n";
cout << "Your health is " << Player_Health << endl;
cout << "The computer's health is " << Comp_Health << endl;
break;
}
}
if (Comp_Health < 0)
cout << "Congratulations, you beat your enemy!" << endl;
if (Player_Health < 0)
cout << "You have been defeated by your enemy!" << endl;;
cin.ignore(2);
return 0;
}
}

Looks like several issues.
How does Turn get changed from it initial value? Looks like whoever goes first will go until the other player is out of health
rand%3 will return 0,1 or 2. The switch is for 1,2,3. The 0 case is ignored.
The return statement is inside the while loop and the program hangs on the cin.ignore(2) line
I fixed these issue up and the program appears to be more or less working.
--Matt

Move the return statement outside of the while loop.
Using a debugger would catch this.

Related

How do I store 1 INT into another or have it stored somewhere so it updates every time the script finishes?

My script is below for a project I'm working on as I learn/practice.
I'd like to know how to keep track of the health. It seems, currently I'm just adding/subtracting/keeping it the same but the system has no no where to actually record the health and I'm not sure how to do it.
Also, what am I doing wrong as I tried to make the script repeat once it's finished but instead, it gives me a blank space instead of giving the Attack/Defend/Heal options again.
I tried the "do" + "while" conditions so DO this script WHILE the health is above 0 ---it works (kind of). The script doesn't end which is an improvement BUT it doesn't repeat the Attack/Defend/Heal menu.
#include <iostream>
using namespace std;
int main() {
int health = 50;
int Attack = 1;
int Defend = 2;
int Heal = 3;
do {
std::cout << "Please select an option: " << endl;
std::cout << "1. Attack" << endl;
std::cout << "2. Defend" << endl;
std::cout << "3. Heal" << endl;
int Move;
std::cin >> Move;
if (Move == 1) {
std::cout << "You did 1 damage" << endl;
std::cout << "You lost 1 health" << endl;
// while (health - 1)
std: cout << "Your current health is: " << health -1 << endl;
while (health > 0);
}
if (Move == 2) {
std::cout << "You took no damage" << endl;
std::cout << "You lost 0 health" << endl;
cout << "Your current health is: " << health << endl;
}
if (Move == 3) {
std::cout << "You gained 1 health" << endl;
cout << "Your current health is: " << health +1 << endl;
}
}
while (health > 0);
}
Remove the first while (health > 0);, that what's called an infinite loop and that is what is giving you the 'blank space'.
The second while (health > 0); is fine because that is the last part of the do .. while loop.
Secondly to store the new health you have to use the health variable. You must assign the new value to this variable, just like you assigned the initial value of 50. E.g.
// assign new health value
health = health - 1;
// display new health value
std::cout << "Your current health is: " << health << std::endl;

C++ Left Center Right Dice Game (Game Loop and connecting vectors/arrays to classes/header files)

So i have to code the dice game LCR for a final prject but am having some trouble. First off, I know the code is sloppy and really redundant, that's why im looking for help. I couldn't figure out how to connect the 'chips' int vector and 'name' array into the player.h file. I basically need help writing methods for the chip passing to make the code less redundant. But another problem of mine is having the game loop until just one person has chips. Thanks for any help or advice.
LeftCenterRight.cpp
#include <iostream>
#include <string>
#include <time.h>
#include "Dice.h"
#include "Player.h"
#include <vector>
#include <algorithm>
using namespace std;
void Player::gameRules()
{
cout << ("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
cout << ("Left Center Right is a multiplayer dice game");
cout << ("With a minimum of three players.");
cout << ("Dice containing letters L, C, R along\n"
"with dots on the remaining side are rolled each turn");
cout << ("\n-----Each Player starts with three chips-----\n");
cout << (">> For each L rolled, the player must pass one chip to the player to their left\n"
">> For each R rolled, the player must pass one chip to the player to their right\n"
">> For each C rolled, the player must pass a chip into the center pot (out of play)\n"
">> Dots are neutral and require no action");
cout << ("If a player has three or more chips, he/she rolls all three dice\n"
"If a player only has two chips, he/she rolles onlt two dice\n"
"If a player only has one chip, he/she rolls only one die\n"
"If a player is out of chips, he/she is still in the game,\n"
"\tbut does not roll any dice and passes their turn"
"\n\n >>> The last player with chips is the winner <<<");
cout << ("\n\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n");
};
int main()
{
int result = 0;
int currPlayer = 1;
srand((unsigned)time(NULL));
int numPlayers;
cout << "How many players are playing? (Please enter 3 or more): " << endl;
cin >> numPlayers;
if (numPlayers <= 2)
{
while (numPlayers <= 2)
{
cout << "More players needed.";
cout << "How many players are player?: ";
cin >> numPlayers;
}
}
std::string* names = new string[numPlayers]; // getting names and index is seat number
for (int i = 0; i < numPlayers; i++)
{
cout << "Please enter your name player " << i+1 << endl;
cin >> names[i];
std::string playerName = names[i];
}
vector<int>chips[1]; // intial 3 chips
for (int i = 0; i < numPlayers; i++)
{
int InitialChips = 3;
chips->push_back(InitialChips);
}
Player::gameRules();
int sum = 0;
for (int i = 0; i < chips->size(); i++)
{
int num = chips->at(i);
sum+=num;
}
int thePot = 0;
int i = 0;
while (sum > 0)
{
if ( i >=4 )
{
i = 0;
}
string currPlayer = names[i];
int currSeat = i;
cout << "It's " << currPlayer << "'s Turn!" << endl;
cout << "[1] Roll Dice [2] Quit :";
int choice;
cin >> choice;
if (choice == 1)
{
if (chips->at(currSeat) == 0)
{
break;
}
if (chips->at(currSeat) >= 3)
{
for (int k = 0; k <= 3; k++)
{
int outcome = Dice::rollDice();
if (outcome == 1)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == 0)
{
int j = (numPlayers - 1);
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i - 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 2)
{
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
if (i == chips->size())
{
int j = chips->at(0);
int currChips2 = chips->at(0);
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
else
{
int j = i + 1;
int currChips2 = (chips->at(j));
chips->at(j) = (currChips2 + 1);
cout << ">> +1 chip to " << names[j] << endl;
cout << ">> " << names[j] << " now has " << chips->at(j) << " chip(s)" << endl;
break;
}
}
if (outcome == 3)
{
thePot++;
cout << ">> +1 chip to the Center Pot" << endl;
cout << "There are now " << thePot << " chip(s) in the Center Pot " << endl;
int currChips = chips->at(i);
chips->at(i) = (currChips - 1);
cout << ">> -1 chip to " << names[i] << endl;
cout << ">> " << names[i] << " now has " << chips->at(i) << " chip(s)" << endl;
break;
}
else if ((outcome == 4) || (outcome == 5) || (outcome == 6))
{
break;
}
}
}
// ^^basically copied and pasted most of the while loop for the other two numbers of dice to roll^^
// had redundant code for if the player had 2 chips, to roll two dice only ^^
// also redundant code for only one chip, to roll one die. ^^
}
}
else if (choice == 2)
{
break;
}
else
{
cout << ">> Input Error";
cout << "[1] Roll Dice [2] Quit";
cin >> choice;
}
i++;
}
return 0;
}
Dice.h
#pragma once
using namespace std;
class Dice
{
public:
static int rollDice();
static int diceOutcome;
};
int Dice::rollDice()
{
int diceOutcome;
diceOutcome = (rand() % 6) + 1;
switch (diceOutcome)
{
default:
cout << "Error, retry";
case 1:
if (diceOutcome == 1)
{
cout << " --- " << endl;
cout << "You rolled a | L | Move 1 chip left." << endl;
cout << " --- " << endl;
return 1;
}
break;
case 2:
if (diceOutcome == 2)
{
cout << " --- " << endl;
cout << "You rolled a | R | Move 1 chip right." << endl;
cout << " --- " << endl;
return 2;
}
break;
case 3:
if (diceOutcome == 3)
{
cout << " --- " << endl;
cout << "You rolled a | C | Move 1 chip to the center." << endl;
cout << " --- " << endl;
return 3;
}
break;
case 4:
if (diceOutcome == 4)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 5:
if (diceOutcome == 5)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
case 6:
if (diceOutcome == 6)
cout << " --- " << endl;
cout << "You rolled a | * | No action needed." << endl;
cout << " --- " << endl;
return 0;
break;
}
}
To be fair, I'm quite new to programming, but I have been working on this project for days and kept running into conatiner problems and storing the name and chip values independently. So i've tried a lot of different things, probably not correctly though so I'm open to anything.

error C4700: uninitialized local variable 'enemyHealth' used

So I'm having some trouble figuring out why exactly I'm getting this error.
I've been programming for about a week now and made this VERY simple game.
I just can't figure out why it's spouting off this error.
Any help is appreciated.
#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <windows.h>
using namespace std;
void battleResults(int enemyHealth);
void battleLoop(int health, int enemyHealth, string selection);
void characterSelection(int health, int enemyHealth, string selection);
int main()
{
// MUH STRINGS
string selection;
string enemy;
// MUH INTS
int health;
int enemyHealth;
cout << "***ULTIMATE COMBAT SIMULATOR***\n";
cout << "***CODED BY OCELOT TOES 2017***\n";
Sleep(1000);
characterSelection(health, enemyHealth, selection);
battleLoop(health, enemyHealth, selection);
battleResults(enemyHealth);
system("PAUSE");
return 0;
}
void characterSelection(int health, int enemyHealth, string selection)
{
string enemy;
cout << "Please choose your race, human or monster!\n";
cin >> selection;
if ((selection == "human") || (selection == "Human"))
{
cout << "***HEALTH***\nHuman = 1000 Monster = 625\n";
cout << "You have higher health, but lower attack!\nYou also deal a
small amount of damage even when blocking.\n";
enemy = "monster";
selection = "human";
health = 1000;
enemyHealth = 625;
}
else if ((selection == "monster") || (selection == "Monster"))
{
cout << "***HEALTH***\nHuman = 1000 Monster = 625\n";
cout << "You have higher attack, but lower health!\n";
enemy = "human";
selection = "monster";
health = 625;
enemyHealth = 1000;
}
else
{
cout << "I'm too lazy to make the game figure out typing errors or just
turds, so now you can exit :)\n";
system("PAUSE");
}
Sleep(1000);
cout << "Okay, so you're a " << selection << ".\n";
Sleep(1000);
cout << "You're fighting a " << enemy << ".\n";
Sleep(1000);
cout << "Let's get this fight started!\n";
}
void battleLoop(int health, int enemyHealth, string selection)
{
// RNG BABY
static mt19937 randomGenerator(time(NULL));
uniform_int_distribution<int> humanAttack(100, 150);
uniform_int_distribution<int> monsterAttack(175, 275);
uniform_real_distribution<float> defenseMultiplier(0.25f, 0.50f);
string uiCombatStatus;
int aCombatStatus;
int damageDealt;
int damageReceived;
while ((health > 0) && (enemyHealth > 0))
{
cout << "What would you like to do?\nAttack or Defend: " << endl;
cin >> uiCombatStatus;
if ((uiCombatStatus == "attack") || (uiCombatStatus == "Attack"))
{
aCombatStatus = 1;
}
else if ((uiCombatStatus == "defend") || (uiCombatStatus == "Defend"))
{
aCombatStatus = 0;
}
if ((aCombatStatus == 1) && (selection == "human"))
{
damageDealt = humanAttack(randomGenerator);
damageReceived = monsterAttack(randomGenerator);
cout << "You chose to attack! You attack for: " << damageDealt <<
".\n";
cout << "The enemy hits you for: " << damageReceived << ".\n";
enemyHealth = enemyHealth - damageDealt;
health = health - damageReceived;
cout << "Enemy's health is at: " << enemyHealth << ".\n";
cout << "Your health is: " << health << ".\n";
}
else if ((aCombatStatus == 0) && (selection == "human"))
{
damageDealt = humanAttack(randomGenerator) - 70;
damageReceived = monsterAttack(randomGenerator) *
defenseMultiplier(randomGenerator);
cout << "You chose to defend! You dealt a small amout of damage: "
<< damageDealt << ".\n";
cout << "The enemy hits you for: " << damageReceived << ".\n";
enemyHealth = enemyHealth - damageDealt;
health = health - damageReceived;
cout << "Enemy's health is at: " << enemyHealth << ".\n";
cout << "Your health is: " << health << ".\n";
}
if ((aCombatStatus == 1) && (selection == "monster"))
{
damageDealt = monsterAttack(randomGenerator);
damageReceived = humanAttack(randomGenerator);
cout << "You chose to attack! You attack for: " << damageDealt <<
".\n";
cout << "The enemy hits you for: " << damageReceived << ".\n";
enemyHealth = enemyHealth - damageDealt;
health = health - damageReceived;
cout << "Enemy's health is at: " << enemyHealth << ".\n";
cout << "Your health is: " << health << ".\n";
}
else if ((aCombatStatus == 0) && (selection == "monster"))
{
damageReceived = humanAttack(randomGenerator) *
defenseMultiplier(randomGenerator);
cout << "You chose to defend!\n";
cout << "The enemy hits you for: " << damageReceived << ".\n";
health = health - damageReceived;
cout << "Enemy's health is at: " << enemyHealth << ".\n";
cout << "Your health is: " << health << ".\n";
}
}
}
void battleResults(int enemyHealth)
{
if (enemyHealth <= 0)
{
cout << "***YOU WIN!***" << endl;
}
else
{
cout << "***YOU LOSE!***" << endl;
}
}
In main, you created an uninitialised variable named enemyHealth, and passed it to a function.
Then, in that function, you gave the resulting copy a value. Then you returned from the function.
Back in main, you're using the old, uninitialised variable again.
Then, you pass it to another function, which attempts to read the value.
However, that value is indeterminate because, contrary to your belief, you never set it!
I think perhaps you intended to pass by reference instead, so it's just the one variable at all times.
This is actually a compiler warning that's been promoted to an error by some setting. But that's good!
By the way, indenting your code will make it so that we can read it without sacrificing life expectancy.

Xcode C++ How to add a "Play Again?" function to my program

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;
}
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.

C++ "Death Battle" game will not properly keep track of how many times a weapon is used

So I am new to C++, and I am currently working on a "Death Battle" game for an assignment, and I am stuck.
Basically, you as the player will get to use each weapon a certain number of times: Canon 3 times, Grenade 4 times. Rifle is infinite.
It works well when I use the canon 3 times, then works once when I choose a grenade after my canons are out. If I try to use a grenade again, it will give me the message stating I am out of canons. The same will happen on my 2nd rifle use.
I can't seem to find why this is, or how to correct it. I would greatly appreciate if someone could point me in the right direction with this!
Here is my code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
int PlayerHealth = 100,
CompHealth = 100,
Turn = 0,
WeaponChoice,
CompWeapon,
Cannon_Damage = 0,
Grenade_Damage = 0,
Rifle_Damage = 0;
int PlayerCanonUse = 3;
int PlayerGrenadeUse = 4;
int CompCanonUse = 3;
int CompGrenadeUse = 4;
srand(static_cast<int>(time(0)));
char yn;
do
{
do
{
if(Turn == 0)// Player Turn
{
cout << "\nWhich weapon will you use? Choose 1, 2, or 3.\n";
cout << "1. Cannon\n";
cout << "2. Grenade\n";
cout << "3. Rifle\n\n";
cin >> WeaponChoice;
//Validate weapon choice
if ((WeaponChoice < 1 || WeaponChoice > 3)){
cout << "\nSilly you. That's not one of the options. Try again.\n";
cin >> WeaponChoice;
}
if (PlayerCanonUse <= 0){
cout << "Sorry, you have used all of your canons. Choose a different weapon.\n";
cin >> WeaponChoice;
}
if (PlayerGrenadeUse <= 0){
cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n";
cin >> WeaponChoice;
}
switch(WeaponChoice)
{
case 1: // player chooses to attack with cannon
Cannon_Damage = (10 + rand() % 6);
cout << "\nYou chose a cannon." << endl;
CompHealth = CompHealth - Cannon_Damage;
cout << setw(10) << Cannon_Damage << " damage!" << endl;
cout << setw(10) << "YOUR HEALTH: " << PlayerHealth << endl;
cout << setw(10) << "OPPONENT HEALTH: " << CompHealth << endl;
PlayerCanonUse = PlayerCanonUse - 1;
break;
case 2:
Grenade_Damage = (7 + rand() % 13);
cout << "\nYou chose a grenade." << endl;
CompHealth = CompHealth - Grenade_Damage;
cout << Grenade_Damage << " damage!" << endl;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
PlayerGrenadeUse = PlayerGrenadeUse - 1;
break;
case 3:
Rifle_Damage = (3 + rand() % 9);
cout << "\nYou chose a Rifle." << endl;
CompHealth = CompHealth - Rifle_Damage;
cout << Rifle_Damage << " damage!" << endl;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
break;
}
}
Turn == 1; // Computer Turn
CompWeapon = rand() % 3;
switch(CompWeapon)
{
case 1:
Cannon_Damage = (10 + rand() % 6);
cout<<"\nYour opponent used a cannon, and you lost " << Cannon_Damage << " HP." << endl;
PlayerHealth = PlayerHealth - Cannon_Damage;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
CompCanonUse = PlayerCanonUse - 1;
break;
case 2:
Grenade_Damage = (7 + rand() % 6);
cout<<"\nYour opponent used a grenade, and you lost " << Grenade_Damage << " HP." << endl;
PlayerHealth = PlayerHealth - Grenade_Damage;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
CompGrenadeUse = CompGrenadeUse - 1;
break;
case 3:
Rifle_Damage = (3 + rand() % 10);
cout<<"\nYour opponent used a rifle, and you lost " << Rifle_Damage << " HP." << endl;
PlayerHealth = PlayerHealth - Rifle_Damage;
cout << "YOUR HEALTH: " << PlayerHealth << endl;
cout << "OPPONENT HEALTH: " << CompHealth << endl;
break;
}
}
while(PlayerHealth >= 0 && CompHealth >= 0); //loops while both players are alive
if (CompHealth < 0)
cout << "Congratulations! You won!" << endl;
if (PlayerHealth < 0)
cout << "Darn, you lost. Game over." << endl;;
std::cout << "That was fun. Would you like to play again? Enter Y or N:" << std::flush;
} while(std::cin >> yn && (yn == 'Y' || yn == 'y'));
}
Also, please excuse me if I am not following c++ conventions, I'm still new to it! Any pointers on that would be nice, as well.
The problem lies here.
if (PlayerCanonUse <= 0){
cout << "Sorry, you have used all of your canons. Choose a different weapon.\n";
cin >> WeaponChoice;
}
if (PlayerGrenadeUse <= 0){
cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n";
cin >> WeaponChoice;
}
These two if statements are checked even if you didn't choose the weapon.
This can be fixed with something like
if (WeaponChoice == 1 && PlayerCanonUse <= 0){
cout << "Sorry, you have used all of your canons. Choose a different weapon.\n";
cin >> WeaponChoice;
}
if (WeaponChoice == 2 && PlayerGrenadeUse <= 0){
cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n";
cin >> WeaponChoice;
}
It would be simpler to do this check inside the switch.