![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#31 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
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);
}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. |
|
|
|
|
|
#32 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#33 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#34 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
now the program is perfect with this last peice of information.
I've learned a lot through this example.u all responsible for that ![]() |
|
|
|
|
|
#35 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|