ok so i worked on this during lunch at school since i was bored, but here it is so far.

#include <iostream>
#include <cmath>

int main()
{
    using namespace std;
    
int badguy1;
int action1;
int action;
int att;
int def;
int health;
int shop;
int action2;
int action3;
int action4;
int gold;


badguy1 = 100;
health = 100;
att = 10;
def = 10;
gold = 200;



cout << "main menu" << endl;
cout << "what do you want to do?" << endl;
cout << "1. fight a monster , 2. train attack , 3. train defense , 4. enter the shop ,           5. fight the boss" << endl;
cin >> action1;
if (action1 == 1 || action1 == 1.)
{
            cout << " you have chosen to fight" << endl;
            cout << " the enemy you are fighting has " << badguy1 << " health" << endl;
            cout << " you health is " << health << endl;
            cout << " now what move will you use?" << endl; 
            cout << " 1. attack , 2. defend. 3. run" << endl;
            cin >> action;
            if (action == 1. || action == 1)
            {
                       cout << " you did 10 damage, but your enemy did 8 to you" << endl;
                       badguy1 = 90;
                       health = 92;
                        cout << " now your enemy has " << badguy1 << " health" << endl;
                        cout << " now you health is " << health << endl;
                        cout << " now what move will you use?" << endl; 
                        cout << " 1. attack , 2. defend. 3. run" << endl;
                        cin >> action2;
                        if (action2 == 1. || action2 == 1)
                        {
                                   cout << "CRITICAL HIT! you did 25 damage! your enemy was so scared they couldnt hit you" << endl;
                                   badguy1 = 65;
                                   cout << " now your enemy has " << badguy1 << " health" << endl;
                                   cout << " now you health is " << health << endl;
                                   cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action3;
                                   if (action3 == 1. || action3 == 1)
                                   {
                                              cout << "you did a combo hit and did 70 damage! your enemy did 50 to you" << endl;
                                              health = 42;
                                              badguy1 = 0;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;
                                              cout << " you killed your enemy!" << endl;
                                              cout << " now chose your loot" << endl;
                                              cout << " 1. 75 gold , 2. 50 gold and 3 attack points , 3. 50 gold and 3 defense points , 4. 6 attack points ,              5. 6 defense points , 6. 3 attack points and 3 defense points." << endl;
                                              cin >> action4;
                                              if (action4 == 1. || action4 == 1)
                                              {
                                                          gold = 275;
                                                          cout << "you now have " << gold << " gold" << endl;
                                                          }
                                              else if(action4 == 2. || action4 == 2)
                                              {
                                                   gold = 250;
                                                   att = 13;
                                                   cout << " you now have " << gold << " gold and " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 3. || action4 == 3)
                                              {
                                                   gold = 250;
                                                   def = 13;
                                                   cout << " you now have " << gold << " gold and " << def << " defense points" << endl;
                                                   }
                                              else if(action4 == 5. || action4 == 5)
                                              {
                                                   def = 16;
                                                   cout << " you now have" << def << " defense points" << endl;
                                                   }
                                              else if(action4 == 4. || action4 == 4)
                                              {
                                                   att = 16;
                                                   cout << " you now have" << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 6. || action4 == 6)
                                              {
                                                   att = 13;
                                                   def = 13;
                                                   cout << " you now have" << att << " attack points and " << def << "defense points" << endl;
                                                   }
                                              else cout << " you lose" << endl;     
                                                   
                                              }
else cout << " you lose" << endl;  
                                   }
else cout << " you lose" << endl; 
                   }
else cout << " you lose" << endl; 
            }

    
    system("pause");
    return 0;
    
    }

theres spots here where i know i could use something to make it better but not sure...
anyone have any ideas?
and ideas of things i should add etc.
thanks!

Ideas:
1) consider moving the using statement outside of and before declaring main()

2) Throughout your code you use this syntax:

if (actionX == y. || actionX == y)

Why? All the actionXs are declared to be ints so the period after the integer, y, may not be doing what you expect. What's that period about?

3) If you know about loops, I think you could improve the flow of the game by using one here.

4) If you know about switch statments, this code seems well suited to using one.

Ideas:
1) consider moving the using statement outside of and before declaring main()

2) Throughout your code you use this syntax:

if (actionX == y. || actionX == y)

Why? All the actionXs are declared to be ints so the period after the integer, y, may not be doing what you expect. What's that period about?

3) If you know about loops, I think you could improve the flow of the game by using one here.

4) If you know about switch statments, this code seems well suited to using one.

ya i dont know much c++ havnt learned switches yet and i dont know where the loops would go,
but as for the actionsX's would i actually just put:
actionX == (put number here)
and how would i make it so when i put the damage thing its random, that way if someone where to play it more than once it wouldnt just be the same damage etc.

bool gameOver = false;
int action;
while(!gameOver)
{
   //display menu;
   //accept user input into action
   if(action == 1)
     yourHealth -= random number
     enemyHealth -= random number
   else if(action == 2)
     yourHealth += random number
     enemyHealth -= random number
   else if(action == 3)
     gold += random number
   else if(action == //etc
   else if(action == -1)
      gameOver is true
      you're not through yet but thanks for playing the game
   else
      sorry, that action isn't available

  if(action != -1)
    if(enemyHealth < 1 and yourHealth > 0)
       you win!
       gameOver is true
    else if(enemyHealth > 0 and your Health < 1)
       you lose!
       gameOver is true
    else if
     you survive to take another action
}

Then all of that could be put inside another loop so they could play as many games as they want without closing the program.

You can generate random numbers within a desired range that can be different for each instance of random numbers indicated above.

dont really get what your saying... like what i was thinking is something like this...

when they go to fight the enemy, depending on their level (that is set starting at 0 and depending on the experience they get for killing enemys) the harder the enemy would show up, and it would do random damage and the person would do random damage, and it would drop random ammounts, my ammounts were just baseline but like depending on its level it would drop something
so it could be like

int level; // not 100% sure this would work...
int monster;
int experience;
char drop;
int att;
att = 10;
int damage;
damage = 1-10;
int mdamage;
mdamage = 1-10;
mlevel;
mlevel = 0;
int def;
def = 10;
drop = " 1. 75 gold , 2. 50 gold and 3 attack points , 3. 50 gold and 3 defense points , 4. 6 attack points ,              5. 6 defense points , 6. 3 attack points and 3 defense points."
array monster [level 1 , 100 health];
level = 0;
if (experience == 50)
{
level = 1;
health = 150;
damage = 1-15;
}
else if (experience == 150)
{
level = 2;
health = 250;
damage = 1-25;
}
else if (experience == 300)
{
level = 3;
health = 400;
damage = 1-40;
}
else if (experience == 500)
{
level = 4;
health = 500;
damage = 1-75;
}
// then continue that to the desired thing, but if i have that setup say at the top of the function then later on as the person got experience from the fights would it automatically change the level?
if (mlevel == 1)
{
monster = [mlevel 2 , 200 health];
mdamage = 1-15;
drop = " 1. 100 gold , 2. 75 gold and 5 attack points , 3. 75 gold and 5defense points , 4. 9 attack points ,              5. 9 defense points , 6. 5attack points and 5 defense points." // repeat for the other monsters too...
}
else if (mlevel == 2)
{
monster = [mlevel 3 , health 300]
mdamage = 1-30
}

if (att == 10-15) // means 10 to 15
{
damage ++ 1-10; // i think that would be 1 minus 35 in the computers mind so how would i get it like 1 TO 35?
}
// then repeated... also for the defense thing... would this work?
if (def == 10-15); // 10 to 15
{
mdamage = -1-15; // minus 10 to 15, so like the higher the defense the less damage the person would take... how would i make that?

}

would any of that work?

Edited 6 Years Ago by skorm909: n/a

Congrats on making a decent effort on what we had talked about in the other thread.
However:

array monster [level 1 , 100 health];

damage ++ 1-10;

you're still making up syntax as you go. It would be the equivalent of me walking up to you to have a conversation and saying "Car nematode went see down global equivocally dentist." It's kind of grinding on the mind to read.

Look up rand and srand to get an idea so that you can say something like energy = energy - (rand() % 35 + 1); which will reduce the energy between 1 and 35 points. Use srand() once in your program to seed the random number generator.

Comments
:)
"Car nematode went see down global equivocally dentist." -> classic!

I like to use a macro for this sort of thing:

#define RAND_INT( low, high ) ( rand() % ( high - (low) + 1 ) + low )

you would put that near the beginning of your code (right after your #includes ). Then, you can simply do something like gold = RAND_INT( 10, 20 ); This would result in gold having a random value ranging from 10 to 20. You would still need to call srand at the beginning of your main before you call this function.

I like to use a macro for this sort of thing:

#define RAND_INT( low, high ) ( rand() % ( high - (low) + 1 ) + low )

you would put that near the beginning of your code (right after your #includes ). Then, you can simply do something like gold = RAND_INT( 10, 20 ); This would result in gold having a random value ranging from 10 to 20. You would still need to call srand at the beginning of your main before you call this function.

what do you guys mean by srand?
how do i call it?

#include <cstdlib> // for rand and srand
#include <ctime> //for time()

int main()
{
      srand((unsigned)time(0));
      
//rest of your code etc.

srand is the function, time(0) gets the current system time (in terms of an integer type called time_t) and the (unsigned) casts that time_t to an unsigned integer that srand prefers.

ok so now i have this...

#include <iostream>
#include <cmath>
#include <cstdlib> // for rand and srand
#include <ctime> //for time()
int main()
{
    using namespace std;
    srand((unsigned)time(0));
    
int mlevel;
int level;
int badguy1;
int action1;
int action;
int att;
int def;
int health;
int shop;
int action2;
int action3;
int action4;
int gold;
int damage;
level = 1;
mlevel = 1;
damage = srand;
int mdamage;
mdamage = srand;


badguy1 = 100;
health = 100;
att = 10;
def = 10;
gold = 200;



cout << "main menu" << endl;
cout << "you have " << health << "health and your level" << level << endl;
cout << "what do you want to do?" << endl;
cout << "1. fight a monster , 2. train attack , 3. train defense , 4. enter the shop ,           5. fight the boss" << endl;
cin >> action1;
if (action1 == 1 || action1 == 1.)
{
            cout << " you have chosen to fight" << endl;
            cout << " the enemy you are fighting has " << badguy1 << " health and is level " << mlevel << endl;
            cout << " you have " << health << "health" << endl;
            cout << " now what move will you use?" << endl; 
            cout << " 1. attack , 2. defend. 3. run" << endl;
            cin >> action;
            if (action == 1. || action == 1)
            {
                       
                       badguy1 = 100 - damage;
                       health = 100 - mdamage;
                        cout << " now your enemy has " << badguy1 << " health" << endl;
                        cout << " now you health is " << health << endl;
                        cout << " now what move will you use?" << endl; 
                        cout << " 1. attack , 2. defend. 3. run" << endl;
                        cin >> action2;
                        if (action2 == 1. || action2 == 1)
                        {
                                   cout << "you did " << damage << endl;
                                   badguy1 = badguy1 - damage;
                                   cout << " now your enemy has " << badguy1 << " health" << endl;
                                   cout << " now you health is " << health << endl;
                                   cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action3;
                                   if (action3 == 1. || action3 == 1)
                                   {
                                              cout << "you did a combo hit and did 70 damage! your enemy did 50 to you" << endl;
                                              health = health - mdamage;
                                              badguy1 = badguy1 - damage;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;
                                              cout << " you killed your enemy!" << endl;
                                              cout << " now chose your loot" << endl;
                                              cout << " 1. 75 gold , 2. 50 gold and 3 attack points , 3. 50 gold and 3 defense points , 4. 6 attack points ,              5. 6 defense points , 6. 3 attack points and 3 defense points." << endl;
                                              cin >> action4;
                                              if (action4 == 1. || action4 == 1)
                                              {
                                                          gold = 275;
                                                          cout << "you now have " << gold << " gold" << endl;
                                                          }
                                              else if(action4 == 2. || action4 == 2)
                                              {
                                                   gold = 250;
                                                   att = 13;
                                                   cout << " you now have " << gold << " gold and " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 3. || action4 == 3)
                                              {
                                                   att = 16;
                                                
                                                   cout << " you now have " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 5. || action4 == 5)
                                              {
                                                   def = 16;
                                                   cout << " you now have" << def << " defense points" << endl;
                                                   }
                                              else if(action4 == 4. || action4 == 4)
                                              {
                                                   att = 16;
                                                   cout << " you now have" << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 6. || action4 == 6)
                                              {
                                                   att = 13;
                                                   def = 13;
                                                   cout << " you now have" << att << " attack points and " << def << "defense points" << endl;
                                                   }
                                              else cin >> action;     
                                                   
                                              }
else cin >> action;  
                                   }
else cin >> action; 
                   }
else cin >> action;
            }
else cin >> action;
    
    system("pause");
    return 0;
    
    }

what am i doing wrong?

What error are you getting (also using namespace std; should be outside of main at the top under your #includes)?

(Post 1500) - Ignore this just a counter

Edited 6 Years Ago by jonsca: n/a

I didn't even see those two lines. Yes, you're right. srand() is called once. rand() is used subsequently. If you don't use the % operator to cap it off it's going to give you some big numbers (% is modulus meaning the remainder left after division).

You have to initialize srand like this:

srand ((unsigned)time(0));

You don't actually assign it to a variable..you only assign the rand with a variable. srand just seeds it.

ya it says just stuff... lol
it was working the way i was doing it but like the only thing that im having a problem doing is making the randoms....
its just not working for me...

ya it says just stuff... lol
it was working the way i was doing it but like the only thing that im having a problem doing is making the randoms....
its just not working for me...

Yeah, it is not random because the program is not seeding. Look at a RPG similar to yours that I made when I first started coding. It could quite possibly answer all of your questions..if you take the time to find the pieces of code in my wall-o-code.

http://www.daniweb.com/code/snippet234266.html

ok so i basically got the ideas, had no clue what to get outta ur code lol and i wanted to try it but when i ran it i got an error...
16 F:\Dev-Cpp\someones game.cpp `GetConsoleWindow' undeclared (first use this function)

if i were to make my script, and then put some // lines where i need help,
then post it would you be willing to help me find whats wrong and fix it?

ok so i basically got the ideas, had no clue what to get outta ur code lol and i wanted to try it but when i ran it i got an error...
16 F:\Dev-Cpp\someones game.cpp `GetConsoleWindow' undeclared (first use this function)

if i were to make my script, and then put some // lines where i need help,
then post it would you be willing to help me find whats wrong and fix it?

Sure, as long as there is effort put into your code, I will try to correct your errors. Just make sure you look at what I fixed, so you will know how to do it in the future. (and yes, that code was very long and not formatted well.)

Aslo, the game did not work, because you are using Dev++. You can either get rid of the getconsole code, or download visual studios. (the getconsole statement just tells the screen to resize..which you can do without.)

Edited 6 Years Ago by restrictment: n/a

ok so right now i got the random thing to work but im trying to base the loot off of the monsters level, heres what i got...
im justing going to give the whole script because theres a few errors...

#include <iostream>
#include <cmath>
#include <cstdlib> // for rand and srand
#include <ctime> //for time()
int main()
{
    using namespace std;
    srand((unsigned)time(0));
    
int mlevel;
int level;
int badguy1;
int action1;
int action;
int att;
int def;
int health;
int shop;
int action2;
int action3;
int action4;
int gold;
int damage;
level = 1;
mlevel = 1;
damage = level * (rand()%10)+1;
int mdamage;
mdamage = mlevel * (rand()%10)+1;


badguy1 = 100;
health = 100;
att = 10;
def = 10;
gold = 200;



cout << "main menu" << endl;
cout << "you have " << health << "health and your level" << level << endl;
cout << "what do you want to do?" << endl;
cout << "1. fight a monster , 2. train attack , 3. train defense , 4. enter the shop ,           5. fight the boss" << endl;
cin >> action1;
if (action1 == 1 || action1 == 1.)
{
            cout << " you have chosen to fight" << endl;
            cout << " the enemy you are fighting has " << badguy1 << " health and is level " << mlevel << endl;
            cout << " you have " << health << "health" << endl;
            cout << " now what move will you use?" << endl; 
            cout << " 1. attack , 2. defend. 3. run" << endl;
            cin >> action;
            if (action == 1. || action == 1)
            {
                       
                       badguy1 = 100 - damage;
                       health = 100 - mdamage;
                        cout << "you did " << damage << endl;
                        cout << "your enemy did " << mdamage << endl;
                        cout << " now your enemy has " << badguy1 << " health" << endl;
                        cout << " now you health is " << health << endl;
                        cout << " now what move will you use?" << endl; 
                        cout << " 1. attack , 2. defend. 3. run" << endl;
                        cin >> action2;
                        if (action2 == 1. || action2 == 1)
                        {
                                    damage = level * (rand()%10)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                   cout << "you did " << damage << endl;
                                   cout << "your enemy did " << mdamage << endl;
                                   badguy1 = badguy1 - damage;
                                   health = health - mdamage;
                                   cout << " now your enemy has " << badguy1 << " health" << endl;
                                   cout << " now you health is " << health << endl;
                                   cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action4;
                                   if (action4 == 1. || action3 == 1)
                                   {
                                                damage = level * (rand()%10)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                            cout << "you did " << damage << endl;
                                            cout << "your enemy did " << mdamage << endl;
                                              health = health - mdamage;
                                              badguy1 = badguy1 - damage;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;
                                              cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action4;
                                        if (action4 == 1. || action3 == 1)
                                   {
                                                damage = level * (rand()%10)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                            cout << "you did " << damage << endl;
                                            cout << "your enemy did " << mdamage << endl;
                                              health = health - mdamage;
                                              badguy1 = badguy1 - damage;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;
                                              cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action4;
                                        if (action4 == 1. || action3 == 1)
                                   {
                                                damage = level * (rand()%10)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                            cout << "you did " << damage << endl;
                                            cout << "your enemy did " << mdamage << endl;
                                              health = health - mdamage;
                                              badguy1 = badguy1 - damage;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;
                                              cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action4;
                                        if (action4 == 1. || action3 == 1)
                                   {
                                                damage = level * (rand()%10)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                            cout << "you did " << damage << endl;
                                            cout << "your enemy did " << mdamage << endl;
                                              health = health - mdamage;
                                              badguy1 = badguy1 - damage;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;
                                              cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action4;
                                    else if (badguy1 <= 0)
                                    cout << "you killed your enemy!" << endl;
                                    cout << "here is what you get, choose your loot: " << endl;
                                     cout << " now chose your loot" << endl;
                                     cout << " gold , 2. gold and attack points , 3. gold and defense points , 4. attack points ,              5. defense points , 6. attack points and defense points." << endl;
                                              cin >> action4;
                                              if (action4 == 1. || action4 == 1)
                                              {
                                                          gold = gold ++ mlevel * 50;
                                                          cout << "you now have " << gold << " gold" << endl;
                                                          }
                                              else if(action4 == 2. || action4 == 2)
                                              {
                                                   gold = gold ++ mlevel * 50 * .75;
                                                   att = att ++ mlevel * 2;
                                                   cout << " you now have " << gold << " gold and " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 3. || action4 == 3)
                                              {
                                                   gold = gold ++ mlevel * 50 * .75
                                                   def = def ++ mlevel * 4;
                                                
                                                   cout << " you now have " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 5. || action4 == 5)
                                              {
                                                   def = def ++ mlevel * 4;
                                                   cout << " you now have" << def << " defense points" << endl;
                                                   }
                                              else if(action4 == 4. || action4 == 4)
                                              {
                                                   att = att ++ mlevel * 4;
                                                   cout << " you now have" << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 6. || action4 == 6)
                                              {
                                                   att = att ++ mlevel * 2;
                                                   def = def ++ mlevel * 2;
                                                   cout << " you now have" << att << " attack points and " << def << "defense points" << endl;
                                                   }
                                              else cin >> action4;
                                              }
else cin >> action;  
                                   }
else cin >> action; 
                   }
else cin >> action;
            }
else cin >> action;
}
    system("pause");
    return 0;
    
    }

Alright, I looked through it and got it into working condition again. Here is the code which now works..but has logic errors which you can fix.

#include <iostream>
#include <cmath>
#include <cstdlib> // for rand and srand
#include <ctime> //for time()
int main()
{
	using namespace std;
	srand((unsigned)time(0));

	int mlevel;
	int level;
	int badguy1;
	int action1;
	int action;
	int att;
	int def;
	int health;
	int shop;
	int action2;
	int action3;
	int action4;
	int gold;
	int damage;
	level = 1;
	mlevel = 1;
	damage = level * (rand()%10)+1;
	int mdamage;
	mdamage = mlevel * (rand()%10)+1;


	badguy1 = 100;
	health = 100;
	att = 10;
	def = 10;
	gold = 200;



	cout << "main menu" << endl;
	cout << "you have " << health << "health and your level" << level << endl;
	cout << "what do you want to do?" << endl;
	cout << "1. fight a monster , 2. train attack , 3. train defense , 4. enter the shop ,           5. fight the boss" << endl;
	cin >> action1;
	if (action1 == 1 || action1 == 1.)
	{
		cout << " you have chosen to fight" << endl;
		cout << " the enemy you are fighting has " << badguy1 << " health and is level " << mlevel << endl;
		cout << " you have " << health << "health" << endl;
		cout << " now what move will you use?" << endl; 
		cout << " 1. attack , 2. defend. 3. run" << endl;
		cin >> action;
		if (action == 1. || action == 1)
		{

			badguy1 = 100 - damage;
			health = 100 - mdamage;
			cout << "you did " << damage << endl;
			cout << "your enemy did " << mdamage << endl;
			cout << " now your enemy has " << badguy1 << " health" << endl;
			cout << " now you health is " << health << endl;
			cout << " now what move will you use?" << endl; 
			cout << " 1. attack , 2. defend. 3. run" << endl;
			cin >> action2;
			if (action2 == 1. || action2 == 1)
			{
				damage = level * (rand()%10)+1;
				mdamage = mlevel * (rand()%10)+1;
				cout << "you did " << damage << endl;
				cout << "your enemy did " << mdamage << endl;
				badguy1 = badguy1 - damage;
				health = health - mdamage;
				cout << " now your enemy has " << badguy1 << " health" << endl;
				cout << " now you health is " << health << endl;
				cout << " now what move will you use?" << endl; 
				cout << " 1. attack , 2. defend. 3. run" << endl;
				cin >> action4;
				if (action4 == 1. || action3 == 1)
				{
					damage = level * (rand()%10)+1;
					mdamage = mlevel * (rand()%10)+1;
					cout << "you did " << damage << endl;
					cout << "your enemy did " << mdamage << endl;
					health = health - mdamage;
					badguy1 = badguy1 - damage;
					cout << " now your enemy has " << badguy1 << " health" << endl;
					cout << " now you health is " << health << endl;
					cout << " now what move will you use?" << endl; 
					cout << " 1. attack , 2. defend. 3. run" << endl;
					cin >> action4;
					if (action4 == 1. || action3 == 1)
					{
						damage = level * (rand()%10)+1;
						mdamage = mlevel * (rand()%10)+1;
						cout << "you did " << damage << endl;
						cout << "your enemy did " << mdamage << endl;
						health = health - mdamage;
						badguy1 = badguy1 - damage;
						cout << " now your enemy has " << badguy1 << " health" << endl;
						cout << " now you health is " << health << endl;
						cout << " now what move will you use?" << endl; 
						cout << " 1. attack , 2. defend. 3. run" << endl;
						cin >> action4;
						if (action4 == 1. || action3 == 1)
						{
							damage = level * (rand()%10)+1;
							mdamage = mlevel * (rand()%10)+1;
							cout << "you did " << damage << endl;
							cout << "your enemy did " << mdamage << endl;
							health = health - mdamage;
							badguy1 = badguy1 - damage;
							cout << " now your enemy has " << badguy1 << " health" << endl;
							cout << " now you health is " << health << endl;
							cout << " now what move will you use?" << endl; 
							cout << " 1. attack , 2. defend. 3. run" << endl;
							cin >> action4;
							if (action4 == 1. || action3 == 1)
							{
								damage = level * (rand()%10)+1;
								mdamage = mlevel * (rand()%10)+1;
								cout << "you did " << damage << endl;
								cout << "your enemy did " << mdamage << endl;
								health = health - mdamage;
								badguy1 = badguy1 - damage;
								cout << " now your enemy has " << badguy1 << " health" << endl;
								cout << " now you health is " << health << endl;
								cout << " now what move will you use?" << endl; 
								cout << " 1. attack , 2. defend. 3. run" << endl;
								cin >> action4;
							}
							else if (badguy1 <= 0)
							{
								cout << "you killed your enemy!" << endl;
								cout << "here is what you get, choose your loot: " << endl;
								cout << " now chose your loot" << endl;
								cout << " gold , 2. gold and attack points , 3. gold and defense points , 4. attack points ,              5. defense points , 6. attack points and defense points." << endl;
								cin >> action4;
							}
							if (action4 == 1. || action4 == 1)
							{
								gold = gold + mlevel * 50;
								cout << "you now have " << gold << " gold" << endl;
							}
							else if(action4 == 2. || action4 == 2)
							{
								gold = gold + mlevel * 50 * (3/4);
								att = att + mlevel * 2;
								cout << " you now have " << gold << " gold and " << att << " attack points" << endl;
							}
							else if(action4 == 3. || action4 == 3)
							{
								gold = gold + mlevel * 50 * (3/4);
								def = def + mlevel * 4;
								cout << " you now have " << att << " attack points" << endl;
							}
							else if(action4 == 5. || action4 == 5)
							{
								def = def + mlevel * 4;
								cout << " you now have" << def << " defense points" << endl;
							}
							else if(action4 == 4. || action4 == 4)
							{
								att = att + mlevel * 4;
								cout << " you now have" << att << " attack points" << endl;
							}
							else if(action4 == 6. || action4 == 6)
							{
								att = att + mlevel * 2;
								def = def + mlevel * 2;
								cout << " you now have" << att << " attack points and " << def << "defense points" << endl;
							}
							else cin >> action4;
						}
						else cin >> action;  
					}
					else cin >> action; 
				}
				else cin >> action;
			}
			else cin >> action;
		}
		system("pause");
		return 0;
	}
}

A few things that I corrected:

• Always remember to format your code! This makes it much easier to see your errors, and helps people viewing your code. If you had properly formatted the code, you would have noticed that you were missing '2' braces. You can reformat your code simply by pressing, "CTRL+A" then "Alt+F8".

• The dreaded semi-colons! You were missing around 3 of these, remember to check for them after each line of code.

• the operation of, "++" should only be used when incrementing a single variable by one. When adding a variable to another variable, just use a sing '+' sign.

• If you are going to use decimals in your game, you must initialize the variable as either a float or double value. You will probably learn this soon..for now I just replaced the decimals with fractions.

Tip:

• Try spacing out your context a bit more by using the /endl command.

ok so while i was waiting i found those errors too myself xD.
fixed them and now it runs fine...
but i added a few things, what im trying to get it to do is when the enemys health reaches 0 the "you killed the thing part"
yet it just continues and even into - numbers.
what i think would help is if i could make it so it would loop the fighting until the enemy's health reached 0 or < 0.
anyway heres what i got.

sorry for the long read but i mainly just copied and pasted since i dont know how to do the loop part yet.

#include <iostream>
#include <cmath>
#include <cstdlib> // for rand and srand
#include <ctime> //for time()
int main()
{
    using namespace std;
    srand((unsigned)time(0));
    
int mlevel;
int level;
int badguy1;
int action1;
int action;
int att;
int def;
int health;
int shop;
int action2;
int action3;
int action4;
int gold;
int damage;
level = 1;
mlevel = 1;
damage = level * (rand()%20)+1;
int mdamage;
mdamage = mlevel * (rand()%10)+1;
int ddrop;
int edrop;


badguy1 = 100;
health = 100;
att = 10;
def = 10;
gold = 200;



cout << "main menu" << endl;
cout << "you have " << health << "health and your level" << level << endl;
cout << "what do you want to do?" << endl;
cout << "1. fight a monster , 2. train attack , 3. train defense , 4. enter the shop ,           5. fight the boss" << endl;
cin >> action1;
if (action1 == 1 || action1 == 1.)
{
            cout << " you have chosen to fight" << endl;
            cout << " the enemy you are fighting has " << badguy1 << " health and is level " << mlevel << endl;
            cout << " you have " << health << "health" << endl;
            cout << " now what move will you use?" << endl; 
            cout << " 1. attack , 2. defend. 3. run" << endl;
            cin >> action;
            if (action == 1. || action == 1)
            {
                       
                       badguy1 = 100 - damage;
                       health = 100 - mdamage;
                        cout << "you did " << damage << endl;
                        cout << "your enemy did " << mdamage << endl;
                        cout << " now your enemy has " << badguy1 << " health" << endl;
                        cout << " now you health is " << health << endl;
                        cout << " now what move will you use?" << endl; 
                        cout << " 1. attack , 2. defend. 3. run" << endl;
                        cin >> action2;
                        if (action2 == 1. || action2 == 1)
                        {
                                    damage = level * (rand()%20)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                   cout << "you did " << damage << endl;
                                   cout << "your enemy did " << mdamage << endl;
                                   badguy1 = badguy1 - damage;
                                   health = health - mdamage;
                                   cout << " now your enemy has " << badguy1 << " health" << endl;
                                   cout << " now you health is " << health << endl;
                                   cout << " now what move will you use?" << endl; 
                                   cout << " 1. attack , 2. defend. 3. run" << endl;
                                   cin >> action4;
                                   if (action4 == 1. || action3 == 1)
                                   {
                                                damage = level * (rand()%20)+1;
                                    mdamage = mlevel * (rand()%10)+1;
                                            cout << "you did " << damage << endl;
                                            cout << "your enemy did " << mdamage << endl;
                                              health = health - mdamage;
                                              badguy1 = badguy1 - damage;
                                              cout << " now your enemy has " << badguy1 << " health" << endl;
                                              cout << " now you health is " << health << endl;

thats the first part and heres the "end"

else cin >> action;}
else if (badguy1 <= 0)
                                    {
                                    cout << "you killed your enemy!" << endl;
                                    cout << "here is what you get, choose your loot: " << endl;
                                     cout << " now chose your loot" << endl;
                                     cout << " gold , 2. gold and attack points , 3. gold and defense points , 4. attack points ,              5. defense points , 6. attack points and defense points." << endl;
                                              cin >> action4;
                                              if (action4 == 1. || action4 == 1)
                                              {
                                                          ddrop = mlevel * 50;
                                                          gold = gold + ddrop;
                                                          cout << "you now have " << gold << " gold" << endl;
                                                          }
                                              else if(action4 == 2. || action4 == 2)
                                              {
                                                   ddrop = mlevel * 50;
                                                   ddrop * .75;
                                                   gold = gold + ddrop;
                                                   edrop = mlevel * 2;
                                                   att = att + edrop;
                                                   
                                                   cout << " you now have " << gold << " gold and " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 3. || action4 == 3)
                                              {
                                                   ddrop = mlevel * 50;
                                                   ddrop * .75;
                                                   gold = gold + ddrop;
                                                   edrop = mlevel * 2;
                                                   def = def + edrop;
                                                
                                                   cout << " you now have " << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 5. || action4 == 5)
                                              {
                                                   edrop = mlevel * 4;
                                                   def = def + mlevel;
                                                   cout << " you now have" << def << " defense points" << endl;
                                                   }
                                              else if(action4 == 4. || action4 == 4)
                                              {
                                                   edrop = mlevel * 4;
                                                   att = att + edrop;
                                                   cout << " you now have" << att << " attack points" << endl;
                                                   }
                                              else if(action4 == 6. || action4 == 6)
                                              {
                                                   edrop = mlevel * 2;
                                                   att = att + edrop;
                                                   def = def + edrop;
                                                   cout << " you now have" << att << " attack points and " << def << "defense points" << endl;
                                                   }
                                              else cin >> action4;
                                              }
else cin >> action;}
    system("pause");
    return 0;
    
    }

the rest is just repeats of that, cut it down because it was almost 1000 lines...

also i tried the ctrl + A , alt + F8
didnt do anything...

Edited 6 Years Ago by skorm909: n/a

You are going to need to know how to use Loops before you finish the game. Look into for, while, and do-while loops.

Once you have learned them, try integrating them within your code.

>> also i tried the ctrl + A , alt + F8

That is most likely because you had your code properly formatted.

Edited 6 Years Ago by restrictment: n/a

I would also suggest you use the switch construct. It's easy, and it works with any integer value

switch( action ) // action is some integer value
{
    case 1:  // action == 1
         cout << "Processing code for action 1" << endl;
         break;
    case 3:
         cout << "Executing commands for action 3" << endl;
        break;
    case 7;
         cout << "Doing some stuff for action 7" << endl;
        break;
    default:
         cout << "Running for all actions besides 1, 3, and 7" << endl;
         break;
}

See this website for more detail on if statements, loops, and switch statements (near the bottom) Do not use goto! It is considered bad practice in general

ok so while i had nothing to do in a class i wrote down some c++ things to add, i'll add them to c++ and link the code but for now heres what it is...

main menu different (in a function)

and some new enemys etc...

#include <iostream>

using namespace std;
// i got more but didnt want to add it all at once, this is basically just added to my previous scripts
void fight()
{
     char bname;
     char name;
     array m1;
     array m2;
     int mexperience;
     int experience;
     array m1;
     array m2;
     int action;
     int badguy1;
     int mdamage;
     
     m1 = ["1. |Rabid dog|" << endl; "|health = 90|" << endl; "|Damage = 10|" << endl;];
     m2 = ["2. |Bat|" << endl; "|health = 50|" << endl; "|Damage = 25|" << endl;];
     cout << "what would you like to face?" << endl;
     cout << m1 << endl;
     cout << m2 << endl;
     cin >> action;
     if (action == 1)
     {
                mdamage = (rand ()%10)+1
                badguy1 = 90;
                bname = "Rabid Dog";
                mexperience = 50;
                // need to get this to its own function so i can make it fight by iteself
                }
     if (action == 2)
     {
               mdamage = (rand ()%5)+1
               badguy1 = 50;
               bname = "Bat";
               mexperience = 25;
               // need to get this to its own function so i can make it fight by iteself
                }
     else cin >> action;
                }
void mainmenu()
{
     using namespace std;
     cout << "Main Menu" << endl;
     cout << "What will you do?" << endl;
     cout << "1. go to wilderness , 2. train attack , 3. train defense , 4. go to shop , 5. fight final boss." << endl; 
     cin >> action;
     if (action == 1)
     {
                cout << fight();
                }
     
     
     
     }
     
     
     
     
     
     
     
     
     }

Edited 6 Years Ago by skorm909: n/a

This article has been dead for over six months. Start a new discussion instead.