Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 5th, 2005, 10:37 AM   #31
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
thank you for your response.

I've noticed that the author's program isn't that great..since I could bet with any amount of money I want..and the program doesn't stop when the amount of money I have is 0..so I said to myself,I'll try to fix that..once the program is running ok yet..it's still not ok with me..

thge author did put this line..I just copied it
void nullrace(void)
{
  return(0, 0);
}
and this is really caused me a lot of confusion..especially after what've u guys told me.

anyway..this is just the first answer after I read all the responses..I'll rewrite the code again in light of what have u told me and I'll tell u about it.

thank u all again for your help.
ARTillery is offline   Reply With Quote
Old Oct 5th, 2005, 11:32 AM   #32
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
all right..I reworked the code..and finally it's working now..I also added a part to make the game to end when the user doesn't have enough money..
I also restricted the amount of money that the user can bet with to whatever he's got..he can't bet with more than he actually got now..
only thing left is to control the user input..if the user enters an unvalid intger..the menu just displayed again..but it goes crazy if the user inters a letter..

I still don't know a way to control a user input..or so I think..so I'll continue and try to modify the code again when I know that..

here's the final version of the code
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

//function declarations
int main(void);
int menu(void);
int race(int, int);
int moneybet(int);
void nullrace(void);

int money=200;

int main(void)
{
    srand(time(0));
    
    int user_response;
    
    cout<<"welcome to snail races"<<endl;
    while(user_response=menu())
    {
        switch(user_response)
            {
            case 1:
            case 2:
            case 3:
                 ::money += race(moneybet(user_response), user_response);
                 break;
            case 4:nullrace();
                 break; 
            }                          
    }
    return 0;     
}    
int menu(void)
    {
              int user_choice;
              if (money>0)
              {
              cout<<"you have "<<money<<" dollars to bet with.\n";
              
              do
              {
              cout<<"\nwhich snail are u going to bet on?"<<endl
                    <<"[1]snail 1\n"
                    <<"[2]snail 2\n"
                    <<"[3]snail 3\n"
                    <<"[4]just watch\n"
                    <<"[0]exit races\n";
                    
                    cin>>user_choice;
              }
              while (user_choice<0 && user_choice>5);
              
              return user_choice;
              }
              else
              cout<<"you don't have any money to bet with"<<endl
                  <<"Game Over\n" ;
                  system ("pause");
                  return 0;                                  
    }
    
int race(int money, int user_response)
{

    int winner= rand() % 3 + 1 ;
    cout<<"here goes the race\n"
           "the winner is the snail number "<<winner<<endl;
           
           if (winner == user_response)
           {
               cout<<"you win "<<money * 2<<" dollars\n"  ;
               return money *2;        
           }
           else
           cout<<"you have lost your money"<<endl;
           return -1* money;     
}

int moneybet(int userResponse)
{
    int betAmount;
     do
     {
     cout<< "Snail " << userResponse << " is a good choice!"
          << endl;
     cout<< "How much would you like to bet on your snail "
          << userResponse <<"?";
     cin >> betAmount;
     }
     while (betAmount > money);
     return betAmount;
     
}


void nullrace (void)
{
    int winner= rand() % 3 + 1 ;
    cout<<"here goes the race\n"
           "the winner is the snail number "<<winner<<endl;
           return;
}

and thank you for your help.
ARTillery is offline   Reply With Quote
Old Oct 5th, 2005, 11:50 AM   #33
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Like all C++ streams, the stream "cin" returns the boolean value "false" if the input operation fails, like when a user enters something that doesn't fit into the reserved space. The following, admittedly very simple, snippet uses the stream's return value to jump out of the loop.

if( !( cin >> user_choice ) )         // '!' means 'NOT', so when >> returns 
                                      //  false, the following is excecuted!
{
   cout << "The input stream broke!"
           "Numbers only please!" << endl;
   break;                             // jump out of do ... while
}
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Oct 5th, 2005, 12:25 PM   #34
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
now the program is perfect with this last peice of information.
I've learned a lot through this example.u all responsible for that
ARTillery is offline   Reply With Quote
Old Oct 5th, 2005, 12:55 PM   #35
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Check your documentation for the purposes on the functions, cin.good (), cin.fail (), and cin.bad (); Not just cin, of course, these are stream functions. They allow you to determine if it's feasible to clear the stream, whether it's "not good" simply because you hit the end of file, or whatever. Perhaps a more robust way to approach user input is to bring in it intact without expecting a conversion to be made (such as with getline), then make the numeric conversions and check to see if they were made or if the attempt failed. You may get bad input, but you won't break the stream.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:18 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC