9 Replies - 789 Views - Last Post: 25 February 2010 - 06:24 PM Rate Topic: -----

#1 faaz123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-February 10

program has small error, can't figure it out

Posted 25 February 2010 - 05:06 PM

The program executes but it is having problems when you press r or h it goes in the loop once and then the program gets stuck. also the score is not being added properly as well maybe it is because of the problem above. please give any suggestions. thank you
This is a game of pig program.




#include<fstream>
#include<iostream>
#include<cstdlib>
#include<stdlib.h> 
#include<ctime>
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;
}




Is This A Good Question/Topic? 0
  • +

Replies To: program has small error, can't figure it out

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: program has small error, can't figure it out

Posted 25 February 2010 - 05:15 PM

This
while (no_winner = 1);

is assigning the value 1 to the variable no_winner, not comparing the two.

= for assignment, == for comparison.
Was This Post Helpful? 0
  • +
  • -

#3 jessicalegner   User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: program has small error, can't figure it out

Posted 25 February 2010 - 05:16 PM

while(no_winner = 1)
while(no_winner == 1)

This post has been edited by jessicalegner: 25 February 2010 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 jessicalegner   User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 66
  • Joined: 05-June 09

Re: program has small error, can't figure it out

Posted 25 February 2010 - 05:22 PM

.
Was This Post Helpful? 0
  • +
  • -

#5 faaz123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-February 10

Re: program has small error, can't figure it out

Posted 25 February 2010 - 05:34 PM

tried it but program still hangs when I press H to hold. and it doesn't seem to add the scores either in the total computer or human score.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: program has small error, can't figure it out

Posted 25 February 2010 - 05:48 PM

Ah.

while (choice == 'H' || choice == 'h')
    {
         humanTotalScore += accumulatedScore;
    }



When do you suppose you'll break out of that loop?
Was This Post Helpful? 1
  • +
  • -

#7 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: program has small error, can't figure it out

Posted 25 February 2010 - 05:53 PM

When the program reaches here it is likely that the user entered choice = 'H' or 'h', since choice is not changed within the loop, the loop will run continuously
    while (choice == 'H' || choice == 'h')
    {
         humanTotalScore += accumulatedScore;
    }



Edit: Jack pipped me to the post

This post has been edited by #define: 25 February 2010 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#8 faaz123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-February 10

Re: program has small error, can't figure it out

Posted 25 February 2010 - 06:12 PM

so where do i enter the break statement?
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: program has small error, can't figure it out

Posted 25 February 2010 - 06:18 PM

Perhaps it's not a while you want, but an "if"?

Put some thought into your problems. Before you come back with the next one, also notice this:
if (no_winner = 0, humanTotalScore > computerTotalScore)

is probably not what you want either. Again you're assigning there, not comparing, and you probably don't want the comma operator.
Was This Post Helpful? 0
  • +
  • -

#10 faaz123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 25-February 10

Re: program has small error, can't figure it out

Posted 25 February 2010 - 06:24 PM

okay i figured that out. The program also doesn't display the computer's turn at all it just keeps asking me for my turn. The program also generates when I roll only 2,3,5 and 6 all the time and whenever i press r it doesn't add the scores and display it in the humantotalscore.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1