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, 10:28 AM   #1
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
While statement

Hi all,
I'm back again with another question
her's a program from the book I'm studying to learn C++ (C++ programming for the absolute beginner), in this program the programmer uses a while loop..from what I understand while tests a condition and if it's true, it continues to perform whatever it's supposed to do..

here in the example..the while doesn't test anything..the programmer just assigned a value to a variable..

//4.4 - The Snail Racing Game -Dirk Henkemans 
//and Mark Lee - Premier Press
#include <iostream>
#include <ctime>
using namespace std;

//function declarations
int main(void);
int race(int, int);
void race(void);
int menu(void);
int placeBet(int);
void ini(void);

//variables
int money = 200;

//the main function
int main(void)
{
     ini();

     int userResponse;
     cout<< "Welcome to the snail races!!!" <<endl;
     while(userResponse = menu())
     {
          switch(userResponse)
          {
          case 1:
          case 2:
          case 3:
               ::money += 
               race(placeBet(userResponse), userResponse);
               break;
          case 4: //the user did not bet
               race();
               break;
          }
     }
     return 0;
}

//displays the main menu and 
//returns the user’s selection
int menu(void)
{
     int UR;//userResponse;
     cout << "You have " << money << " dollars."<< endl;
     do
     {
          cout<< "Races Menu" <<endl
               << "1) Bet on snail 1" << endl
               << "2) Bet on snail 2" << endl
               << "3) Bet on snail 3" << endl
               << "4) Just Watch" << endl
               << "0) leave the races" << endl;
          cin>> UR;
     }
     while(UR < 0 && UR > 4); 
     return UR;
}

//decides how much a person will bet on the snail
int placeBet(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;
}
//if they are just watching the race
void race (void)
{
     race(0, 0);
}

//if they are betting money
int race (int money, int userResponse)
{
     //stores the random number
     int winner = rand() % 3 + 1;
     cout<< "And the snails are off" << endl
          << "Look at them GO!!!" << endl
          << "The winner is snail " << winner;
     if(winner == userResponse)
     {
          cout<< "You Win!" <<endl;
          return 2 * money;
     }
     cout<<"You loose " << money << " dollars." <<endl;
     return -1 * money;
}

//handles program initializations
void ini(void)
{
     srand(time(0));
}

in line
while(userResponse = menu())

it's very confusing to me at the moment..please can u explain this to me?and I'll probably need more help with this example so please be patiant with me.
thanx in advance.
ARTillery is offline   Reply With Quote
Old Oct 4th, 2005, 10:35 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
The while(userResponse = menu()) line is used to repeat the menu of choices until the user presses 0, which exits the snail races.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Oct 4th, 2005, 10:38 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
this about it this way, what does this do:
userResponse = menu();
is sets the variable called userResponse to whatever is returned from the function menu. So if the user pick option 3, userResponse will be set to 3.

In
while(userResponse = menu())
, any number other than zero is considered true, thus any userResponce we get will keep the loop running EXCEPT when the user enters zero. Then the loop will break and probably the program will end.
OpenLoop is offline   Reply With Quote
Old Oct 4th, 2005, 10:38 AM   #4
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
Simple... while tests, in this case, the result of the assignment expression.

An assignment in C and C++ returns with the value that was assigned. In this case, when menu() is called, and the user inputs 0, userResponse is assigned the value zero. THEN the result of the assignment statement: "userResponse = menu()", which is zero as well, is passed to while for scrutiny. Inherent to C++, false = 0, so the loop is stopped. Any other value is considered true, and the loop would continue.

An assignment always returns the value assigned (*achtung, maybe not when the = operator is overloaded.. )

Which is why

a = b = c = 0;
sets all three variables to 0.
(c = 0) returns with 0, which is then passed to (b = (c = 0)) etc.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Oct 4th, 2005 at 10:54 AM.
stevengs is offline   Reply With Quote
Old Oct 4th, 2005, 10:45 AM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
while(userResponse = menu()) {

}

is equal to
while(1) { // loop for ever
 int userResponse = menu();
 if (!userResponse)
  break;

 // put the rest of the loop here
}
Polyphemus_ is offline   Reply With Quote
Old Oct 4th, 2005, 11:03 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
or
do
{
   userresponse=menu();
   ....
   ....
}while(userresponse);
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Oct 4th, 2005, 11:24 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The "do" is not the same as the "while". If 'userresponse" was zero going in, the while would never be executed, the do will be executed at least once. Close, but no kewpie doll. That's why there are two forms.
__________________
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, 12:04 PM   #8
ARTillery
Newbie
 
ARTillery's Avatar
 
Join Date: Sep 2005
Posts: 17
Rep Power: 0 ARTillery is on a distinguished road
thank u very much for your answer.

Quote:
, any number other than zero is considered true
well, that one is new for me..that's why I thought that the result of this line maybe 2 or 3..and that isn't 0 or 1 which I thought they are a must.

I'm still fighting to understand the whole program but now I have another quick question.

in the previous program..what does ini() function does??I removed this line from the code, and it worked just fine.
ARTillery is offline   Reply With Quote
Old Oct 4th, 2005, 12:10 PM   #9
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
it calls srand(), which sets the random number generator's seed (in this case seeded with the current time). I think you need to add
#include <cstdlib>
at the top with the other includes to get srand().

The seed helps the rand() function produce numbers of greatest possible randomness. There are better sources for the seed, but for most cases, the current time suffices.

CSTDLIB library reference

*Back to the original question, it is important that you understand that the assignment statement is returning the value that it assigns. This return value is what the while construct tests.
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Oct 4th, 2005 at 12:24 PM.
stevengs is offline   Reply With Quote
Old Oct 4th, 2005, 1:04 PM   #10
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by ARTillery
what does ini() function does??I removed this line from the code, and it worked just fine.
If you do that, you will get the same winner everytime you restart your program because the random seed will be constant.
OpenLoop 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 12:31 PM.

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