I am teaching myself C++ and decided to make a dice program. The program will ask the user for the number of dice they want and how many sides on the dice. For instance, I could specify 6 die with 5 sides each. It all works pretty well except it gives me the same random number for each dice roll. Lines 43-47(the for loop) is where the problem lies. Any help or advice would be appreciated. I cant seem to find a solution that works for me and I have been trying to figure it out for a week. Should I be using a nested for loop? vector?
//Dice Roller 2.0
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace std;
int main()
{
int numberDie;
int playerDie, sidesDie;
string yesNo;
srand(time(0)); //seed random number generator based on time
do
{
cout <<"How many sides to the die?\n";
cin >> sidesDie;
cout <<"How many die?\n";
cin >> playerDie;
if (playerDie > 99)
{
cout << "\n\nYou can only have a maximum of 99 die.";
cout << "\nHow many die?\n";
cin >> playerDie;
}
int randomNumber; // Move this inside the loop your getting the numbers from!
for (int i = 0; i < playerDie; i++)
{
randomNumber = rand(); //generates random number // Now makes a new random number each loop.
numberDie = (randomNumber % sidesDie) + 1;
cout << "\nYour " << (i + 1) << " rolled a " << numberDie << ".\n" << endl;
}
cout <<"Would you like to roll again?\n\n";
cin >> yesNo;
for (int i = 0; i < yesNo.length(); ++i) // Make it easier for your condition below
yesNo[i] = tolower(yesNo[i]);
} while (yesNo == "yes");
cout << "Cya next time!\n\n\n";
//keeping the screen open
cout <<"Press Enter Key to Exit";
cin.ignore(std::cin.rdbuf()->in_avail() +4);
return 0;
}