![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
this about it this way, what does this do:
userResponse = menu(); In while(userResponse = menu()) |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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; (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. |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
while(userResponse = menu()) {
}is equal to while(1) { // loop for ever
int userResponse = menu();
if (!userResponse)
break;
// put the rest of the loop here
} |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
or
do
{
userresponse=menu();
....
....
}while(userresponse);
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Sep 2005
Posts: 17
Rep Power: 0
![]() |
thank u very much for your answer.
Quote:
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. |
|
|
|
|
|
|
#9 |
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
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> 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. |
|
|
|
|
|
#10 | |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|