i'm totally lost

This is a discussion on i'm totally lost within the C++ Programming forums, part of the General Programming Boards category; I wrote some of the program but I know it incomplete and wrong. It's suppose to be roulette but the ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    Unhappy i'm totally lost

    I wrote some of the program but I know it incomplete and wrong. It's suppose to be roulette but the random generator isn't working. Wheredo I put that? What do I need to do to make my program correct? Would someone please point me in the right direction please?
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int roll (void);
    int main ()
    
    
    {
    int bet, choice = 0;
    int win, lose;
    cout << "Let's play some roulette!" << endl;
    
    cout << "You will start with $1000." << endl;
    
    cout << "What is your bet ( 1 - 1000, 0 to quit)?" << endl;
    cin >> bet;
    cout << "What do you want to bet on?" << endl;
    
    cout << " 1 - ODD" << endl;
    cout << " 2 - EVEN" << endl;
    cout << " 3 - First group (1 - 12)"<< endl;
    cout << " 4 - Second group (13 - 24)" << endl;
    cout << " 5 - Third group (25 - 36)" << endl;
    cout << " 6 - specific number" << endl;
    
    cout << "Enter choice (1-6): " << endl;
    cin >> choice;
    
    srand(time(0));
    
    
    
    switch (choice) {
    
    case 1: 
    cout << "You chose odd" << endl;
    cout << "The ball lands on" << roll << endl;
    
    win = 1000 + bet;
    if (win)
    	cout << "You win" << win<< endl;
        cout << "You now have $" << win << endl;
    else 
        cout << "You lose" << lose << endl;
        cout << "You now have $" << lose << endl;
    
    break;
    
    case 2: 
    cout << "You chose even" << endl;
    win = 1000 + bet;
    lose = 1000 - 2*bet;
    
    if (win)
    	cout << "You win" << win<< endl;
        cout << "You now have $" << win << endl;
    else 
        cout << "You lose" << lose << endl;
        cout << "You now have $" << lose << endl;
    break;
    
    case 3:
    cout << "You chose first group (1 - 12)"<< endl;
    
    win = 1000 + 2*bet;
    lose = 1000 - 2*bet;
    
    cout << "The ball lands on" << roll << endl;
    
    if (win)
       cout << "You win" << win << endl;
       cout << "You now have $" << win << endl;
    else
       cout << "You lose" << lose << endl;
       cout << "You now have $" << lose << endl; 
    
    break;
    
    case 4:
    cout << "You chose second group (13 - 24)"<< endl;
    win = 1000 + 2*bet;
    lose = 1000 - 2*bet;
    
    cout << "The ball lands on" << roll << endl;
    
    if (win)
       cout << "You win" << win << endl;
       cout << "You now have $" << win << endl;
    else
       cout << "You lose" << lose << endl;
       cout << "You now have $" << lose << endl; 
    
    break;
    
    case 5:
    cout << "You chose third group"<< endl;
    win = 1000 + 2*bet;
    lose = 1000 - 2*bet;
    
    cout << "The ball lands on" << roll << endl;
    
    if (win)
       cout << "You win" << win << endl;
       cout << "You now have $" << win << endl;
    else
       cout << "You lose" << lose << endl;
       cout << "You now have $" << lose << endl; 
    
    break;
    
    case 6:
    cout << "You chose specific number" << endl;
    
    win = 1000 + 35*bet;
    lose = 1000 - 35*bet;
    
    cout << "The ball lands on" << roll << endl;
    
    if (win)
       cout << "You win" << win << endl;
       cout << "You now have $" << win << endl;
    else
       cout << "You lose" <<lose << endl;
       cout << "You now have $" << lose << endl; 
    break;
    
    
    default:
    cout << "Program should never get here!!" << endl;
    }
    
    
    
    while ( bet != 0 ) {
    
    cout << "What is your bet ( 1 - 1000, 0 to quit)?" << endl;
    cin >> bet;
    }
    
    return 0;
    
    }
    
    int roll (void){
    
    int roll = rand() % 38;
    int r;
    r = rand ();
    for (int counter = 1; counter <= 38; counter++) {
    	cout << (roll);
    
    }
     return roll;
    }


    Task:You are to write a program that will allow a user to play a simplified version of roulette. The sample execution given below should help demonstrate the expected behavior and the error checking required. If you lose on a turn, then you lose your bet. If you win on a turn, then for ODD/EVEN you win the equivalent amount of your bet. For a 1/3 RANGE bet (first group, second group, third group), you win 2 times your bet. When betting on a specific number, you win 35 times your bet. Also notice that the roulette wheel contains 38 equally probable numbers: 0, 00, 1, 2, ... 36. When 0 or 00 comes up, you lose ... regardless of the bet.
    Rules and Requirements:
    1) Start the user out with $1000. The game should loop until the user enters a 0 bet (the signal to cash out) or until the user goes bankrupt (no money left). Upon cashing out, you should print out the user's final monetary total. When a game ends, ask the user if he/she wants to play again (y/n). If the user answers yes, start another game, beginning agiain at $1000. If no, quit the program. If an invalid character is entered, print an error message and make the user re-enter their response. (You may assume the input to this question will be a single character).
    2) For each spin of the wheel, first ask the user for the amount of the bet, then for what type of bet. Negative bets are illegal, as are bets larger than the amount of money you have, and there are only six valid menu choices (as shown in the sample execution). Upon an illegal entry, print an error message and prompt the user to re-enter. If the user wants to bet on a specific number, have the user input this as well (and error check the value). Valid numbers on the wheel are 1 through 36.

    Note: For all of these inputs, you may assume the correct type of input. (All of the inputs described in item (2) above are integers). You only need to error check on values.

    3) You will need to use random number generation to simulate the spinning of the roulette wheel. Make sure to include the appropriate libraries.

    4) Your output doesn't have to match the sample execution word for word, but it must contain all of the elements that are shown in the sample run. For example, when prompting for the bet, you must print the possible range allowable for the bet (this varies based on the amount of money you have).

    5) General Requirements
    No global variables, other than constants!
    You must use two or more useful functions in your program. (For example, if you write the entire program in a function, then write main() so that it does nothing but call that function, this does not count as useful). Between your two or more functions, you must use at least one useful parameter, and one useful return.
    You may use the libraries: iostream , cstdlib, ctime. (The last two are for the random number generation).

    Sample output:

    Let's play some roulette!

    You will start out with $1000.

    What is your bet (1 - 1000, 0 to quit)? 500
    What do you want to bet on?

    1 - ODD
    2 - EVEN
    3 - First group (1-12)
    4 - Second group (13-24)
    5 - Third group (25-36)
    6 - specific number

    Enter choice (1-6): 2
    The ball lands on 2.
    You win $500.
    You now have $1500

    What is your bet (1 - 1500, 0 to quit)? 1


    Code tags added by Hammer

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    AIYAHH! Use code tags and indent
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    lurker
    Guest
    I compiled your code, and there were a bunch of syntax errors. If you have more than one line in your 'if' statement, you have to put "{" "}" around them. And, you have a bunch of logic problems. Can you try to do one piece of the program at a time, get it working, then move onto the next. For instance, you don't verify whether the bet amount is valid. I've been working on this (just to keep busy) and I wrote a betamt function. Here it is:

    Code:
    int getbetamt(int &playerstot)
    {
    bool validbetamt;
    int betamt = 0;
       //This do-while statement asks for the amount of the bet and verifies
       //that it is a valid amount
    do{
       cout<< "You have $" <<playerstot <<" to bet." <<endl
            <<"What is your bet (0 to quit)?" <<endl;
       cin >> betamt;
       validbetamt = true;
    
       if(betamt > playerstot)
          {
          cout<<"The amount you bet is greater than the amount you "
              <<"have, please try again. " <<endl;
          validbetamt = false;
          }
       else if (betamt < 0)
          {
          cout<<"You have entered a negative bet amount. Please "
               <<"try again. " <<endl;
          validbetamt = false;
          }
       }while (!validbetamt);
    return betamt;
    }
    'main' looks like this:
    Code:
    int main ()
    {
    int bet = 0, playerstotal = 1000;
    int betnumber, balldrop; 
    char choice = '0';
    
    cout <<"\nLet's play some roulette!" <<endl
         <<"You will start with $1000." <<endl;
    
    
    bet = getbetamt(playerstotal);
    I have a 'playerstotal' variable which you don't, but I think you'll find it useful. The next piece is the bet choice. I've been working on it, but it's not working yet. If you get that working and post as you go, I and others are willing to help. Just an idea of what needs to be done:

    Code:
    have the player make a choice (1-6)
       if 6 - player has to pick a number to bet on
       if not 1-6 player has to choose again
    wheel spins, ball drops on number
    compare number with players choice
      if players choice == odd && balldrop == odd
         player wins
         calculate amount won
      else if players choice == even && balldrop == even
         player wins
         calculate amount won
      else if players choice == 1-12 && balldrop == 1-12
         player wins
         calculate amount won
      etc
      else
        player loses
    
    calculate new playerstotal
    There's alot to do, but if you do it piece by piece it'll be easier then trying to do the whole thing at once then fix it. Also, this is just a quick analysis, and I may have left stuff off, so don't use this as your only guide.

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    You might want to check if the input is acually a digit because if it isn't then you will probley have a problem. In my expirence with invalid input my program got into an infinate loop and we all know this is annoying. Look into isdigit().

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    This is wrong :
    Code:
    if (win)
      cout << "You win" << win<< endl;
      cout << "You now have $" << win << endl;
    else 
      cout << "You lose" << lose << endl;
      cout << "You now have $" << lose << endl;
    try this ;

    Code:
    if (win)
    {
      cout << "You win" << win<< endl;
      cout << "You now have $" << win << endl;
    }
    else 
    {
      cout << "You lose" << lose << endl;
      cout << "You now have $" << lose << endl;
    }

    This is wrong,
    Code:
      cout << "The ball lands on" << roll << endl;
    this is probably what your looking for
    Code:
      cout << "The ball lands on" << roll() << endl;
    Another thing,
    Code:
     if (win)
    If Im not mistaken this is true aslong as win is not equal to zero.

    It seems to me you have some logical errors in your code.

  6. #6
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    You are to write a program...
    tsk tsk...
    I AM WINNER!!!1!111oneoneomne

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!! NEw to Arrays/Functions.. LOST
    By felixgun in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2006, 01:20 PM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 08:13 PM
  3. Totally lost beginner
    By burrissa in forum C Programming
    Replies: 7
    Last Post: 03-29-2004, 08:00 PM
  4. Lost ID number
    By ripper079 in forum C++ Programming
    Replies: 13
    Last Post: 10-04-2002, 01:51 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21