0

I am having trouble with my program in class, as it is infinitely choosing a new random number when I am trying to use the srand() function. I would appreciate some help as I am stuck on this. I am trying to get the code to randomly select one number, and then the user has to guess what the number is. However, my programs random code changes everytime the user is incorrect.

// Game Module

void header();
int menuchoice();
void storytime();
int enter = 0;
int number = 0;
void exit();

void Game2();
int Random_Number();
int selection = 0;

int main() {
  // Declarations
  int game1 = 0;
  int game2 = 0;
  int game3 = 0;
  int choice = 0;

  header();

  menuchoice();

  return 0;
}

void header() {
  cout << "Welcome to the Sprengel Game Emporium!" << endl << endl;
}

int menuchoice() {
  int choice;

  cout << "Press 1 and Enter to Play Game 1 - Story Time" << endl;
  cout << "Press 2 and Enter to Play Game 2 - Guess the Random Number!" << endl;
  cout << "Press 3 and Enter to Play Game 3" << endl;
  cout << "Press 0 to EXIT" << endl;

  cin >> choice;

  if (choice == 1) {
    storydata();
  } else if (choice == 2) {
    Game2();
  } else if (choice == 3) {
    Game3();
  } else if (choice > 3) {
    cout << "Invalid Option, please choose again." << endl;
    menuchoice();
  } else if (choice == 0) {
    exit();
  }
  return 0;
}

// GAME 2

void Game2() {
  cout << "Welcome to Game 2!" << endl;
  cout << "This game randomly generates a number! To win, you have to "
          "correctly guess the number between 1 and 10!"
       << endl;
  cout << Random_Number();
}

int Random_Number() {
  srand(time(NULL));

  int selection = 0;
  int number = rand() % 10 + 1;
  cout << number << endl;
  cout << "Please guess a number between 1 and 10!" << endl;
  cin >> selection;

  if (selection == number) {
    int choose = 0;
    cout << "You are correct!" << endl;
    cout << "Press 1 to Play Again, Press 2 to return to the Game Menu, and "
            "Press 3 to Exit the Game"
         << endl;
    cin >> choose;
    if (choose == 1) {
      Random_Number();
    } else if (choose == 2) {
      menuchoice();
    } else if (choose == 3) {
      exit();
    }
  } else if (selection > number) {
    cout << "Incorrect! Try guessing lower." << endl;
    Random_Number();
  } else if (selection < number) {
    cout << "Incorrect! Try guessing higher." << endl;
    Random_Number();
  }
  return choice;
}

void Game3() { cout << "Welome to Game 3!" << endl; }

void exit() {
  cout << endl << endl;
  cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl << endl;
  cout << "Cya Later! Thanks for Playing!" << endl << endl;
  cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Can you give us what you enter when prompted by the program to reproduce this issue? And can you also give us full output from the program in addition to what you're *expecting* to see and why the actual output is wrong? – scohe001 Jan 29 '20 at 22:36
  • try moving your srand call to main instead of calling it every time you enter Random_Number – swaggy p Jan 29 '20 at 22:44
  • 2
    You should only call `srand` once in your whole program. – Eljay Jan 29 '20 at 22:44
  • 2
    The suggestions to only call `srand` once are fine rules of thumb, but won't fix that issue that you're generating a new random number every time you enter `Random_Number()`. You're using recursive function calls where you should use loops instead. – JohnFilleau Jan 29 '20 at 22:47
  • @Eljay you should never use rand() in the first place, use one of the random number engines provided in the stl – Yamahari Jan 29 '20 at 23:02

0 Answers0