I'm trying to create a text based RPG, like pokemon.
At the moment i'm trying to make a store to buy health and stuff in.
Everytime you kill a monster you get a random amount of money.
But how do i make the program say "You can't afford that" if you don't have enough money?
And, why does the program always generate the same number?

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

class HJALTEN//the player
{
      public:
      int liv;//life
      int pengar;//pengar


void merliv()// more life
{
     liv++;
     }
     
void mindreliv()//less life
{
     liv--;
     }
     
     
     void merpengar()
{
     pengar++;
     }
     
void mindrepengar()
{
     pengar--;
     }
    
};






class MONSTER// the opponent
{
      public:
      int liv;


void merliv2()
{
     liv++;
     }
     
void mindreliv2()
{
     liv--;
     }

};





int main()
{
    
    int random;
    int random2;
    int random3;
    int random4;
    int vinst;
    int val;
    int val3;
    int val2; 
    HJALTEN t;
    MONSTER h;
    h.liv = 5;
    t.liv = 10;
    t.pengar = 10;
   
    
          
          system("CLS");
          cout << "\a";
          Sleep(400);
          cout << "\a";
          cout << "\a";
          cout << "\a";
          Sleep(800);
          cout << "\a";
          Sleep(800);
          cout << "\a";
          Sleep(400);
          cout << "\a";
          
          start:
                system("CLS");      
          cout << "What do you want to do?" << endl;
          cout << "1. Find an opponent" << endl;
          cout << "2. Buy stuff in the store" << endl;
          cout << "3. Quit" << endl;
          cin  >> val2;
          switch (val2)
          {
                 
          case 1:
                       if(t.liv <=0)
               {
               cout << "\nyou're dead" << endl;
               system("pause");
               }
          cout << "\a";
          system("CLS");
          
          
        
                            

        
        
        
        
                   cout << "you've encountered a monster!" << endl << endl;
                   Sleep(300);
                   mini:
                        system("CLS");
                   cout << "                                                              you have " << t.liv << " HP" ;
                   cout << "                                                          monster has " << h.liv << " HP";
                   cout <<"\nWhat do you want to do?" << endl;
                   cout << "1. Hit monster" << endl;
                   cout << "2. Escape" << endl;
                   cin  >> val;
                   srand( (unsigned)time( NULL ) );
                   for(int randomm=0; randomm<2; randomm++){ 
        random2 = (rand()%10)+1; 
        
        if(randomm=1)
        {
                   
                   if(h.liv <= 0)
                   {
                            system("CLS");
                            cout << "you won!" << endl;
                            Sleep(500);
                            for(int rando2=0; rando2<2; rando2++){ 
                            random = (rand()%10)+1; 
                                 
                            if(rando2 == 1)
                            {
                                      cout << "du har fatt 8 pengar!" << endl;
                                      t.merpengar();
                                      t.merpengar();
                                      t.merpengar();
                                      t.merpengar();
                                      t.merpengar();
                                      t.merpengar();
                                      t.merpengar();
                                      t.merpengar();
                                      
                                      }
                                      
                             else if (rando2 == 2)
                             {
                                  cout << "du har fatt 12 pengar!" << endl;
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  }     
                                  else{cout <<"du har fatt 4 pengar!" << endl;
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  t.merpengar();
                                  }
                            
                            system("pause");
                            goto start;
                            }
                            }
                            }
              
              
              
              
              
                switch(val)
        {
          case 1:
               {
        for(int rando3=0; rando3<2; rando3++){ 
        random4 = (rand()%10)+1; 
                              
               if(rando3==1)
               {
                            cout << "monster lost 2 hp!" << endl;
                            h.mindreliv2();
                            h.mindreliv2();
               goto mini;
               }
               else if (rando3=2)
          {
               cout << "monster lost 1 hp!" << endl;
               h.mindreliv2();
               goto mini;
               }
               }
          case 2:
               {
                    cout << "COWARD" << endl;
                    Sleep(500);
                    goto start;
                    }
                    
                    
                    
                    
}
}         

  
}

case 2: {shop:
     system("CLS");
     cout << " You have " << t.pengar << " money and " << t.liv <<" HP" << endl;
     
cout << "\n\n\n1.buy 5 HP for 4 money" << endl;
         cout << "2.buy 10 HP for 8 money" << endl;
         cout << "3. go back to the menu" << endl;
         cin >> val3;
         
         switch (val3)
         {
                case 1:
                     { if(t.pengar<=0)
                                           {
                                                          cout <<"you can't afford that" << endl;
                                                          cout << "\a";
                                                          goto shop;
                                                          }
                                                                                                                    else{
                           cout << "thanks" << endl;}
                           
                           
                           t.merliv();
                           t.merliv();
                           t.merliv();
                           t.merliv();
                           t.merliv();
                           t.mindrepengar();
                           t.mindrepengar();
                           t.mindrepengar();
                           t.mindrepengar(); 
                                                          
                              goto shop;}
                              
                           
                           case 2:
                               {  if(t.pengar<=0)
                                           {
                                                          cout <<"you can't afford that!" << endl;
                                                          Sleep(500);
                                                          cout << "\a";
                                                          goto shop;
                                                          }
                                                          
                                                          else{cout << "thanks" << endl;
                                Sleep(500);
                                
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.merliv();
                                t.mindrepengar();
                                t.mindrepengar();
                                t.mindrepengar();
                                t.mindrepengar();
                                t.mindrepengar();
                                t.mindrepengar();
                                t.mindrepengar();
                                t.mindrepengar();
                                goto shop;}
                                }
}
                                case 3:
                                     {
                                           goto start;
                                           }
                                          
                                          

}

}

}

i'm swedish, but i've translated the important stuff to english.
Any tips on how i can improve it?
Please give me some advice too, i'm new to C++.
Oh yeah, is there any other way than using goto?
Cuz, for example, when i used goto after killing the opponent, next time you encounter an enemy,
it's health is at 0.

You need to seed the random number generator to make it generate a true random number. Try

#include <ctime>
....
srand((unsigned)time(0));

As for not having enought money you may want to try putting "break" statements at the end of your switch cases... but I forget

You need to seed the random number generator to make it generate a true random number. Try

#include <ctime>
....
srand((unsigned)time(0));

As for not having enought money you may want to try putting "break" statements at the end of your switch cases... but I forget

He already does seed it, at a quick glance i would make a suggestion of changing your functions to save lots of calls to the function example

void merliv(int x)// more life
{
     liv += x;
     }

That way all you have to make is one call and you can increase it by however many you want, aslong as it is within the size of an int ofcourse. Which it would be.

Also you may want to look at class inheritance.

Chris

1.Still no answer why the rand doesn't work :S


2.And oh yeah, with the source code i posted,
whenever you have LESS than 0 money it tells you you don't have enough money.
How do i make it calculate for example:
You have 8 money and wanna buy something for 10 money = Not enough money.

You can keep a new variable for every item.

Namely price.

int price =10;


and a function like

void buy()
{
if(money<price)
{
cout<<"Not Enough Money";
else
{
//Code to add in another .... goes in here
}
}

This is an example You should try to get it for each class. by introducing a variable in the class.

1.Still no answer why the rand doesn't work :S


2.And oh yeah, with the source code i posted,
whenever you have LESS than 0 money it tells you you don't have enough money.
How do i make it calculate for example:
You have 8 money and wanna buy something for 10 money = Not enough money.

It does work... just your not checking the random number! Your checking the loop inwhich you are generating a random number, so the loop counter always reaches 2 when it stops. So when you check what value it is, it's always going to be 2!
you should be checking the random number
Chris

Ermm...
What?:sad:
Idiot explanation please :P

for(int rando3=0; rando3<2; rando3++){ 
random4 = (rand()%10)+1; 
                              
if(rando3==1)

Look at this bit of code, you generate the random number and store it in random4 then in your if statement you check against rando3, which is the loop value, not the random number.

Chris

for(int rando3=0; rando3<2; rando3++){ 
random4 = (rand()%10)+1; 
                              
if(rando3==1)

Look at this bit of code, you generate the random number and store it in random4 then in your if statement you check against rando3, which is the loop value, not the random number.

Chris

Thanks that helped :)
But i've still got some questions left.

1. How do i go back to another piece of the without using goto, because when i use goto it messes up.
Try to compile the code and you'll see what i mean.
After you beat the opponent you go back to the menu, but the next time you click "Find an opponent" it'll have 0 health.


2.What the heck does this mean?
"256 D:\game.cpp jump to case label "
"131 D:\game.cpp crosses initialization of `int randomm' "

2.What the heck does this mean?
"256 D:\game.cpp jump to case label "
"131 D:\game.cpp crosses initialization of `int randomm' "

You are trying to define a variable within a case statement of your switch. This doesn't work due to scoping laws. The quick and easy method to over come this is

switch(x){
case 1:
{
 int b;
}
case 2:

Etc.

[1] Well use a while loop, with a bool flag, so something like this

bool continue = 1;
while(continue){
    //blaahhhhhhhhhhhhhhh
    if (quitVar == "yes") continue = 0;
}

And when you first enter the loop you should set the values for health etc there. So each time it go back to the start of the loop, before printing the menu it resets health etc.

Chris

The only problem is he can't use name "continue", it's c++ keyword ;)

lol, yer i should really go to sleep. At least someone pointed it out, thanks for spotting my silly mistakes.

Chris

*BUMP*
Come on guys i really need some help. :(

*BUMP*
Come on guys i really need some help. :(

Short of writing a full code solution for you, we cannot provide alot more help.
Read the post and have an attempt.

Chris

void merliv(int x)// more life
{
     liv += x;
     }

That way all you have to make is one call and you can increase it by however many you want, aslong as it is within the size of an int ofcourse. Which it would be.

Also you may want to look at class inheritance.

Chris

How do i declare it?
I tried " void merliv(5)" and "void merliv(int x=5)".
When i declared void merliv(5) i got an error message.
When i declared the other one it compiled but you didn't get any health. :S

the code is the declaration. You call it use
t.merliv(5)

And which other code compiled?

Chris

the code is the declaration. You call it use
t.merliv(5)

And which other code compiled?

Chris

Nvm that :P
But thanks anyways :)
And oh yeah, can i get a mega uber easy explanation on how to use anything but goto to go to somewhere else.
I know someone answered the question before but i didn't really get it >.<

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.