←  Code Block

Fallout Studios Forums

»

My C++ RPG game

Warbz's Photo Warbz 18 Mar 2008

I've kinda jumped into the deep end here, and trying to make a text based RPG.
But, I'm getting a few errors and can't figure out why they would be occuring.
So far Ive coded the player to input his/her name.
Then for the player to attack an opponent or heal him/herself.
But I think Ive defined a few variables incorrectly.
I was hoping someone here might be able to check over it?
I'd appreciate it.
Even just guidance in how to correct them would be good.

//Author: Jaimie Warburton
//Date:  18 March 2008
//Title: RPG game

#include <windows.h>
#include <conio.h>
#include <iostream>

using namespace std;
//This is line 10--------------------------------------------------------------------
int main &#40;&#41;
{
// Define Variables	
  string name;
  int userchoice;
  int attack;
  int heal;
  int StartHealth = 100;
  int EnemyStartHealth = 100;
  int PlayerHealth;   //this is line 20
  int EnemyHealth;
  
//User inputs name.
	   cout << &#34;Hello, \n&#34;;
	   cout << &#34;What is your name? \n&#34;;
	   cin >> name;
	   cout << &#34;Hello &#34; << name; &#34;. \n&#34;;
	   Sleep&#40;3000&#41;;
	   system&#40;&#34;cls&#34;&#41;;
	   //this is line 30----------------------------------------------------------------
//Switch statement for user attack/heal choice.
				   cout << &#34;You are being attacked. \n&#34;;
				   cout << &#34;What would you like to do? \n&#34;;
				   cout << &#34;Press, \n&#34;;
				   cout << &#34;1. To attack. \n&#34;; // Attack opponent
				   cout << &#34;2. To heal. \n&#34;;   // Heal self
				   cin >> userchoice;

//Switch statement
					   switch&#40;userchoice&#41;	//line 40--------------------------------------------------------------
					   {
						case 1&#58;
							 attack = &#34;%40&#34;;
							 cout << &#34;You decided to attack. \n&#34;;
							 cout << &#34;You hit for &#34; << attack &#34;. \n&#34;;
							 EnemyHealth = EnemyStartHealth - attack;
							 cout << &#34;The enemy has &#34; << EnemyHealth &#34; remaining. \n&#34;;
							 break;
						
						case 2&#58;	//line 50----------------------------------------------------------
							 heal = &#34;%30&#34;;
							 cout << &#34;You decided to heal. \n&#34;;
							 cout << &#34;You have recovered &#34; << heal &#34; health. \n&#34;;
							 PlayerHealth = PlayerHealth + heal;
							 cout << &#34;You now have &#34; << PlayerHealth &#34; health.&#34;;
							 break;
					   }
							 
getch&#40;&#41;;
return 0;	 //line 60------------------------------------------------------------
}




and the errors:

In function `int main()':
line 43 invalid conversion from `const char*' to `int'
line 45 expected `;' before string constant
line 47 expected `;' before string constant
line 51 invalid conversion from `const char*' to `int'
line 53 expected `;' before string constant
line 55 expected `;' before string constant
Quote

UnderFlow's Photo UnderFlow 18 Mar 2008

I don't know much C++, but I can guess some things that might have gone wrong:

First of all you have to understand that Strings and Integers are completely different, even if they have the same values:
An integer 40 is a 40 in the RAM (0b00101000).
A String "40" in the RAM are the ASCII/Unicode-values for "4" and for "0" (0b00110100 and 0b00110000), both stored at different places.
You cannot simply write these two characters into one integer (especially not with the "%").

The other errors seem to be syntax errors, but I'm not sure about that since I'm a java-programmer.

Something like this would probably work:
attack = 40;
cout << "You decided to attack. \n";
cout << "You hit for " << attack << "%. \n";

Edited by UnderFlow, 18 March 2008 - 21:37.
Quote

Warbz's Photo Warbz 18 Mar 2008

The attack is meant to be a random value every time between 0 and 40.

I thing I need to do something like...

srand(unassigned, time( 0))

Using the ctime.h library, and using that to create the random value.
Quote

Warbz's Photo Warbz 18 Mar 2008

I did it!!

//Author&#58; Jaimie Warburton
//Date&#58;  18 March 2008
//Title&#58; RPG game

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <string>

using namespace std;

int main &#40;&#41;
{
// Define Variables	
  srand&#40;&#40;unsigned&#41;time&#40;0&#41;&#41;;

  string name;
  int StartHealth = 100;
  int EnemyStartHealth = 100;
  int PlayerHealth;
  int EnemyHealth;
  int attack = rand&#40;&#41;%40;
  int heal = rand&#40;&#41;%30;
  int userchoice;
  
//User inputs name.
	   cout << &#34;Hello, \n&#34;;
	   cout << &#34;What is your name? \n&#34;;
	   cin >> name;
	   cout << &#34;Hello &#34; << name; &#34;. \n&#34;;
	   Sleep&#40;3000&#41;;
	   system&#40;&#34;cls&#34;&#41;;
	   
//Switch statement for user attack/heal choice.
				   cout << &#34;You are being attacked. \n&#34;;
				   cout << &#34;What would you like to do? \n&#34;;
				   cout << &#34;Press, \n&#34;;
				   cout << &#34;1. To attack. \n&#34;; // Attack opponent
				   cout << &#34;2. To heal. \n&#34;;   // Heal self
				   cin >> userchoice;

//Switch statement
					   switch&#40;userchoice&#41;
					   {
						case 1&#58;
							 cout << &#34;You decided to attack. \n&#34;;
							 cout << &#34;You hit for &#34;;
							 cout << attack;
							 EnemyHealth = EnemyStartHealth - attack;
							 cout << &#34; the enemy has &#34;;
							 cout << EnemyHealth;
							 cout << &#34; health remaining. \n&#34;;
							 cout << endl;
							 break;
						
						case 2&#58;
							 
							 cout << &#34;You decided to heal. \n&#34;;
							 cout << &#34;You have recovered &#34;;
							 cout << heal;
							 cout << &#34; health. \n&#34;;
							 PlayerHealth = StartHealth + heal;
							 cout << &#34;You now have &#34;; 
							 cout << PlayerHealth;
							 cout << &#34; health.&#34;;
							 cout << endl;
							 break;
					   }
							 
getch&#40;&#41;;
return 0;
}


Now I just need to figure out how to make the computer decide whether to heal or atatck, and then loop the whole thing while carrying over current health values. :(
Quote

Dauth's Photo Dauth 18 Mar 2008

For the computer have it heal whenever under say 30% health (+ or - a random amount) and make a loop using if or whiel statements.
Quote

Warbz's Photo Warbz 19 Mar 2008

Easier said than done. lol
Quote

Dauth's Photo Dauth 19 Mar 2008

something like (and I'm only writing concept not full code)

Decision = rand()50%
Decision = 50 - decision
If CPUhealth > decision
Case 1
Else
Case 2

This means it will heal between at health 0 and 50 and you can use something similar to the base code.

Create a loop that keeps going through the code with the condition that If CPUhealth || playerhealth < 1 end.
Edited by Dauth, 19 March 2008 - 12:02.
Quote

UnderFlow's Photo UnderFlow 19 Mar 2008

What happens when the user enters "3" or "a"?

Quote

This means it will heal between at health 0 and 60 and you can use something similar to the base code.

Not 30-60?
Quote

Dauth's Photo Dauth 19 Mar 2008

To answer the first question he'll have to put in some sort of invalid respose line.

And well spotted on my coding, I'll go fix that now
Quote

Warbz's Photo Warbz 19 Mar 2008

'Tis finished.
Thanks all.

//Author&#58; Jaimie Warburton
//Date&#58;  18 March 2008
//Title&#58; RPG game

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <string>

using namespace std;

int main &#40;&#41;
{
//______________________________________________________________________________
// Define Variables	
  srand&#40;&#40;unsigned&#41;time&#40;0&#41;&#41;;//Use windows time to generate random integers

  string name; //Player name
  int StartHealth = 100; //Player&#39;s initial health
  int EnemyStartHealth = 100;//Enemies initial health
  int PlayerHealth=100;//Updated players health
  int EnemyHealth=100;//Updated enemy health
  int attack = rand&#40;&#41;%40;//Random attack value  
  int heal = rand&#40;&#41;%30;//Random heal value
  int userchoice=0;//variable for switch statement
  int EnemyChoice = rand&#40;&#41;%2;//Enemy choice for whether or not to attack
//______________________________________________________________________________
//User inputs name.
	   cout << &#34;Hello, \n&#34;;
	   cout << &#34;What is your name? \n&#34;;
	   cin >> name; //Input player name
	   cout << &#34;Hello &#34; << name; &#34;. \n&#34;;
	   getch&#40;&#41;;//Wait for input
	   system&#40;&#34;cls&#34;&#41;; // Clearscreen

//______________________________________________________________________________	   
//Switch statement for user attack/heal choice.
				   while &#40;PlayerHealth > 0 || EnemyHealth > 0&#41;
				   {
				   cout << &#34;You are being attacked. \n&#34;;
				   cout << &#34;What would you like to do? \n&#34;;
				   cout << &#34;Press, \n&#34;;
				   cout << &#34;1. To attack. \n&#34;; // Attack opponent
				   cout << &#34;2. To heal. \n&#34;;   // Heal self
				   cin >> userchoice;

//Switch statement
					   switch&#40;userchoice&#41;
					   {
						case 1&#58;
							 {
							 attack = rand&#40;&#41;%40;
							 cout << &#34;You decided to attack. \n&#34;;
							 cout << &#34;You hit for &#34;;
							 cout << attack; //Output attack value
							 EnemyHealth = EnemyHealth - attack; //calculate current health of enemy
							 cout << &#34; the enemy has &#34;;
							 cout << EnemyHealth; // Current enemy health
							 cout << &#34; health remaining. \n&#34;;
							 cout << endl;
							 getch&#40;&#41;;
//______________________________________________________________________________
//If statement check if enemy is dead.

							 if &#40;EnemyHealth < 1&#41;
							 {
							 cout << &#34; Congratulations, &#34; << name; 
							 cout << &#34;, you have defeated the enemy.&#34;;
							 getch&#40;&#41;;
							 return 0;
							 }
							 
							 EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
							 getch&#40;&#41;;//Get player input	
							 
//______________________________________________________________________________
										   if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
										   {
										   attack = rand&#40;&#41;%40;  //Reset random intger
										   cout << &#34;The enemy  has attacked you. \n&#34;;
										   PlayerHealth = PlayerHealth - attack;
										   cout << &#34;The enemy attacks for &#34;;
										   cout << attack; &#34;. \n&#34;;
										   
										   if &#40;PlayerHealth <=0&#41;
										   {
												  cout << endl;		  
												  cout<<&#34;The enemy has killed you.&#34;<<endl;
												  getch&#40;&#41;;
												  return 0;
										   }
										   
										   cout << &#34; You have &#34; << PlayerHealth;
										   cout << &#34; remaining. \n&#34;;   
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;
										   }
										   else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
										   {
										   heal = rand&#40;&#41;%30;//Reset heal integer
										   cout << &#34;The enemy has decided to heal. \n&#34;;
										   EnemyHealth = EnemyHealth + heal;
										   cout << &#34;The enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;   
										   
											 if &#40;EnemyHealth < 1&#41;
							 {
							 cout << &#34; Congratulations, &#34; << name; 
							 cout << &#34;, you have defeated the enemy. \n&#34;;
							 getch&#40;&#41;;
							 return 0;
							 }
							 
							 EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
							 getch&#40;&#41;;//Get player input	
							 
//______________________________________________________________________________
										   if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
										   {
										   attack = rand&#40;&#41;%40;  //Reset random intger
										   cout << &#34;The enemy  has attacked you. \n&#34;;
										   PlayerHealth = PlayerHealth - attack;
										   cout << &#34;The enemy attacks for &#34;;
										   cout << attack; &#34;. \n&#34;;
										   
										   if &#40;PlayerHealth <=0&#41;
										   {
												  cout<<endl;		  
												  cout<<&#34;The enemy has killed you. \n&#34;<<endl;
												  getch&#40;&#41;;
												  return 0;
										   }
										   
										   cout << &#34; You have &#34; << PlayerHealth;
										   cout << &#34; remaining. \n&#34;;   
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;
										   }
										   else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
										   {
										   heal = rand&#40;&#41;%30;//Reset heal integer
										   cout << &#34;The enemy has decided to heal. \n&#34;;
										   EnemyHealth = EnemyHealth + heal;
										   cout << &#34;the enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;   
										   }
										   }
										   }
										   
										   break;
						
						case 2&#58; 
							 if &#40;PlayerHealth > 99&#41;
							 {
							 cout << &#34; You already have full health. \n&#34;;
														  
							 EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
							 getch&#40;&#41;;//Get player input	
							 
//______________________________________________________________________________
										   if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
										   {
										   attack = rand&#40;&#41;%40;  //Reset random intger
										   cout << &#34;The enemy  has attacked you. \n&#34;;
										   PlayerHealth = StartHealth - attack;
										   cout << &#34;The enemy attacks for &#34;;
										   cout << attack; &#34;. \n&#34;;
										   
										   if &#40;PlayerHealth <=0&#41;
										   {
												  cout<<&#34;The enemy has killed you. \n&#34;<<endl;
												  getch&#40;&#41;;
												  return 0;
										   }
										   
										   cout << &#34; You have &#34; << PlayerHealth;
										   cout << &#34; remaining. \n&#34;;   
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;
										   }
										   else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
										   {
										   heal = rand&#40;&#41;%30;//Reset heal integer
										   cout << &#34;The enemy has decided to heal. \n&#34;;
										   if
												&#40;EnemyHealth > 99&#41;
										   {
												cout << &#34;The enemy already has full health \n&#34;;
										   }
										   else
										   {
										   EnemyHealth = EnemyHealth + heal;
										   cout << &#34;the enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;   
										   }
										   }
							 }
							 else
							 {
							 cout << &#34;You decided to heal. \n&#34;;
							 cout << &#34;You have recovered &#34;;
							 cout << heal; //Output amount of recovered health
							 cout << &#34; health. \n&#34;;
							 PlayerHealth = PlayerHealth + heal; //Calculate new player health
							 cout << &#34;You now have &#34;; 
							 cout << PlayerHealth; //Current health of player
							 cout << &#34; health. \n&#34;;
							 cout << endl;
							 
							 EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
							 getch&#40;&#41;;//Get player input	
							 
//______________________________________________________________________________
										   if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
										   {
										   attack = rand&#40;&#41;%40;  //Reset random intger
										   cout << &#34;The enemy  has attacked you. \n&#34;;
										   PlayerHealth = PlayerHealth - attack;
										   cout << &#34;The enemy attacks for &#34;;
										   cout << attack; &#34;. \n&#34;;
										   
										   if &#40;PlayerHealth <=0&#41;
										   {
												  cout<<&#34;The enemy has killed you. \n&#34;<<endl;
												  getch&#40;&#41;;
												  return 0;
										   }
										   
										   cout << &#34; You have &#34; << PlayerHealth;
										   cout << &#34; remaining. \n&#34;;   
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;
										   }
										   else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
										   {
										   heal = rand&#40;&#41;%30;//Reset heal integer
										   cout << &#34;The enemy has decided to heal. \n&#34;;
										   if &#40;EnemyHealth > 99&#41;
										   {
										   cout << &#34;The enemy already has full health. \n&#34;;
										   }
										   else
										   {
										   EnemyHealth = EnemyHealth + heal;
										   cout << &#34;The enemy gained &#34; << heal; &#34; health&#34;;
										   cout << &#34;The enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
										   getch&#40;&#41;;
										   system&#40;&#34;cls&#34;&#41;;  
										   } 
										   }
							 }
							 break;
						
						default&#58;
							 cout << &#34;Stop failing at life and do something properly for once. \n&#34;;
							 cout << &#34;Choose either option 1 or option 2! \n&#34;;
					   }
					   }
//End switch statement
//______________________________________________________________________________
							 
getch&#40;&#41;;
return 0;
}


Though I say it's finished, I will most likely continue to further develop it for shits n' giggles.
Edited by Seren, 19 March 2008 - 20:57.
Quote

CodeCat's Photo CodeCat 19 Mar 2008

A few notes. First of all, you would do well to split your code up into functions. It makes it easier to understand.

And a stylistic note: there is really no reason to indent your code that far. Only indent after:

- An opening brace {
- A line that continues a previous statement, if the statement was too long to put all on one line.
- An if, while or for, if you don't use a {. (i.e. there's only one statement)
- A case in a switch (but not the 'case' line itself)

And also, try not to write comments that are really obvious. Like:
cin >> name; //Input player name

That's just not necessary... it's pretty obvious to anyone that knows C++ what that line does.
Quote

UnderFlow's Photo UnderFlow 20 Mar 2008

I'd like to add that you should export some code into separate functions (e.g. you have the same enemy-attacks code several times in the main function, cut/paste it into a function and call the function every time the enemy has his turn).
If you have a short main function and all the code is in minor functions it will be a lot easier to read and understand.
Quote

CodeCat's Photo CodeCat 20 Mar 2008

I looked at your code a bit more, and noticed a little problem. You use
cin >> userchoice;

to let the user input a number. What if the user doesn't input a number but something else?

Here's the same code, except that it's now indented 'properly' (by accepted standards). Nothing else has been changed.
//Author&#58; Jaimie Warburton
//Date&#58;  18 March 2008
//Title&#58; RPG game

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <ctime>
#include <string>

using namespace std;

int main &#40;&#41;
{
	// Define Variables	
	srand&#40;&#40;unsigned&#41;time&#40;0&#41;&#41;;//Use windows time to generate random integers

	string name; //Player name
	int StartHealth = 100; //Player&#39;s initial health
	int EnemyStartHealth = 100;//Enemies initial health
	int PlayerHealth=100;//Updated players health
	int EnemyHealth=100;//Updated enemy health
	int attack = rand&#40;&#41;%40;//Random attack value  
	int heal = rand&#40;&#41;%30;//Random heal value
	int userchoice=0;//variable for switch statement
	int EnemyChoice = rand&#40;&#41;%2;//Enemy choice for whether or not to attack

	//User inputs name.
	cout << &#34;Hello, \n&#34;;
	cout << &#34;What is your name? \n&#34;;
	cin >> name; //Input player name
	cout << &#34;Hello &#34; << name; &#34;. \n&#34;;
	getch&#40;&#41;;//Wait for input
	system&#40;&#34;cls&#34;&#41;; // Clearscreen

	//Switch statement for user attack/heal choice.
	while &#40;PlayerHealth > 0 || EnemyHealth > 0&#41;
	{
		cout << &#34;You are being attacked. \n&#34;;
		cout << &#34;What would you like to do? \n&#34;;
		cout << &#34;Press, \n&#34;;
		cout << &#34;1. To attack. \n&#34;; // Attack opponent
		cout << &#34;2. To heal. \n&#34;;   // Heal self
		cin >> userchoice;

		//Switch statement
		switch&#40;userchoice&#41;
		{
			case 1&#58;
			{
				attack = rand&#40;&#41;%40;
				cout << &#34;You decided to attack. \n&#34;;
				cout << &#34;You hit for &#34;;
				cout << attack; //Output attack value
				EnemyHealth = EnemyHealth - attack; //calculate current health of enemy
				cout << &#34; the enemy has &#34;;
				cout << EnemyHealth; // Current enemy health
				cout << &#34; health remaining. \n&#34;;
				cout << endl;
				getch&#40;&#41;;

				//If statement check if enemy is dead.

				if &#40;EnemyHealth < 1&#41;
				{
					cout << &#34; Congratulations, &#34; << name; 
					cout << &#34;, you have defeated the enemy.&#34;;
					getch&#40;&#41;;
					return 0;
				}
				
				EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
				getch&#40;&#41;;//Get player input	
				
				if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
				{
					attack = rand&#40;&#41;%40;  //Reset random intger
					cout << &#34;The enemy  has attacked you. \n&#34;;
					PlayerHealth = PlayerHealth - attack;
					cout << &#34;The enemy attacks for &#34;;
					cout << attack; &#34;. \n&#34;;
					
					if &#40;PlayerHealth <=0&#41;
					{
						cout << endl;		  
						cout<<&#34;The enemy has killed you.&#34;<<endl;
						getch&#40;&#41;;
						return 0;
					}
					
					cout << &#34; You have &#34; << PlayerHealth;
					cout << &#34; remaining. \n&#34;;   
					getch&#40;&#41;;
					system&#40;&#34;cls&#34;&#41;;
				}
				else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
				{
					heal = rand&#40;&#41;%30;//Reset heal integer
					cout << &#34;The enemy has decided to heal. \n&#34;;
					EnemyHealth = EnemyHealth + heal;
					cout << &#34;The enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
					getch&#40;&#41;;
					system&#40;&#34;cls&#34;&#41;;   
					
					if &#40;EnemyHealth < 1&#41;
					{
						cout << &#34; Congratulations, &#34; << name; 
						cout << &#34;, you have defeated the enemy. \n&#34;;
						getch&#40;&#41;;
						return 0;
					}
					
					EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
					getch&#40;&#41;;//Get player input	
					
					if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
					{
						attack = rand&#40;&#41;%40;  //Reset random intger
						cout << &#34;The enemy  has attacked you. \n&#34;;
						PlayerHealth = PlayerHealth - attack;
						cout << &#34;The enemy attacks for &#34;;
						cout << attack; &#34;. \n&#34;;

						if &#40;PlayerHealth <=0&#41;
						{
							cout<<endl;		  
							cout<<&#34;The enemy has killed you. \n&#34;<<endl;
							getch&#40;&#41;;
							return 0;
						}
						
						cout << &#34; You have &#34; << PlayerHealth;
						cout << &#34; remaining. \n&#34;;   
						getch&#40;&#41;;
						system&#40;&#34;cls&#34;&#41;;
					}
					else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
					{
						heal = rand&#40;&#41;%30;//Reset heal integer
						cout << &#34;The enemy has decided to heal. \n&#34;;
						EnemyHealth = EnemyHealth + heal;
						cout << &#34;the enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
						getch&#40;&#41;;
						system&#40;&#34;cls&#34;&#41;;   
					}
				}
			}

			break;

			case 2&#58; 
				if &#40;PlayerHealth > 99&#41;
				{
					cout << &#34; You already have full health. \n&#34;;

					EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
					getch&#40;&#41;;//Get player input	

					if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
					{
						attack = rand&#40;&#41;%40;  //Reset random intger
						cout << &#34;The enemy  has attacked you. \n&#34;;
						PlayerHealth = StartHealth - attack;
						cout << &#34;The enemy attacks for &#34;;
						cout << attack; &#34;. \n&#34;;

						if &#40;PlayerHealth <=0&#41;
						{
							cout<<&#34;The enemy has killed you. \n&#34;<<endl;
							getch&#40;&#41;;
							return 0;
						}

						cout << &#34; You have &#34; << PlayerHealth;
						cout << &#34; remaining. \n&#34;;   
						getch&#40;&#41;;
						system&#40;&#34;cls&#34;&#41;;
					}
					else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
					{
						heal = rand&#40;&#41;%30;//Reset heal integer
						cout << &#34;The enemy has decided to heal. \n&#34;;
						if &#40;EnemyHealth > 99&#41;
						{
							cout << &#34;The enemy already has full health \n&#34;;
						}
						else
						{
							EnemyHealth = EnemyHealth + heal;
							cout << &#34;the enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
							getch&#40;&#41;;
							system&#40;&#34;cls&#34;&#41;;   
						}
					}
				}
				else
				{
					cout << &#34;You decided to heal. \n&#34;;
					cout << &#34;You have recovered &#34;;
					cout << heal; //Output amount of recovered health
					cout << &#34; health. \n&#34;;
					PlayerHealth = PlayerHealth + heal; //Calculate new player health
					cout << &#34;You now have &#34;; 
					cout << PlayerHealth; //Current health of player
					cout << &#34; health. \n&#34;;
					cout << endl;

					EnemyChoice = rand &#40;&#41;%2;//Generate enemy choice
					getch&#40;&#41;;//Get player input	

					if &#40;EnemyChoice == 0&#41;//If enemy chooses to attack
					{
						attack = rand&#40;&#41;%40;  //Reset random intger
						cout << &#34;The enemy  has attacked you. \n&#34;;
						PlayerHealth = PlayerHealth - attack;
						cout << &#34;The enemy attacks for &#34;;
						cout << attack; &#34;. \n&#34;;

						if &#40;PlayerHealth <=0&#41;
						{
							cout<<&#34;The enemy has killed you. \n&#34;<<endl;
							getch&#40;&#41;;
							return 0;
						}

						cout << &#34; You have &#34; << PlayerHealth;
						cout << &#34; remaining. \n&#34;;   
						getch&#40;&#41;;
						system&#40;&#34;cls&#34;&#41;;
					}
					else if &#40;EnemyChoice == 1&#41;//If enemy chooses to heal
					{
						heal = rand&#40;&#41;%30;//Reset heal integer
						cout << &#34;The enemy has decided to heal. \n&#34;;
						if &#40;EnemyHealth > 99&#41;
						{
							cout << &#34;The enemy already has full health. \n&#34;;
						}
						else
						{
							EnemyHealth = EnemyHealth + heal;
							cout << &#34;The enemy gained &#34; << heal; &#34; health&#34;;
							cout << &#34;The enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;
							getch&#40;&#41;;
							system&#40;&#34;cls&#34;&#41;;  
						} 
					}
				}
				break;

			default&#58;
				cout << &#34;Stop failing at life and do something properly for once. \n&#34;;
				cout << &#34;Choose either option 1 or option 2! \n&#34;;
		}
	}
	//End switch statement

	getch&#40;&#41;;
	return 0;
}
Quote

Warbz's Photo Warbz 20 Mar 2008

If the user inputs a value other than 1 or 2,
it outpts the default.
ie.
default:
cout << "Stop failing at life and do something properly for once. \n";
cout << "Choose either option 1 or option 2! \n";
Quote

CodeCat's Photo CodeCat 20 Mar 2008

No it won't. I just tried it, and when I typed in something that wasn't a number it just kept repeating the same thing over and over:

Quote

You are being attacked.
What would you like to do?
Press,
1. To attack.
2. To heal.
bla
Stop failing at life and do something properly for once.
Choose either option 1 or option 2!
You are being attacked.
What would you like to do?
Press,
1. To attack.
2. To heal.
Stop failing at life and do something properly for once.
Choose either option 1 or option 2!
You are being attacked.
What would you like to do?
Press,
1. To attack.
2. To heal.
etc.
etc.
etc. forever


What really happens is that any invalid input (such as a string when a number is expected) is left waiting in cin and cin is set into a 'fail' state. userchoice remains at its last known value (i.e. 1 or 2, or the default 0) and is not changed. When the next value is input from cin, it will simply fail again because cin is still in the fail state. So userchoice it will simply keep the same (invalid) value again without you inputting anything, and repeat over and over.

One way of handling it would be this:

		while &#40; !&#40;cin >> userchoice&#41; || &#40;userchoice != 1 && userchoice != 2&#41;&#41;
		{
			cin.clear&#40;&#41;;
			cin.ignore&#40;numeric_limits<streamsize>&#58;&#58;max&#40;&#41;, &#39;\n&#39;&#41;;
			
			cout << &#34;Stop failing at life and do something properly for once. \n&#34;;
			cout << &#34;Choose either option 1 or option 2! \n&#34;;
		}

What happens first is that cin tries to input userchoice. If that somehow fails, then !(cin >> userchoice) as a whole will return true, causing the while-loop to execute. The loop clears cin's fail state, then removes the invalid input (which would otherwise just stick around). Then it outputs an error, and gives you a chance to try again until you get it right.


Now secondly, after compiling, I got a few warnings from the compiler. Here's what I got:

In the line:
cout << &#34;Hello &#34; << name; &#34;. \n&#34;;

There's a ; before the rest of the string. So the part after that ; doesn't actually do anything. I think you meant to use << instead.

In the line:
cout << attack; &#34;. \n&#34;;

Same thing as above. This one actually occurs several times in the code.

In the line:
cout << &#34;The enemy now has &#34; <<  EnemyHealth; &#34; health remaining. \n&#34;;

Again, same thing. This one also occurs several times.

In the line:
cout << &#34;The enemy gained &#34; << heal; &#34; health&#34;;

Again, the same.

The compiler also warns that 'EnemyStartHealth' isn't used anywhere. Might want to look into it?
Quote

Warbz's Photo Warbz 20 Mar 2008

What compiler do you use?
I got DevC++, I used to use TurboC++ but its just out of date.
Quote

CodeCat's Photo CodeCat 20 Mar 2008

Dev-C++ isn't a compiler, it's an IDE (Integrated Development Environment). Dev-C++ however comes with the MinGW compiler, which is a port of the well-known GCC compiler to Windows.

MinGW happens to be what I use as well, except that I use the Code::Blocks IDE.
Quote

Warbz's Photo Warbz 20 Mar 2008

IDE, thats what I meant sorry.
Thanks for the help.

I'll try and figure out how to call the functions, rather than having the same code several times, like you suggested.

One other thing I meant to mention is that whenever the enemy heals, he gets another go straightaway.
Any idea whats causing that?
Quote

CodeCat's Photo CodeCat 20 Mar 2008

Because you copied the heal/attack code into the heal part. If an enemy decides to heal, then they get another choice again.

You might also want to consider writing a class called 'Entity' that is used for both the player and the enemy. Here's a concept:
class Entity
{
public&#58;
	Entity&#40;int initialHealth&#41;;
	~Entity&#40;&#41;;
	
	int getHealth&#40;&#41;;
	bool setHealth&#40;int newHealth&#41;;
	bool changeHealth&#40;int difference&#41;;
	
	void attack&#40;Entity& target&#41;;
	void heal&#40;&#41;;

private&#58;
	int m_health;
};

I haven't written the methods myself, but you can if you're up to it. :P

If this is all abracadabra to you, contact me on MSN or something and I'll help out.
Quote

Warbz's Photo Warbz 20 Mar 2008

Thanks, Ill add you in a mo.
Its just that I've pretty much jumped into the deep end on this, and people look at my code and think I know more than I actually do because of some of the functions ive used.
but ive pretty much just learnt whatever ive had to in order to progress, rather than learning the basics of the language via stufying it.
I prefer to learn by doing.
Quote