Jump to content
  • Advertisement
Sign in to follow this  
hothead

need help with my code

This topic is 5463 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'm trying to make a game where you attack and get attacked or type in quit to quit
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main()
{
    const int MAX_DAMAGE = 4;
    const int MAX_HEALTH = 20;
    const string damage[MAX_DAMAGE];      // the damage
    string Phealth[MAX_HEALTH];     // the players health
    string Ehealth[MAX_HEALTH];     // the enemies health
    
    srand(time(0));    // seed the random generater
     damage = (rand() % 4 + 1);    // make the damage random
    int choice;
    while((Phealth != 0) &&(Ehealth != 0) &&(choice != "quit"))
    {
        cout<<"--------\n";
        cout<<"-attack-\n";
        cout<<"--quit--\n";
        cout<<"--------\n";
        cin>>choice;
        if (choice = "attack")
                cout<<"you took"<<damage<<endl;
                cout<<"he took"<<damage<<endl;
        }
    return 0;
    }

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
It could be helpful in the feature if you posted the errors/problems :)

I've compiled your code and there are a few problems with it.

First of all, do you know what the following things mean?

int x;
const string y[value];

You are comparing an integer value against a string. This makes no sence in your program :) You should display a number and behind that the option( "1 attack" , "2 quit"). The user presses the correct number and you compare that number in choice to another integer value (if choice == 1 )

Do some research about variable types. What is integer, what is a string, what means const and what could be compared to eachother with a meaningful result.

Good luck! :)

Share this post


Link to post
Share on other sites
1) What is the problem exactly?

2)
const string damage[MAX_DAMAGE]; // the damage
string Phealth[MAX_HEALTH]; // the players health
string Ehealth[MAX_HEALTH]; // the enemies health

These are all arrays of strings.

3) damage = (rand() % 4 + 1);
Will not work.

4) (choice != "quit")
choice is an int, "quit" is a string literal
Will not work.

5) choice = "attack"
Trying to assign a string literal to an int won't work either.

6)
cout<<"you took"<<damage<<endl;
cout<<"he took"<<damage<<endl;

damage is an array of strings

7) int main()
Should be int main( int argc, char * argv[] )

Share this post


Link to post
Share on other sites
heres the new code its still got errors the errors are unitilized constant on line 12 theres a error on line 19 because of it
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main(int argc, char * argv[])
{
const int MAX_DAMAGE = 4;
const int MAX_HEALTH = 20;
const int damage[MAX_DAMAGE]; // the damage
const int attack = 1;
const int quit = 2;
string Phealth[MAX_HEALTH]; // the players health
string Ehealth[MAX_HEALTH]; // the enemies health

srand(time(0)); // seed the random generater
int damage = (rand() % 4 + 1); // make the damage random
int choice;
while((Phealth != 0) &&(Ehealth != 0) &&(choice != 2))
{
cout<<"--------\n";
cout<<"-1attack-\n";
cout<<"--2quit--\n";
cout<<"--------\n";
cin>>choice;
if (choice = 1)
cout<<"you took"<<damage<<endl;
cout<<"he took"<<damage<<endl;
}
return 0;
}

Share this post


Link to post
Share on other sites
I've removed the damage array and initialised choice.
You could have a look at enumerations to give '1' and '2' a more meaningful name.

The following code compiles:


#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main(int argc, char * argv[])
{
const int MAX_DAMAGE = 4;
const int MAX_HEALTH = 20;
//const int damage[MAX_DAMAGE]; // the damage
const int attack = 1;
const int quit = 2;
string Phealth[MAX_HEALTH]; // the players health
string Ehealth[MAX_HEALTH]; // the enemies health

srand(time(0)); // seed the random generater
int damage = (rand() % 4 + 1); // make the damage random
int choice = 0;
while((Phealth != 0) &&(Ehealth != 0) &&(choice != 2))
{
cout<<"--------\n";
cout<<"-1attack-\n";
cout<<"--2quit--\n";
cout<<"--------\n";
cin>>choice;
if (choice = 1)
cout<<"you took"<<damage<<endl;
cout<<"he took"<<damage<<endl;
}
return 0;
}



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Scheermesje
I've removed the damage array and initialised choice.
You could have a look at enumerations to give '1' and '2' a more meaningful name.

The following code compiles:

*** Source Snippet Removed ***


That code doesn't do what you want it to. Try int PHealth instead of const string PHealth[]. You want health to be a number, not a bunch of strings that you can't change. Here's some changes:


#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>

using namespace std;

int main()
{
const int MAX_DAMAGE = 4;
const int MAX_HEALTH = 20;
int Phealth = MAX_HEALTH; // the players health
int Ehealth = MAX_HEALTH; // the enemies health

srand(time(0)); // seed the random generater
string choice;
do
{
cout<<"--------\n";
cout<<"-attack-\n";
cout<<"--quit--\n";
cout<<"--------\n";
cin>>choice;
if (choice = "attack")
{
damage = (rand() % MAX_DAMAGE + 1); // make the damage random
cout<<"you took"<<damage<<endl;
damage = (rand() % MAX_DAMAGE + 1); // make the damage random
cout<<"he took"<<damage<<endl;
}
}
while((Phealth != 0) &&(Ehealth != 0) &&(choice != "quit"));

return 0;
}



I just took your code and changed the variable types so they made sense. You want to see if choice is "quit" or "attack", so you want a string. Health is a numerical value so you want an int. I also changed it to a do-while loop because you need to find out what choice is before you compare it to stuff. If you try to use it before your program stores anything there you are in trouble. I also moved the damage line inside the loop because you want the damage to be randomized every time you attack.

Also, your code doesn't do anything with health; it will always be at MAX_HEALTH. If you want a player's health to go down you should change the stuff inside the if statement to something like


if (choice = "attack")
{
damage = (rand() % MAX_DAMAGE + 1); // make the damage random
cout<<"you took "<<damage<<" damage\n"<<endl;
Phealth -= damage; //take health away
cout<<"you have "<<Phealth<<" health left\n"; //tell them health
damage = (rand() % MAX_DAMAGE + 1); // make the damage random
cout<<"he took"<<damage<<endl;
Ehealth -= damage; //take health away
cout<<"enemy has "<<Phealth<<" health left\n"; //tell them health
}



I just added lines to take health away and display how much health is left for each player. I hope you understand what I said; if not please read about variable types. Don't just use this if you don't understand it; it will only be more confusing later if you do.

Share this post


Link to post
Share on other sites
I would add that you are messing up your if statement....
if (choice = "attack")
This is an assignment, not a comparison. Should be...
if (choice == "attack")

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!