//this game is played with a six sided die, on each player's turn he may roll until he gets a 1 from the die, at which point, all points accumulated to that point are wiped, he may hold and add the accumulation to his score at any time. 

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h> 
using namespace std; 

const int SCORE_LIMIT = 100;

int diceRoll();
int humanTurn(int& humanTotalScore);
int computerTurn(int& computerTotalScore);


int main ()
{
    bool no_winner = 1;
    int humanTotalScore = 0, computerTotalScore = 0;
    cout << "I would like to play a game" << endl;
    cout << "Take your turn now; the game is pig" << endl << endl;
    
    if ((humanTotalScore < SCORE_LIMIT) && (computerTotalScore < SCORE_LIMIT))
    {
          no_winner = 1;       
    }
    else
    { 
        no_winner = 0;
    }
    
    do
    {
        cout << "Current computer score: " << computerTotalScore << endl;
          
          computerTurn(computerTotalScore);
          humanTurn(humanTotalScore);
    }
    while (no_winner = 1);
    
    if (no_winner = 0, humanTotalScore > computerTotalScore)
    {
                  cout << "You win this time human";
    }
    else 
    {
                  cout << "The Machines win again, human";
    }
    getchar();
    getchar();
    
    return 0;
}

int humanTurn(int& humanTotalScore)
{
    int accumulatedScore = 0; //this is the score accumulated from rolling the dice on this turn
    char choice; //if user wants to continue rolling or keep his score
    int lastRoll; //value of the last dice roll
    cout << "Your current score is " << humanTotalScore << "." << endl;
    cout << "Would you like to roll the dice?" << endl;
    cout << "R(r) to Roll, H(h) to hold your score" << endl;
    cout << "Accumulation from this set of rolls is " << accumulatedScore << endl << endl;
    cin >> choice;
    while (choice == 'R' || choice == 'r')
          {
                  lastRoll = diceRoll();
                  if (lastRoll == 1)
                    { 
                          break;
                    }
                  else
                    {
                          accumulatedScore += lastRoll;
                          cout << "You rolled a " << lastRoll << endl;  
                          cout << "R(r) to roll Again. H(h) to hold. Current Accumulation:" << accumulatedScore << endl;
                          cin >> choice;          
                    }
          }
    while (choice == 'H' || choice == 'h')
    {
         humanTotalScore += accumulatedScore;
    }
    while ( (choice != 'H') || (choice != 'h') || (choice != 'R') || (choice != 'r') ) //not sure why this is not working
    {
         cout << "Invalid input, please enter either R(r) or H(h)" << endl;
         cin >> choice;
    }
    return humanTotalScore;
}

int computerTurn (int& computerTotalScore)
{
     int accumulatedScore = 0; //this is the score accumulated from rolling the dice on this turn
    char choice; //if user wants to continue rolling or keep his score
    int lastRoll; //value of the last dice roll
   /* while ((lastRoll != 1) && (accumulatedScore <= 20))
          {
                   lastRoll = diceRoll();
                   accumulatedScore += lastRoll;
                   computerTotalScore += accumulatedScore;
          }
     */     
          do 
          {       lastRoll = diceRoll();
                  if (lastRoll == 1)
                    { 
                          break;
                    }
                  else
                    {
                          accumulatedScore += lastRoll;
                    }
          }
          while (accumulatedScore <= 20);
          
    return computerTotalScore;
}



int diceRoll( ) 
{
    int x, count; //x is the random number to be written to the file, count is the amount of numbers to be generate
    srand (time(NULL)); //gets a new seed based on the time
    int const M = 1, N = 6;
    x = (M+(rand()%(N-M+1)));
    return x;
}

This is the code, I can't figure out why the while loop that checks if the user did not input an h, H, r, or R does not work properly. I'm sure the solution is simple, its just some kind of syntax thing maybe?

After cin >> choice; you need to flush the keyboard buffer of the '\n' character (Enter key). Just add cin.ignore(); after those lines and it should work

After cin >> choice; you need to flush the keyboard buffer of the '\n' character (Enter key). Just add cin.ignore(); after those lines and it should work

Ok, I tried doing that, but the same thing kept happening.
Right now, I've altogether eliminated that while loop, so that if the user inputs the wrong value, it just brings up his score and asks if he wants to roll again.

while ( (choice != 'H') || (choice != 'h') || (choice != 'R') || (choice != 'r') ) //not sure why this is not working

try && instead of ||. If I'm reading your code correctly, the only time you want the loop to run is if all 4 conditions are true.

Thanks for all the help, I just solved my problems.

How did you solve this? I'm having basically the same problem.

Comments
Don't resurect dead threads.

how did you solve this? I'm having basically the same problem.

oooga boooga, why are you resurecting dead threads?

How did you solve this? I'm having basically the same problem.

What makes you think after two years this guy is just waiting around for you to ask this?

If it's the same problem, try reading the thread and get the same help he got.

Edited 6 Years Ago by WaltP: n/a

This question has already been answered. Start a new discussion instead.