Hi first time posting on this site so I hope it works out :).
While coding a game I had to use a few if statements to determine a number. The game is "Nim" (not that it matters), and it is being played with marbles. As part of my program I decided to give the user a choice of whether or not they wanted a random number of marbles. To do this the variable of marbles is in an if or else statement. I needed to use the variable of marbles later on but visual studio is telling me that its undefined. Here is the code
cout << "Would you like a Random number of marbles (y/n)" << endl;
string random;
cin >> random;
cout << endl;
//Selected yes for random number of marbles.
if (random == "y")
{
srand((unsigned)time(0));
int marbles;
int lowest=10, highest=100;
int range=(highest-lowest)+1;
for(int index=0; index<20; index++)
{
marbles = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
cout << "You will be playing with " << marbles << " marbles" << endl;
cout << endl;
}
//selected no for the random number of marbles.
else if(random =="n")
{
cout << "How many marbles would you like to be using?" << endl;
cout << endl;
int marbles;
cin >> marbles;
cout << "You will be playing with " << marbles << " marbles." << endl;
cout << endl;
}
Now I need to code the game play for an easy and hard computer. the game revolves around the number of marbles on the table. so I started in a while loop with the variable marbles but it doesn't recognize marbles as a variable so I stopped to solve the issue before I go any further.
if(level == "e")
{
while (marbles >= 1) //marbles is undefined and underlined in red.
}
How do I carry the variable of marbles over form the top after it is either randomly selected or input?