?
Solved

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

Posted on 2003-03-07
5
Medium Priority
?
229 Views
Last Modified: 2010-04-01
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
Comment
Question by:Svy-20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Expert Comment

by:calebS
ID: 8088491
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
 
LVL 5

Accepted Solution

by:
Kocil earned 80 total points
ID: 8088953
#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
 
LVL 6

Expert Comment

by:gj62
ID: 8089942
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
 

Author Comment

by:Svy-20
ID: 8162438
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
 
LVL 5

Expert Comment

by:Kocil
ID: 8163664
Another question please :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question