• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

How the heck do you suppose to write the code for this program?????????????

How do you gets the program to continue to loop when answer y or Y
And how do you clear the screen whenever the player enter y or Y to play the game again (this is when the win)

Here is the code:
Please fix it for me

#include <iostream>                                                            // iostream header file
#include <cstdlib>            // contains function prototypes for functions srand and rand
#include <ctime>            // contains prototype for function time
#include <windows.h>                                                      // contains clear and pause

using std::cout;                                                            // program uses cout
using std::endl;                                                            // program uses endl
using std::cin;                                                                  // program uses cin

int rollDice( void );                                                      // function prototype
void regrets (int);                                                            // function prototype
void congrats (int);                                                      // function prototype

int main() {                                                // function main begins program execution
   
   enum Status { CONTINUE, WON, LOST };      // enumeration constants represent game status

   int sum;                                                                   // declared
   int myPoint;                                                                  // declared
   int bankBalance=1000, newBalance=0;                              // initialize
   int home, wager;                                                            // declared
   char answer;                                                                  // declared character

   cout <<"You have $1000 in the bank" << endl;                  // print
   cout <<"Place your wager:" << endl;                              // print
   cin >> wager;                                                            // read from keyboard

   while (wager>1000 || wager<=0) {                                    // begin while
         cout <<"Please bet a valid amount:" << endl;            // print
         cin >> wager;                                                      // read from keyboard
   }                                                                              // end while

   Status gameStatus;                  // can contain CONTINUE, WON or LOST
   srand( time( 0 ) );                  // randomize random number generator using current time
   sum = rollDice();                                                      // first roll of the dice
     
   switch ( sum ) {                        // determine game status and point based on sum of dice
                                                                                    // win on first roll
      case 7:                  
        case 11:            
         gameStatus = WON;
         break;                                                                  // exit switch
                                                                                    // lose on first roll
      case 2:
        case 3:
        case 12:            
         gameStatus = LOST;
         break;                                                                  // exit switch
                                                                                    // remember point
      default:                   
         gameStatus = CONTINUE;
         myPoint = sum;
         cout << "Point is " << myPoint << endl;
         break;                                                                  // optional  
   }                                                                              // end switch

   while ( gameStatus == CONTINUE ) {                              // while game not complete ...
      sum = rollDice();                                                      // roll dice again
                                                                                     // determine game status
      if ( sum == myPoint )                                                // win by making point
         gameStatus = WON;                                                
             
      else
         if ( sum == 7 )                                                // lose by rolling 7
            gameStatus = LOST;
      }                                                                              // end while
                                                                                    // display won or lost message
   home = 1 + rand() % 4;                                                // randomly pick messages
   if ( gameStatus == WON ) {                                          // if won the game
        congrats (home);                                                      // call congrats () for messages
      cout << "Player wins" << endl;                              // print                  
      newBalance=bankBalance + wager;                              // calculate total money
      cout <<"You have $" << newBalance << endl;            // print
        cout <<"Would you like to continue playing:" << endl;      // print
        cin >> answer;                                                      // read from keyboard
        while (answer=='y' || answer=='Y') {                        // begin while
            }                                                                        // end while                        
      }                                                                  
   else                                                                              // if loses
         regrets (home);                                                      // call regrets () for messages
      cout <<"Player loses" << endl;                              // print
        newBalance=bankBalance - wager;                              // calculate total money
        cout <<"You have $" << newBalance << endl;            // print
      if (newBalance ==0)                                                      // end up with 0 money
            cout <<"Sorry. You Busted!" << endl;                  // print

   return 0;                                                            // indicates successful termination
}                                                                                    // end main

                                                            // roll dice, calculate sum and display results
int rollDice( void )
{
   int die1;                                                                  // declared
   int die2;                                                                  // declared
   int workSum;                                                                  // declared

   die1 = 1 + rand() % 6;                                                // pick random die1 value
   die2 = 1 + rand() % 6;                                                // pick random die2 value
   workSum = die1 + die2;                                                // sum die1 and die2
                                                                                    // display results of this roll
   cout << "Player rolled " << die1 << " + " << die2
        << " = " << workSum << endl;

   return workSum;                                                            // return sum of dice
}                                                                                    // end function rollDice


void congrats (int CongratPicker) {                                    // begin function prototype congrats
      switch (CongratPicker) {                                          // begin switch
      case 1:                                                                        // case 1
            cout <<"Now's the time to cash in your chips!" << endl;      // print
            break;                                                                  // exit switch

      case 2:                                                                        // case 2
            cout <<"Wow. You're so lucky!" << endl;                  // print
            break;                                                                  // exit switch
      
      case 3:                                                                        // case 3
            cout <<"Can I borrow a chip?" << endl;                  // print
            break;                                                                  // exit switch

      case 4:                                                                        // case 4
            cout <<"Way too lucky! Those dice have to be loaded" << endl;      // print
            break;                                                                  // exit switch
      }                                                                              // end switch
}                                                                                    // end function congrats

void regrets (int SorryPicker) {                                    // begin function prototype regrets
      switch (SorryPicker) {                                                // begin switch
      case 1:                                                                        // case 1
            cout <<"Oh, you're going for broke, huh?" << endl;            // print
            break;                                                                  // exit switch
            
      case 2:                                                                        // case 2
            cout <<"Aw cmon, take a chance!" << endl;            // print
            break;                                                                  // exit switch

      case 3:                                                                        // case 3
            cout <<"You're a Cheat! It just a matter of time before I catch you" << endl; // print
            break;                                                                  // exit switch

      case 4:                                                                        // case 4
            cout <<"Let try our luck at another table" << endl;      // print
            break;                                                                  // exit switch
      }                                                                              // end switch
}                                                                                    // end function regrets
0
Svy-20
Asked:
Svy-20
1 Solution
 
calebSCommented:
Ouch,
this is a really yucky question. I have no doubt it would be easy, but I am none too keen on trying to figure out what it is that you want.

You asked 2 questions, but perhaps you could be more specific.

ie.
"How do you gets the program to continue to loop when answer y or Y"

And you have this part of your program:
while (answer=='y' || answer=='Y')
{
}

Is this your code (ie is the above snippet that I pasted evidence of you 'having a go'), or is it someone elses, ie an assignment etc.

What part of the program do you want to loop? because whatever part you want to loop should go in the above code snippet.

as for your second question:
'And how do you clear the screen whenever the player enter y or Y to play the game again (this is when the win)'

You would have to do a system call.
ie if you are using windows, (ie a DOS window), you will need something like:
exec("cls");
Though I can't remember what the exact call is, as I never really do system calls (Can anyone else help here??)

Thanks,
Cassandra.


     

0
 
KocilCommented:
#include <conio.h>

int main() {
  enum Status { CONTINUE, WON, LOST };
  /....

  // this is the play loop
  do {
    // clear the screen
    clrscr();   // Borland C++, don't know about VC++

    cout <<"You have $1000 in the bank" << endl;
    cout <<"Place your wager:" << endl;
    // ....

    if ( gameStatus == WON ) {
       congrats(home);
       //...
       cout <<"Would you like to continue playing:"
       // wait the answer
       do {
          cin >> answer;
          answer = toupper(answer);
       } while (answer!='Y' && answer!='N');
     }                                                          else {
        regrets(home);
        // ....
        answer = 'N';
     }
  } while (answer == 'Y');
  // this will loop if the answer is Y

  return 0;
}                                                          

0
 
gj62Commented:
Seems like the continue if Y question has been answered.  I would like to note that Kocil's code always exits if you lose - do you want that?

There is no standard way in C to clear the screen, however, the method below, while having a few drawbacks, actually works quite nicely...

for (int i = 0; i < NUM_LINES_ON_SCREEN; ++i)
  printf("\n");

On most systems, you won't even see it scroll off.  You can set NUM_LINES_ON_SCREEN to a big number (100 or so) if you are unsure of screen size...  Yes, I know there are drawbacks, but it is a quick and easy way that will solve it 98% of the time...

Kocil - FYI clrscr() does not exist outside Borland that I am aware of (not in VC++, nor on any UNIX systems I'm aware of)...
0
 
Svy-20Author Commented:
Thanks!
Can someone debug this one for me too

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int FirstClass (int[], int, int);
int Economy (int[], int, int);
void Indication (int, int);

int main()
{
    int num, seat;
    const int Seat = 10;
    int assignSeat [Seat] = {0,0,0,0,0,0,0,0,0,0};

    while (true) {
         
         cout <<"Please type 1 for First Class" << endl;
         cout <<"Please type 2 for Economy" << endl;
         cin >> num;
     
         if (num == 1) {
              seat = FirstClass (assignSeat, Seat, FirstClass);
         }
         if (num == 2) {
              seat = Economy (assignSeat, Seat, Economy);
         }
         if (seat == -1)
              cout <<"Next flight leaves in 3 hours." << endl;
         else
              Indication (seat, Economy);
    }
    return 0;
}

int FirstClass (int assignSeat[], int Seat, int FirstClass) {
    char answer;
    for (int seat = 1; seat <= 5; seat++) {
         if (assignSeat [seat-1] == 0) {
             assignSeat [seat-1] = 1;
              return seat;
         }
    }

    cout <<"The First Class section is full" << endl;
    cout <<"Would you like to sit in the Economy section (y or n)";
    cin >> answer;
   
     if (answer == 'y' || answer == 'Y') {
         return Economy (assignSeat, Seat, Economy);
    }
    else {
         return -1;
    }
}

int Economy (int assignSeat [], int Seat, int Economy) {
    char answer;
    for (int seat = 6; seat <= 10; seat++) {
         assignSeat [seat-1] = 0; {
         assignSeat [seat-1] = 1;
         return seat;
         }
    }
    cout <<"Next flight leaves in 3 hours" << endl;
}


void Indication (int seat, int Economy) {
    if (seat < Economy)
         cout <<"Your seat assignment is " << seat <<
           "\nYou are in First Class section" << endl;
    else
         cout <<"Your seat assignment is " << seat <<
           "\nYou are in Economy section" << endl;
}  
0
 
KocilCommented:
Another question please :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now