Jan 29, 2012 at 7:48am UTC
if (trueorfalse)
{
trueorfalse = false;
}
// not a clue what u are comparing here .... should be if (truefalse == ??)
Jan 29, 2012 at 7:50am UTC
Since trueorfalse is a Boolean variable if (trueorfalse) just checks is it is true. But that works fine. What dosent work is the while loop that starts on line 130. It never ends.
Jan 29, 2012 at 8:04am UTC
I dont have my compiler on my laptop to run your code...
it is still possible to endlessly loop inside commandpromt()
Does the displaynumbers() func keep displaying after you are done with command prompt ?
Jan 29, 2012 at 8:06am UTC
no, everying works fine except for that one little loop in main ().
Last edited on Jan 29, 2012 at 8:12am UTC
Jan 29, 2012 at 8:14am UTC
well just to make sure u can add a line after commandprompt() like
cout<< " broke";
if you dont see broke then u never even got past commandpromt(); is what i am saying
your while (yhealth > 0 || ehealth > 0)
you never do anything with yhleath anyway u can take that out for now...
while (eheatlh>0) {
}
Jan 29, 2012 at 8:18am UTC
the only reason i can see why you cant exit the loop is because ehealth never hits 0 for some reason ... you should check ehealth during the loop to make sure the math is actually happening .
after the line add.
ehealth = ehealth - damage;
cout<<ehealth; // so u can monitor the value to make sure the value is actually changing correctly.
Jan 29, 2012 at 8:22am UTC
Ok, for some reason it was the while loop line
while (yhealth > 0 || ehealth > 0)
When I took out yhealth the program worked perfectly. But! In order to finish this off I need yhealth in their. Lets say the computer kills the user- then the program must end. But if yhealth wasn't in there then nothing would happen and the program would keep on going.
Oh, and the revised code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
bool trueorfalse = true ;
//prototypes
void helplist ();
void displaynumber (int yhealth, int ehealth);
void commandprompt ();
//health/etc. variable declaration
int yhealth, ehealth, damage, heal;
//displays health
void displaynumbers (int yhealth, int ehealth)
{
cout << "\nYour Health " << yhealth << "\tEnemy Health " << ehealth <<"\n" << endl;
}
void commandprompt ()
{
char x;
bool input = false ;
while (!input)
{
cout << "Please enter a command: " ;
cin >> x;
switch (x)
{
case 'h' :
case 'H' :
helplist();;
break ;
case 'a' :
case 'A' :
damage = damage = rand() % 5 + 5;
cout << "\nYou punched the enemy for " << damage << " damage" << endl;
input = true ;
break ;
case 'b' :
case 'B' :
damage = damage = rand() % 13 + 2;
cout << "\nYou kicked the enemy for " << damage << " damage" << endl;
input = true ;
break ;
case 'c' :
case 'C' :
damage = damage = rand() % 30;
if (damage == 0)
{
cout << "\nYou missed" << endl;
}
else
{
cout << "\nYou slashed the enemy for " << damage << " damage" << endl;
}
input = true ;
break ;
case 'd' :
case 'D' :
damage = damage = rand() % 100 + -50;
if (damage > 0)
{
cout << "\nYou cast magic on the enemy for " << damage << " damage" << endl;
}
else if (damage == 0)
{
cout << "\nYou cast magic on the enemy but it did nothing" << endl;
}
else
{
cout << "\nYou cast magic but accidently healed the enemy for " << damage * -1 << " health" << endl;
}
input = true ;
break ;
case 'e' :
case 'E' :
heal = damage = rand() % 50 + -50;
if (heal > 0)
{
cout << "\nYou healed yourself for " << heal << " health" << endl;
}
else if (heal == 0)
{
cout << "\nThe potion did nothing" << endl;
}
else
{
cout << "\nThe potion did not work quite right and lowered your health by " << heal * -1 << endl;
}
input = true ;
break ;
default :
cout << "\nYou did not enter a valid command. Please try again. If you need help, type 'h' for a list of commands.\n" << endl;
}
}
}
//displays help
void helplist ()
{
cout << "\nPlease type in the letter of the command you wish to use\n" << endl;
cout << "---List of commands---" << endl;
cout << "Punch (a) - Punch the enemy for damage" << endl;
cout << "Kick (b) - Kick the enemy with a sword for damage" << endl;
cout << "Slash (c) - Slash the enemy for damage" << endl;
cout << "Magic (d) - Cast magic on the enemy causing either damage or health" << endl;
cout << "Potion(e) - Use a potion to heal yourself - it might have negetive effects\n" << endl;
}
int main()
{
srand((unsigned )time(0));
cout << "Welcome to the battle program! Type 'h' if you want a list of the possible commands. Remember to press the ENTER key after each command you type." << endl;
//creates health for each side
yhealth = 100;
ehealth = 100;
//starts loop
while (ehealth > 0)
{
displaynumbers (yhealth, ehealth);
commandprompt ();
ehealth = ehealth - damage;
}
if (yhealth < 1)
{
cout << "\nYou lose!" ;
}
else
{
cout << "\nYou win!" ;
}
return 0;
}
Last edited on Jan 29, 2012 at 8:23am UTC
Jan 29, 2012 at 8:30am UTC
you can try this ...
while ((yhealth > 0 || ehealth > 0))
{
}
Jan 29, 2012 at 8:35am UTC
Sadly this dosen't work. I tried while ((yhealth > 0) || (ehealth > 0)) too. Some how I don't think this problem is going to be fixed today.
Jan 29, 2012 at 8:39am UTC
um actually it is quite simple really
just add a if statement behind
ehealth = ehealth - damage;
if(yhealth == value u want it to exit here)
break;
Jan 29, 2012 at 8:41am UTC
or
if(yhealth< whatever)
break;
this will break the while loop. you are in
Last edited on Jan 29, 2012 at 8:42am UTC
Jan 29, 2012 at 8:44am UTC
Ok so I fixed the problem by puting this after ehealth = ehealth - damage;
if (ehealth < 1)
{
break;
}
else if (yhealth < 1)
{
break;
}
That seemed to work but I'm sure there is a better way to do this. Oh well, this will suffice for now. Thanks for the help.
Jan 29, 2012 at 9:03am UTC
you are welcome...
you do not need braces for the if statements if you will only execute one line of code if you meet the condition.
braces are only for 2 or more statements after the if.
if (ehealth<1 || yhealth<1)
break;
that is much more cleaner, and u can replace
while (ehealth > 0)
with
while(1) now that u are using if()break;