Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Oct 4th, 2005, 7:04 PM   #21
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
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.
ARTillery is offline   Reply With Quote
Old Oct 4th, 2005, 7:15 PM   #22
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Oct 4th, 2005, 7:18 PM   #23
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
oh it's fine with me..take your time..and thanks again
ARTillery is offline   Reply With Quote
Old Oct 4th, 2005, 8:06 PM   #24
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
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.
Silvanus is offline   Reply With Quote
Old Oct 4th, 2005, 8:07 PM   #25
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
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
stevengs is offline   Reply With Quote
Old Oct 4th, 2005, 8:43 PM   #26
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
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
stevengs is offline   Reply With Quote
Old Oct 4th, 2005, 9:14 PM   #27
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
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.
ARTillery is offline   Reply With Quote
Old Oct 5th, 2005, 12:56 AM   #28
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Oct 5th, 2005, 9:37 AM   #29
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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));
}
It would be a different story if "ini" contained a number of things, and was used in a number of places. In this instance, you are at least doubling the amount of work the cpu has to do in order to seed the random-number generator. It actually detracts from clarity in that the reader has to chase it down to read ONE LINE that could have been in place of the call.

This statement,
   cin>>user_choice;
requires two things of the program. It requires that it gather input, and it requires that the input BE CONVERTIBLE TO AN INTEGER. Do you have so much control over your user that you can force him/her to enter an integer? I don't think so. Cin does not promise to do what you want. It promises to do what you want, OR TELL YOU IT FAILED. Do you ask it if it succeeded or failed? Most definitely not. Once it fails, it won't work again until you fix it. I realize that you are new, and that newbies make this mistake unthinkingly and from ignorance. You have been advised. Failure to check your returns, hereafter, is not a mistake, but schlocky programming, the sign of an unemployable, rank amateur. You can spend as much or as little code as you wish trying to analyze or pinpoint the cause of failure, but at the least, you must detect it and prevent disastrous things from occurring.

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
DaWei is offline   Reply With Quote
Old Oct 5th, 2005, 10:13 AM   #30
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
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
stevengs 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 6:59 AM.

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