Jump to content
  • Advertisement
Sign in to follow this  
bxela1

if problems

This topic is 4402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have nearly finished a game but i have a sentence telling the user to enter attack or potion. and underneath it says: cin >> choice; if ( choice = attack ) The compiler wont accept if ( choice = attack ) why not and how do i fix this?????

Share this post


Link to post
Share on other sites
Advertisement
What does the compiler do? What is choice and what is attack? You are aware that you are trying to assign choice to attack and check choice's new value, right? If you want to compare them you should use ==, not =. Also if you intend to check if the user entered attack, you should make it a string, like this "attack".

Share this post


Link to post
Share on other sites
this is my code.
when i run it my program has no writing.
whats wrong with it?



#include <iostream>
#include <ctime>
#include <string>
#include <stdio.h>
using namespace std;
int main(void)
{
int health, opHealth, potion, opPotion, attack, opAttack;
string choice;
potion = 1;
opPotion = 1;
health = 30;
opHealth = 30;
bool death = false, opDeath = false;
while (true);
{
srand(time(NULL));
cout << "Type attack to fight or potion to heal: " << endl;
cin >> choice;
if ( choice == "attack" )
{
attack = (rand() % 7) + 1;
cout << "You have done " << attack << "damage." << endl;
cout << "Your opponent now has " << opHealth - attack << "health left." << endl;
}
else
{
(potion - 1);
cout << "You now have " << health + 10 << "health." << endl;
}
cout << "It's now the opponents turn." << endl;
opAttack = (rand() % 7) + 1;
cout << "The opponent has done " << opAttack << "damage." << endl;
cout << "You have " << health - opAttack << "health." << endl;
if (health == 0)
{
(death == true);
cout << "You have lost. GAMEOVER." << endl;
}
else
{
(opDeath == true);
cout << "You have won..... WELL DONE!!!!" << endl;
}
}
return 0;
}

Share this post


Link to post
Share on other sites
I think this is what you were looking for:


#include <iostream>
#include <ctime>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
int health, opHealth, potion, opPotion, attack, opAttack;
string choice;
potion = 1;
opPotion = 1;
health = 30;
opHealth = 30;
bool death = false, opDeath = false;
srand(time(NULL));

while ((death==false) && (opDeath == false))
{
cout << "Type attack to fight or potion to heal: " << endl;
cin >> choice;
if ( choice == "attack" )
{
attack = (rand() % 7) + 1;
cout << "You have done " << attack << "damage." << endl;
cout << "Your opponent now has " << opHealth - attack << "health left." << endl;
opHealth=opHealth-attack;
}
else
{
potion--;
cout << "You now have " << health + 10 << "health." << endl;
health=health+10;
}

cout << "It's now the opponents turn." << endl;
opAttack = (rand() % 7) + 1;
cout << "The opponent has done " << opAttack << "damage." << endl;
cout << "You have " << health - opAttack << "health." << endl;
health=health-opAttack;

if (health <= 0)
{
death = true;
cout << "You have lost. GAMEOVER." << endl;
}

if (opHealth <=0)
{
opDeath = true;
cout << "You have won..... WELL DONE!!!!" << endl;
}

}
system("pause");
return 0;
}




please don't just copy-paste it.

a few remarks:

your loop : while(true) is an infinate loop since true is always true. you need to check if death is true or false.

using "cout<<health-1" doesn't actually change the value of health it just shows on the screen how much health-1 is.

Go through my and your code and compare them. And feel free to ask any questions :)

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
What does the compiler do? What is choice and what is attack? You are aware that you are trying to assign choice to attack and check choice's new value, right? If you want to compare them you should use ==, not =. Also if you intend to check if the user entered attack, you should make it a string, like this "attack".


What he said. More specifically, we still don't know:
- What does the compiler do? Which errors do you get?
- What do you expect choice and attack to be? (I see attack is declared as an int, but in the above code you compare choice to "attack", that is, the string literal instead of the int variabl. Which do you actually *intend* to do? What is the player supposed to type in choice?

Share this post


Link to post
Share on other sites
The fact that you just ge a black screen is because of this:

while(true);

with the semicolon after it, this while loop only has effect on the first statement which is, well, nothing. this would be the same as

while(true)
{
//do nothing
}

take a look at the two ways of making a while loop:

while (death == false)
onestatement;
anotherstatement;

here only onestatement will be repeated.


and

while(death ==false)
{
onestatement;
anotherstatement;
andanotherone;
}

here all three statements will be repeated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!