My C++ RPG game
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.
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
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 () { // 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 << "Hello, \n"; cout << "What is your name? \n"; cin >> name; cout << "Hello " << name; ". \n"; Sleep(3000); system("cls"); //this is line 30---------------------------------------------------------------- //Switch statement for user attack/heal choice. cout << "You are being attacked. \n"; cout << "What would you like to do? \n"; cout << "Press, \n"; cout << "1. To attack. \n"; // Attack opponent cout << "2. To heal. \n"; // Heal self cin >> userchoice; //Switch statement switch(userchoice) //line 40-------------------------------------------------------------- { case 1: attack = "%40"; cout << "You decided to attack. \n"; cout << "You hit for " << attack ". \n"; EnemyHealth = EnemyStartHealth - attack; cout << "The enemy has " << EnemyHealth " remaining. \n"; break; case 2: //line 50---------------------------------------------------------- heal = "%30"; cout << "You decided to heal. \n"; cout << "You have recovered " << heal " health. \n"; PlayerHealth = PlayerHealth + heal; cout << "You now have " << PlayerHealth " health."; break; } getch(); 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
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:
Edited by UnderFlow, 18 March 2008 - 21:37.
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.
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.
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.
Warbz 18 Mar 2008
I did it!!
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. :(
//Author: Jaimie Warburton //Date: 18 March 2008 //Title: RPG game #include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <string> using namespace std; int main () { // Define Variables srand((unsigned)time(0)); string name; int StartHealth = 100; int EnemyStartHealth = 100; int PlayerHealth; int EnemyHealth; int attack = rand()%40; int heal = rand()%30; int userchoice; //User inputs name. cout << "Hello, \n"; cout << "What is your name? \n"; cin >> name; cout << "Hello " << name; ". \n"; Sleep(3000); system("cls"); //Switch statement for user attack/heal choice. cout << "You are being attacked. \n"; cout << "What would you like to do? \n"; cout << "Press, \n"; cout << "1. To attack. \n"; // Attack opponent cout << "2. To heal. \n"; // Heal self cin >> userchoice; //Switch statement switch(userchoice) { case 1: cout << "You decided to attack. \n"; cout << "You hit for "; cout << attack; EnemyHealth = EnemyStartHealth - attack; cout << " the enemy has "; cout << EnemyHealth; cout << " health remaining. \n"; cout << endl; break; case 2: cout << "You decided to heal. \n"; cout << "You have recovered "; cout << heal; cout << " health. \n"; PlayerHealth = StartHealth + heal; cout << "You now have "; cout << PlayerHealth; cout << " health."; cout << endl; break; } getch(); 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. :(
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.
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.
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.
UnderFlow 19 Mar 2008
What happens when the user enters "3" or "a"?
Not 30-60?
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?
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
And well spotted on my coding, I'll go fix that now
Warbz 19 Mar 2008
'Tis finished.
Thanks all.
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.
Thanks all.
//Author: Jaimie Warburton //Date: 18 March 2008 //Title: RPG game #include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <string> using namespace std; int main () { //______________________________________________________________________________ // Define Variables srand((unsigned)time(0));//Use windows time to generate random integers string name; //Player name int StartHealth = 100; //Player'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;//Random attack value int heal = rand()%30;//Random heal value int userchoice=0;//variable for switch statement int EnemyChoice = rand()%2;//Enemy choice for whether or not to attack //______________________________________________________________________________ //User inputs name. cout << "Hello, \n"; cout << "What is your name? \n"; cin >> name; //Input player name cout << "Hello " << name; ". \n"; getch();//Wait for input system("cls"); // Clearscreen //______________________________________________________________________________ //Switch statement for user attack/heal choice. while (PlayerHealth > 0 || EnemyHealth > 0) { cout << "You are being attacked. \n"; cout << "What would you like to do? \n"; cout << "Press, \n"; cout << "1. To attack. \n"; // Attack opponent cout << "2. To heal. \n"; // Heal self cin >> userchoice; //Switch statement switch(userchoice) { case 1: { attack = rand()%40; cout << "You decided to attack. \n"; cout << "You hit for "; cout << attack; //Output attack value EnemyHealth = EnemyHealth - attack; //calculate current health of enemy cout << " the enemy has "; cout << EnemyHealth; // Current enemy health cout << " health remaining. \n"; cout << endl; getch(); //______________________________________________________________________________ //If statement check if enemy is dead. if (EnemyHealth < 1) { cout << " Congratulations, " << name; cout << ", you have defeated the enemy."; getch(); return 0; } EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input //______________________________________________________________________________ if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = PlayerHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout << endl; cout<<"The enemy has killed you."<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; EnemyHealth = EnemyHealth + heal; cout << "The enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); if (EnemyHealth < 1) { cout << " Congratulations, " << name; cout << ", you have defeated the enemy. \n"; getch(); return 0; } EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input //______________________________________________________________________________ if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = PlayerHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout<<endl; cout<<"The enemy has killed you. \n"<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; EnemyHealth = EnemyHealth + heal; cout << "the enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); } } } break; case 2: if (PlayerHealth > 99) { cout << " You already have full health. \n"; EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input //______________________________________________________________________________ if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = StartHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout<<"The enemy has killed you. \n"<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; if (EnemyHealth > 99) { cout << "The enemy already has full health \n"; } else { EnemyHealth = EnemyHealth + heal; cout << "the enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); } } } else { cout << "You decided to heal. \n"; cout << "You have recovered "; cout << heal; //Output amount of recovered health cout << " health. \n"; PlayerHealth = PlayerHealth + heal; //Calculate new player health cout << "You now have "; cout << PlayerHealth; //Current health of player cout << " health. \n"; cout << endl; EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input //______________________________________________________________________________ if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = PlayerHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout<<"The enemy has killed you. \n"<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; if (EnemyHealth > 99) { cout << "The enemy already has full health. \n"; } else { EnemyHealth = EnemyHealth + heal; cout << "The enemy gained " << heal; " health"; cout << "The enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); } } } break; default: cout << "Stop failing at life and do something properly for once. \n"; cout << "Choose either option 1 or option 2! \n"; } } //End switch statement //______________________________________________________________________________ getch(); 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.
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:
That's just not necessary... it's pretty obvious to anyone that knows C++ what that line does.
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.
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.
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.
CodeCat 20 Mar 2008
I looked at your code a bit more, and noticed a little problem. You use
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.
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: Jaimie Warburton //Date: 18 March 2008 //Title: RPG game #include <iostream> #include <conio.h> #include <windows.h> #include <ctime> #include <string> using namespace std; int main () { // Define Variables srand((unsigned)time(0));//Use windows time to generate random integers string name; //Player name int StartHealth = 100; //Player'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;//Random attack value int heal = rand()%30;//Random heal value int userchoice=0;//variable for switch statement int EnemyChoice = rand()%2;//Enemy choice for whether or not to attack //User inputs name. cout << "Hello, \n"; cout << "What is your name? \n"; cin >> name; //Input player name cout << "Hello " << name; ". \n"; getch();//Wait for input system("cls"); // Clearscreen //Switch statement for user attack/heal choice. while (PlayerHealth > 0 || EnemyHealth > 0) { cout << "You are being attacked. \n"; cout << "What would you like to do? \n"; cout << "Press, \n"; cout << "1. To attack. \n"; // Attack opponent cout << "2. To heal. \n"; // Heal self cin >> userchoice; //Switch statement switch(userchoice) { case 1: { attack = rand()%40; cout << "You decided to attack. \n"; cout << "You hit for "; cout << attack; //Output attack value EnemyHealth = EnemyHealth - attack; //calculate current health of enemy cout << " the enemy has "; cout << EnemyHealth; // Current enemy health cout << " health remaining. \n"; cout << endl; getch(); //If statement check if enemy is dead. if (EnemyHealth < 1) { cout << " Congratulations, " << name; cout << ", you have defeated the enemy."; getch(); return 0; } EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = PlayerHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout << endl; cout<<"The enemy has killed you."<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; EnemyHealth = EnemyHealth + heal; cout << "The enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); if (EnemyHealth < 1) { cout << " Congratulations, " << name; cout << ", you have defeated the enemy. \n"; getch(); return 0; } EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = PlayerHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout<<endl; cout<<"The enemy has killed you. \n"<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; EnemyHealth = EnemyHealth + heal; cout << "the enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); } } } break; case 2: if (PlayerHealth > 99) { cout << " You already have full health. \n"; EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = StartHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout<<"The enemy has killed you. \n"<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; if (EnemyHealth > 99) { cout << "The enemy already has full health \n"; } else { EnemyHealth = EnemyHealth + heal; cout << "the enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); } } } else { cout << "You decided to heal. \n"; cout << "You have recovered "; cout << heal; //Output amount of recovered health cout << " health. \n"; PlayerHealth = PlayerHealth + heal; //Calculate new player health cout << "You now have "; cout << PlayerHealth; //Current health of player cout << " health. \n"; cout << endl; EnemyChoice = rand ()%2;//Generate enemy choice getch();//Get player input if (EnemyChoice == 0)//If enemy chooses to attack { attack = rand()%40; //Reset random intger cout << "The enemy has attacked you. \n"; PlayerHealth = PlayerHealth - attack; cout << "The enemy attacks for "; cout << attack; ". \n"; if (PlayerHealth <=0) { cout<<"The enemy has killed you. \n"<<endl; getch(); return 0; } cout << " You have " << PlayerHealth; cout << " remaining. \n"; getch(); system("cls"); } else if (EnemyChoice == 1)//If enemy chooses to heal { heal = rand()%30;//Reset heal integer cout << "The enemy has decided to heal. \n"; if (EnemyHealth > 99) { cout << "The enemy already has full health. \n"; } else { EnemyHealth = EnemyHealth + heal; cout << "The enemy gained " << heal; " health"; cout << "The enemy now has " << EnemyHealth; " health remaining. \n"; getch(); system("cls"); } } } break; default: cout << "Stop failing at life and do something properly for once. \n"; cout << "Choose either option 1 or option 2! \n"; } } //End switch statement getch(); return 0; }
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";
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";
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:
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:
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:
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:
Same thing as above. This one actually occurs several times in the code.
In the line:
Again, same thing. This one also occurs several times.
In the line:
Again, the same.
The compiler also warns that 'EnemyStartHealth' isn't used anywhere. Might want to look into it?
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 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 ( !(cin >> userchoice) || (userchoice != 1 && userchoice != 2)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Stop failing at life and do something properly for once. \n"; cout << "Choose either option 1 or option 2! \n"; }
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 << "Hello " << name; ". \n";
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; ". \n";
Same thing as above. This one actually occurs several times in the code.
In the line:
cout << "The enemy now has " << EnemyHealth; " health remaining. \n";
Again, same thing. This one also occurs several times.
In the line:
cout << "The enemy gained " << heal; " health";
Again, the same.
The compiler also warns that 'EnemyStartHealth' isn't used anywhere. Might want to look into it?
Warbz 20 Mar 2008
What compiler do you use?
I got DevC++, I used to use TurboC++ but its just out of date.
I got DevC++, I used to use TurboC++ but its just out of date.
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.
MinGW happens to be what I use as well, except that I use the Code::Blocks IDE.
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?
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?
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:
I haven't written the methods myself, but you can if you're up to it.
If this is all abracadabra to you, contact me on MSN or something and I'll help out.
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: Entity(int initialHealth); ~Entity(); int getHealth(); bool setHealth(int newHealth); bool changeHealth(int difference); void attack(Entity& target); void heal(); private: int m_health; };
I haven't written the methods myself, but you can if you're up to it.
If this is all abracadabra to you, contact me on MSN or something and I'll help out.
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.
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.