I'm trying to write a loop, and there's no bugs but there's certain "oopsies" about it.
the random number generator needs to be generated multiple times but is only generating once, giving the same answer over and over.
my purpose is to guide the user through a maze game, with the random number generator making a random outcome with each yes/no. If they win/lose, they drop out of the loop. If they don't win/lose, they open another door and repeat.
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
//assign variables
int random;
char choice = ' ';
char choiceAgain = ' ';
char playAgain = ' ';
//number generator
srand(time(0));
random = 1 + rand() % 3;
//game introduction
cout << "You've lost your great dane and your suspicious-looking green friend." << endl;
cout << "They're in the haunted house somewhere. Will you save them?" << endl;
cout << "Every time you enter a door, you have a chance of:" << endl;
cout << " Meeting a ghost!! You lose." << endl;
cout << " Finding another door." << endl;
cout << " ...And finding your friends! Winner!" << endl;
//begin do-while
do
{
cout << "Would you like to open the door? Y/N" << endl;
cin >> choice;
cin.clear();
cin.ignore(100, '\n');
//not really sure what to do here
do
{
if (random == 3)
cout << "You win! You've found your friends." << endl;
elseif (random == 1)
cout << "You've opened the door and.... there's a ghost!! You lose." << endl;
else{
cout << "You've encountered another door. Open it? Y/N" << endl;
cin >> choiceAgain;
cin.clear();
cin.ignore(100, '\n');
}
}while(toupper(choiceAgain) == 'Y');
cout << "Would you like to play again?" << endl;
cin >> playAgain;
}while (toupper(playAgain) == 'Y');
cout << "Thanks for playing!" << endl;
return 0;
}
I cut and pasted it there, and it worked for each game, (when it's a win/lose) but if the number goes to another door (tie) it goes into a loop.
at least I'm one step closer!
here's current code
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
//assign variables
int random;
char choice = ' ';
char choiceAgain = ' ';
char playAgain = ' ';
//game introduction
cout << "You've lost your great dane and your suspicious-looking green friend." << endl;
cout << "They're in the haunted house somewhere. Will you save them?" << endl;
cout << "Every time you enter a door, you have a chance of:" << endl;
cout << " Meeting a ghost!! You lose." << endl;
cout << " Finding another door." << endl;
cout << " ...And finding your friends! Winner!" << endl;
//begin do-while
do
{
cout << "Would you like to open the door? Y/N" << endl;
cin >> choice;
cin.clear();
cin.ignore(100, '\n');
//NEW random number generator
srand(time(0));
random = 1 + rand() % 3;
do
{
if (random == 3)
cout << "You win! You've found your friends." << endl;
elseif (random == 1)
cout << "You've opened the door and.... there's a ghost!! You lose." << endl;
else{
cout << "You've encountered another door. Open it? Y/N" << endl;
cin >> choiceAgain;
cin.clear();
cin.ignore(100, '\n');
}
}while(toupper(choiceAgain) == 'Y');
cout << "Would you like to play again?" << endl;
cin >> playAgain;
}while (toupper(playAgain) == 'Y');
cout << "Thanks for playing!" << endl;
return 0;
}
I also added another do while statement. I tried to put the rand generator below the second cin.ignore but it also had an infinite loop.
edit: okay, after running it for a few games, it does an infinite loop again. Rats.
edit: I timed it, and the infinite loop only runs for two seconds, giving a different looped answer each time. and then it gives me the "would you like to open another door?" prompt.
I think this is because it is determined by each whole second, so for each second a statement is true, it runs that infinitely until the next second comes by. maybe there's another way to do it rather than seconds?
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
//assign variables
int random;
char choice = ' ';
char choiceAgain = ' ';
char playAgain = ' ';
//game introduction
cout << "You've lost your great dane and your suspicious-looking green friend." << endl;
cout << "They're in the haunted house somewhere. Will you save them?" << endl;
cout << "Every time you enter a door, you have a chance of:" << endl;
cout << " Meeting a ghost!! You lose." << endl;
cout << " Finding another door." << endl;
cout << " ...And finding your friends! Winner!" << endl;
//begin do-while
do
{
cout << "Would you like to open the door? Y/N" << endl;
cin >> choice;
cin.clear();
cin.ignore(100, '\n');
do
{
srand(time(0));
random = 1 + rand() % 3;
if (random == 3)
cout << "You win! You've found your friends." << endl;
elseif (random == 1)
cout << "You've opened the door and.... there's a ghost!! You lose." << endl;
else{
cout << "You've encountered another door. Open it? Y/N" << endl;
cin >> choiceAgain;
cin.clear();
cin.ignore(100, '\n');
}
}while(toupper(choiceAgain) == 'Y');
cout << "Would you like to play again?" << endl;
cin >> playAgain;
}while (toupper(playAgain) == 'Y');
cout << "Thanks for playing!" << endl;
return 0;
}
so the problem is when line 39/else runs once the else statement will make choiceAgain =y and it never changes from Y! so for example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// follow my comments hope this helps
do
{
srand(time(0));
random = 1 + rand() % 3; // if this = 2
if (random == 3)// does not run
cout << "You win! You've found your friends." << endl;
elseif (random == 1)// does not run
cout << "You've opened the door and.... there's a ghost!! You lose." << endl;
else{// does run
cout << "You've encountered another door. Open it? Y/N" << endl;// you put " Y"
cin >> choiceAgain;// now this is = to Y;
cin.clear();
cin.ignore(100, '\n');
}
}while(toupper(choiceAgain) == 'Y');// this is true run again
so because ur do/while was true it runs again
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
{
srand(time(0));
random = 1 + rand() % 3;// now this becomes a 1
if (random == 3)does not run
cout << "You win! You've found your friends." << endl;
elseif (random == 1)//does run
cout << "You've opened the door and.... there's a ghost!! You lose." << endl;
else{//does not run
cout << "You've encountered another door. Open it? Y/N" << endl;
cin >> choiceAgain; // because this does not run it is still = Y
cin.clear();
cin.ignore(100, '\n');
}
}while(toupper(choiceAgain) == 'Y'); // true it = y "this is the problem "
if you dont know how to fix that let me know
p.s just a heads up that on line 26 and 27 if i say N it will open the door anyways D=
No, I'm not quite sure how to fix that.
edit: wait, can I put an OR statement in the while at the end of that?
double edit: YES. I think I've finally got it this time!! I think it works! Thank you much!
I fixed the line 26 & 27 problem. Thanks for pointing that out!
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
//assign variables
double random = 0.0;
char choice = ' ';
char choiceAgain = ' ';
char playAgain = ' ';
//game introduction
cout << "You've lost your great dane and your suspicious-looking green friend." << endl;
cout << "They're in the haunted house somewhere. Will you save them?" << endl;
cout << "Every time you enter a door, you have a chance of:" << endl;
cout << " Meeting a ghost!! You lose." << endl;
cout << " Finding another door." << endl;
cout << " ...And finding your friends! Winner!" << endl;
//begin do-while
do
{
cout << "Would you like to open the door and enter? Y/N" << endl;
cin >> choice;
cin.clear();
cin.ignore(100, '\n');
if (toupper(choice) == 'Y'){
do
{
srand(time(0));
random = 1 + rand() % 3;
if (random == 3)
cout << "You win! You've found your friends." << endl;
elseif (random == 1)
cout << "You've opened the door and.... there's a ghost!! You lose." << endl;
else{
cout << "You've encountered another door. Open it? Y/N" << endl;
cin >> choiceAgain;
cin.clear();
cin.ignore(100, '\n');
}
}while(toupper(choiceAgain) == 'Y' && random == 2);
cout << "Would you like to play again?" << endl;
cin >> playAgain;
}
else{
cout << "Darn right! They can get lost!" << endl;
}
}while (toupper(playAgain) == 'Y');
cout << "Thanks for playing!" << endl;
return 0;
}