This is my first stack overflow question, so please bear with me if I'm not descriptive. My assignment is to create a very simple dice program that's based on a point system, e.g. if you land on a specific number from 1-12, you either win a point, or lose. That's basically all it is. My problem is that, I'm supposed to put a validate function just in case the user put in the wrong input. I made the program to run only if the user input 'y' or 'n', and if not, it will prompt the user to to put the correct response.
class Game
{
private:
int die1;
int die2;
int dice;
int totalPoints;
int totalRolls;
char ch;
void prompt();
void display();
void validate();
void rolls();
public:
void driver();
Game();
};
int main()
{
Game dcObj;
dcObj.driver();
cout << "\n\n\n\n\n\n";
return 0;
}
Game::Game()
{
totalPoints = 0;
totalRolls = 0;
srand(time(NULL));
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice = die1 + die2;
}
void Game::driver()
{
prompt();
display();
}
void Game::display()
{
cout << "\n\nYou rolled the dice " << totalRolls << " times.";
cout << "\nYou won " << totalPoints << " points.";
if (totalPoints <= 0)
{
cout << "\n\nBetter luck next time!";
}
else
cout << "Not bad!";
}
void Game::prompt()
{
cout << "\nWelcome To My Game of Absolute Chance!";
cout << "\n\nDo you want to roll? (y = yes, n = no) ";
cin >> ch;
validate();
}
void Game::validate()
{
while (ch != 'n' || ch != 'y')
{
fseek(stdin, 0, SEEK_END);
cin.clear();
cout << "Invalid response. Please input (y = yes, n = no) ";
cin >> ch;
}
}
void Game::rolls()
{
while (ch == 'y')
{
switch(dice)
{
case 2: cout << "Sorry, you rolled a " << dice << ", you lost.";
totalPoints--;
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 3: cout << "Sorry, you rolled a " << dice << ", you lost.";
totalPoints--;
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 4: cout << "You rolled a " << dice << ", you get nothing.";
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 5: cout << "You rolled a " << dice << ", you get nothing.";
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 6: cout << "You rolled a " << dice << ", you get nothing.";
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 7: cout << "Congratulations! You rolled a " << dice << ", you win!";
totalPoints++;
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 8: cout << "You rolled a " << dice << ", you get nothing.";
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 9: cout << "You rolled a " << dice << ", you get nothing.";
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 10: cout << "You rolled a " << dice << ", you get nothing.";
totalRolls++;
cout << "Points: " << totalPoints;
break;
case 11: cout << "Congratulations! You rolled a " << dice << ", you win!";
totalPoints++;
totalRolls++;
break;
case 12: cout << "Sorry, you rolled a << " << dice << ", you lose.";
totalPoints--;
totalRolls++;
break;
}
}
if (ch == 'n')
{
display();
}
}
My issue is that, regardless if there's good input or not, that while loop always runs. Putting 'y' or 'n' will still make it run. I've attempted about almost everything. Any help will be great!