text battle project

Hi all,

I am very new to C++ and have been spinning my wheels on a project. I am stuck at a few points.

1. I have to give the User a turn and the PC a turn, I am not really certain how to write a count statement to do this as either player can go unlimited times until their health = zero. I think I would put it in an if statement such as
int userturn = 0
if(userturn > 0, userturn<20, userturn++)
but I'm not sure where to put it to do the most good or even if I should.

2. The weapons the computer used on it's turn should display and it is only displaying the choice number

3. Not sure if random numbers are working because they are not displaying.

4. Userhealth and PChealth totals are not coming out right.

I've been staring at this for 8 hours and trying to go through my book. Can someone tell me what I'm doing wrong and where?


#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;


int main()
{
//Hold menu choice
int choice;
int pcchoice;
int damagedone = 0;
int Damageimpossed = 0;


//Constants for Health
int PChealth = 100,
Userhealth = 100;



//Constants for Weapon rounds
int Canon = 3,
Grenade = 4,
RifleR = 0;


//Constants for Menu Choices
const int Canon_CHOICE = 1,
Grenade_CHOICE = 2,
Rifle_CHOICE = 3;


//Declare varibale for damage via weapons
int CanonDamage,
GrenadeDamage,
RifleDamage,
RandomPCDamage;

//Randomization and top out at 100
srand ((unsigned)time(0));
CanonDamage = (rand() % 15) + 10; // OR CanonDamage -= (rand()%11)+5;
GrenadeDamage = (rand() % 12) + 7;
RifleDamage = (rand() % 8) + 3;
RandomPCDamage =(rand() & 15) +3;


//Welcome message
cout<<"Welcome to the Text Battle Game\n";
cout<<"Please choose your Weapon"<<endl;

//Loop for game
while ((PChealth >= 0) || (PChealth >= 0))
{


//Menu and Choice
cout<<"1. Canon: " <<Canon << " left\n"
<<"2. Grenade: " <<Grenade << " left\n"
<<"3. Rifle: UNLIMITED\n";
cin>>choice;
cout<<endl;


//PC Menu and Choice
//pcchoice = 0;
pcchoice = (rand() % 3) + 1;

cout<<fixed<<showpoint<<setprecision(2);

//if(pcchoice == 0)
//{
if((pcchoice == 1)&&(Canon != 0))
{
Canon = Canon - 1;
cout<<"Computer used "<<Canon<<endl<<endl;
}
else if((pcchoice == 2)&&(Grenade != 0))
{
Grenade = Grenade - 1;
cout<<"Computer used "<<Grenade<<endl<<endl;
}
else if(pcchoice == 3)
{
cout<<"Computer used "<<RifleR<<endl<<endl;
}
//}
//else break;



if(Userhealth > 0)
{
if ((choice == 1)&&(Canon != 0))
{
Canon = Canon - 1;
damagedone = PChealth - CanonDamage;
}
else if ((choice == 1)&&(Canon == 0))
{
cout<<"You have no Canon Balls left, pick another weapon"<<endl<<endl;
}
else if ((choice == 2)&&(Grenade != 0))
{
Grenade = Grenade - 1;
damagedone = PChealth - GrenadeDamage;
}
else if ((choice == 2)&&(Grenade == 0))
{
cout<<"You have no Grenades left, pick another weapon"<<endl;
}
else if (choice == 3)
{
damagedone = PChealth - RifleDamage;
}

}
else break;

// }
// else if(Userhealth > 0)
// {
//Computer's Turn
// Damageimpossed = Userhealth - RandomPCDamage;
// }
// else break;




//Userhealth = Userhealth -= Damageimpossed;
cout<<"Your Health is: "<<Userhealth<<"\n";
PChealth = PChealth -= damagedone;
cout<<"PC Health is: "<<damagedone<<"\n"<<endl;




}
cout<<fixed<<showpoint<<setprecision(2);
if (PChealth == 0)
cout<<"You Win!!!";
else if (Userhealth == 0)
cout<<"You lose :(";


system("pause");

cout<<"Press Enter to exit"<<endl;


return 0;
}

When posting code, highlight it all and press the '<>' button under the format options. This will put it in an easier to read format

Firstly, you're not declaring variables correctly

For instance;

1
2
3
4
int CanonDamage,
GrenadeDamage,
RifleDamage,
RandomPCDamage;


Should be:

1
2
3
4
int CanonDamage,
int GrenadeDamage,
int RifleDamage,
int RandomPCDamage;


Also:
while ((PChealth >= 0) || (PChealth >= 0))

Should probably be:
while ((PChealth >= 0) || (Userhealth >= 0))


Haven't looked at the rest of it, it is kind of difficult to read. Try this and if it doesn't work give me a message back.

For the unlimited turns, do this;

1
2
3
4
5
6
while(true){
//get computer and user turns

if(Userhealth ==0 || PChealth==0)
break;
}


Hope this helps
Topic archived. No new replies allowed.