I am making a little silly game in c++ and I am currently working on a way to generate random monsters for me to fight, however, either my random number generator isn't working or my if else function isn't working and it keeps giving me the same monster(Polythemus) If someone could help me fix my code or give me a suggestion on how to make a better random monster generator it would be much appreciated!(Btw the problem lines are around line 35)

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

using namespace std;

int main(int argc, char *argv[])
{
    int loop = 1, health = 100, food = 0, choice, monsterhealth;
    string monstername;
    
    while(loop==1)
    {
                  
                  if(health>100)
                  {
                                health = 100;
                  }
                  system ("CLS");
                  cout << "******Epic Odyssey*****\n\n"
                       << "1. Fight\n"
                       << "2. Rest\n"
                       << "3. Check Inventory\n"
                       << "4. Exit\n\n"
                       << "Your health is at " << health << "%\n\n";
                       cin >> choice;
      
                       
                       switch(choice)
                       {
                                     
                                     case 1:
                                          system ("CLS");

                                          srand((unsigned)time(0)); 
                                          int monsternumber;
                                          monsternumber = (rand()%6)+1; 
                                          if(monsternumber=1)
                                          {
                                                             monsterhealth = 50;
                                                             monstername = "Polyphemus";
                                          }
                                          else
                                          {
                                                             monsterhealth = 60;
                                                             monstername = "Scylla";    
                                          }    
                                          cout << "You encounter a wild " << monstername << " and you have no choice but to fight!\n\n";
                                          system ("PAUSE");
                                          while (monsterhealth>0)
                                          {
                                                cout << "You attack the " << monstername << " for 10 damage!\n";
                                                monsterhealth -= 10;
                                                if(monsterhealth<=0)
                                                {
                                                                    break;
                                                }
                                                cout << "The " << monstername << " eats your friends for 2 damage!\n";
                                                health -= 2;
                                                 if(health<=0)
                                                 {
                                                              cout << "\nYOU DIED GAME OVER!\n";
                                                              system ("PAUSE");
                                                              exit(0);
                                                 }
                                          }
                                          cout << "You defeat the " << monstername << " and gain 5 food! Hoorah!\n\n";
                                          food += 5;
                                          system ("PAUSE");
                                          break;
                                     
                                     case 2:
                                          system ("CLS");
                                          health += 30;
                                          cout << "\nYou rest for a day and gain 30% health\n\n";
                                          system ("PAUSE");
                                          break;
                                     
                                     case 3:
                                          system ("CLS");
                                          cout << "\nYou have " << food << " food in your inventory.\n\n";
                                          system ("PAUSE");
                                          break;
                                          
                                          
                                     case 4:
                                          exit(0);
                                          break;
                                     
                       }
                                          
                  
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

In your if statement you should use a double equality sign:

monsternumber==1

instead of:

monsternumber=1

Cheers,
Erik

use NULL as arg to time

srand((unsigned)time(0)); //change this
srand ( time(NULL) );

Edited 5 Years Ago by vinayakgarg: n/a

In C++ NULL is defined as 0, so it won't make a difference.
(The use of NULL is even discouraged by some, including Stroustrup).

This question has already been answered. Start a new discussion instead.