![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
thank you very much for your help..I really appreciate it.
well the code doesn't change much since the last time I wrote it..just changed the name of the function as I told u..here it is.. #include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
//function declarations
int main(void);
void ini(void);
int menu(void);
int race(int, int);
int moneybet(int);
void nullrace(void);
int money=200;
int main(void)
{
ini();
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;
}
void ini(void)
{
srand(time(0));
}
int menu(void)
{
int user_choice;
cout<<"you have "<<money<<" dollars to bet with.\n";
do
{
cout<<"which 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;
}
int race(int money, int user_response)
{
int winner= rand() % 3 + 1 ;
cout<<"here goes the race"
"the winner is the snail number "<<winner<<endl;
if (winner == user_response)
{
cout<<"you win "<<money * 2<<" dollars" ;
return money *2;
}
else
cout<<"you have lost your money"<<endl;
return -1* money;
}
int moneybet(int userResponse)
{
int betAmount;
cout<< "Snail " << userResponse << " is a good choice!"
<< endl;
cout<< "How much would you like to bet on your snail "
<< userResponse <<"?";
cin >> betAmount;
return betAmount;
}
void nullrace (void)
{
return (0, 0);
}p.s. I don't know if this might help but the code that I wrote in the first post is my reference and has some comments to help understand what's going on and it's working. |
|
|
|
|
|
#22 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Okay, I ripped it. I won't mess with it tonight, check sometime tamale.
__________________
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 |
|
|
|
|
|
#23 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
oh it's fine with me..take your time..and thanks again
![]() |
|
|
|
|
|
#24 |
|
Hobbyist Programmer
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4
![]() |
I think the problem is here:
void nullrace (void)
{
return (0, 0);
}You're defining the function to return nothing, then telling it to return something. Want you want, I think, is: void nullrace (void)
{
race(0, 0);
}This is what the author's code had. I'm not really a C/C++ guy, so I might be totally off the mark- but this makes sense to me. |
|
|
|
|
|
#25 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
void nullrace (void)
{
return (0, 0);
}your prototype says void nullrace(void); The bold void means it promises to return void, which is NOTHING!. But there, within the function definition, what do you see? return (0, 0); This is trying to return something. Even a zero (or whatever that is your are trying to return) is something. You must return void, which means ABSOLUTELY NOTHING. Best bet is to not even have a return statement. (ok, maybe the best thing would be: "return;"): void nullrace(void)
{
// Do some stuph, race some snails, show some results,
// BUT DO NOT RETURN ANYTHING!!
return;
}You might want to consider getting a different book, or reading some introduction material on the internet. You seem to have a few 'knowledge gaps', which might be due to the material you have.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#26 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
you probably want nullrace to look something like this:
void nullrace(void)
{
int winner= rand() % 3 + 1 ;
cout<<"here goes the race"
"the winner is the snail number "<<winner<<endl;
return;
}
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
|
|
#27 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
well..that really fixed it..altho it worked with the original code.
I confess..I'm a complete beginner and I'm tryng to follow the book..I understand the concept but when I see the author use them it sometimes confuses me..the book name is "C++ for the absolute beginner"..that's me..but it leaps a bit ahead sometimes. I'm willing t try another sources though..if u guys know a good source for a beginner like me..please tell me about it..till then, I'll stick to the one I have. |
|
|
|
|
|
#28 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
now that you got it fixed try debugging it for logic errors to remove shit that doesn't make sense. this is a good excercise.
i ran it twice, the first time i bet "201" and lost, giving me $-1. this indicates that i could bet $800,000,000,000 if i wanted although it clearly states i only have 200. the second time i bet "f". this caused everything to go crazy. check for crappy input. also, check to see if the number the user put in is too big for your variable type.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#29 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You've received some appropriate responses, so this is more in light of a reiteration.
While modularization is generally a Good Thang, and usually contributes to clarity or understanding, this is patently ridiculous: ...
ini ();
...
...
void ini (void)
{
srand (time (0));
}This statement, cin>>user_choice; The following has been explained to you in other responses. If this is in the author's work, shitcan the book. If it's your modification, just take the lesson to heart: read what you just wrote with the eyes of the compiler. void nullrace (void)
{
return (0, 0);
}All the following does is keep you in the loop so long as you enter 1-4. No race takes place. Not too terribly exciting. Side note: real people spell "you" out. Get out of your IM mode if you want to appear to be professional. do
{
cout<<"which 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);The correct logic would be: while (user_choice<0 ||user_choice>5); Only cure for this is to learn boolean algebra. Whether or not you control the amount of money bet more closely is up to you. If you're of a mind to let the player bet on credit, that's your business. Currently, you place no restriction on the amount bet, although you provide an initial "stake" of 200.
__________________
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 |
|
|
|
|
|
#30 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
That must be the eleventeen-bazillionth time you've had to mention testing input. However simple yet fundamental it is, you must get tired of repeating it. (or do you copy and paste it anymore?
)( I reckon it only really hurts when you have to mention it to a self acclaimed seasoned programmer )
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|