Hey guys. This is a portion of code that I've been trying to fix, and I know I'm missing something blatantly obvious and I've recreated this same thing with a simpler program, But for some reason this I can't exit this while loop. Basically I have While (roll != 1 || input != "hold") but whenever I roll a 1 it doesn't exit. Can you guys spot what I'm missing? (this is only a problem for game 1 so I only provided that code)
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
int gameChoice;
int score;
cout << "Please select what you want to do." << endl;
cout << "Input 1 to play Pig, Input 2 to play Blackjack, and input 3 to exit" << endl;
cin >> gameChoice;
if (gameChoice == 1) //start PIG
{
int turnPoints = 0;
int compScore = 0;
int userScore = 0;
int roll = 0;
int turn = 1;
string input;
cout << "Welcome to PIG! Be the first to reach 100." << endl;
while (compScore != 100 || userScore != 100)
{
cout << "Please input 'roll' to roll the dice" << endl;
cin >> input;
if (input == "roll") //First roll of two dice (player)
{
while (roll != 1 || input != "hold")
{
srand(time(0));
roll = 1 + (rand() % 6);
cout << "You rolled a " << roll << endl;
turnPoints = roll + turnPoints;
cout << "You're current score is " << turnPoints << endl;
cout << "Would you like to hold or roll again? " << endl;
cout << "Input 'role' to roll again, or input 'hold' to hold and end your turn." << endl;
cin >> input;
}
if (roll == 1)
{
cout << "You have lost all of your points this turn." << endl;
turnPoints = 0;
cin.ignore();
cin.get();
}
if (input == "hold")
{
userScore = turnPoints + userScore;
cout << "Your total score is now " << userScore << endl;
}
}
}
cin.ignore();
cin.get();
}
system("pause");
return 0;
}
You exit the while loop only if both conditions are false, so you need to roll 1 and hold. If you don't hold, input!="hold" is true, and you continue. I think you want to use && instead of ||.
Also, you need to reset roll and input variables while in the loop on line 31.