Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Having problems with loop

Reply
Thread Tools

Having problems with loop

 
 
Freyr
Guest
Posts: n/a
 
      03-07-2006
For some reason, my program won't kill itself when Pull == 'n' or the
counter is equal to zero.

I tried two different ways:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
int Tokens=5;
char Pull;

cout << "You have " << Tokens << " tokens. Pull? ";
cin >> Pull;

if(Pull == 'Y' || 'y' && Tokens != 0) {
Tokens--;
srand( (unsigned)time( NULL ) );

int A, B, C, N=3; //Three wheels on spinner
A = rand()%N + 1;
B = rand()%N + 1;
C = rand()%N + 1;

cout << "[" << A << "] [" << B << "] [" << C << "]\n";

if(A==1 && B==1 && C==1) {
Tokens+=4;
cout << "You won 4 tokens!" << endl;
}
else if(A==2 && B==2 && C==2) {
Tokens+=8;
cout << "You won 8 tokens!" << endl;
}
else if(A==3 && B==3 && C==3) {
Tokens+=12;
cout << "You won 12 tokens!" << endl;
}
else {
cout << "You lost" << endl;
}
cout << "You have " << Tokens << " tokens. Pull? ";
cin >> Pull;
}
else {
cout << "Thanks for playing!" << endl;
}
return(0);

}


and


#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
int Tokens=100;
char Pull;

cout << "You have " << Tokens << " tokens. Pull? ";
cin >> Pull;

while(Pull == 'Y' || 'y') {
Tokens--; //Subtract one token for pull

srand( (unsigned)time( NULL ) );

int A, B, C, N=3; //Three wheels on spinner
A = rand()%N + 1;
B = rand()%N + 1;
C = rand()%N + 1;

cout << "[" << A << "] [" << B << "] [" << C << "]\n";

if(A==1 && B==1 && C==1) {
Tokens+=4;
cout << "You won 4 tokens!" << endl;
}
else if(A==2 && B==2 && C==2) {
Tokens+=8;
cout << "You won 8 tokens!" << endl;
}
else if(A==3 && B==3 && C==3) {
Tokens+=12;
cout << "You won 12 tokens!" << endl;
}
else {
cout << "You lost";
}
cout << "You have " << Tokens << " tokens. Pull? ";
cin >> Pull;
}

cout << "Thanks for playing!" << endl;

return(0);

}

I'm at a complete loss. Thanks for any help!

-Freyr

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2006
Freyr wrote:
> [...]
> if(Pull == 'Y' || 'y' && ...


This is not how you check the value of 'Pull' to be _either_ 'Y' or 'y'.
You have _two_ possible situations. 'Pull' can be 'Y'. 'Pull' can be
'y'. You have to spell out both and make a proper connection:

if (Pull == 'Y' || Pull == 'y' && ...

> I'm at a complete loss. [..]


What book are you reading that doesn't have an example of testing two
different values for the same variable?

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
 
Mikael
Guest
Posts: n/a
 
      03-07-2006
"Freyr" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> if(Pull == 'Y' || 'y' && Tokens != 0) {


if((Pull == 'Y' || Pull == 'y') && Tokens != 0) {

> while(Pull == 'Y' || 'y') {


while((Pull == 'Y' || Pull == 'y') && Tokens != 0) {



 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      03-07-2006

> if(Pull == 'Y' || 'y' && Tokens != 0) {


Take the English sentence:

"Put the butter in the fridge or the freezer."

We would rarely repeat "in", as in:

Put the butter in the fridge or _in_ the freezer.

Sometimes we even omit the second "the":

Put the butter in the fridge or freezer.


Well programming languages don't do that. Here's the written English of what
you want to do:

If Pull is equal to 'Y' or 'y', and if Tokens is not equal to zero, then do
particular actions.

Instead of writing:

if ( (Pull == 'Y' || 'y') && Tokens != 0) {

You have to write:

if ( (Pull == 'Y' || Pull == 'y') && Tokens != 0) {


In the latter example, which is correct, the compiler does the following:

1: Test if Pull is equal to 'Y', make this true or false
2: Test if Pull is equal to 'y', make this true or false
3: Do an "OR operation" on the two previous boolean values, and make this
true or false.
4: Test if Tokens is not equal to zero, make this true or false.
5: Do an "AND operation" on the two boolean values retrieved from 3 and 4
respectively, and make this true or false. This is the overall condition for
the "if" statement.

Now let's look at the former example, which doesn't do what you want:

1: Test if Pull is equal to 'Y', make this true or false.
2: 'y' is an expression of type "char". What we need is an expression of
type "bool". How do we convert? Well, all non-zero values become "true",
while zero becomes false; so we make this true or false. 'y' might be the
integral value 56, so this becomes true.
3: Do an "OR operation" on the two previous boolean values, and make this
true or false. As the boolean value retrieved from (2) is always going to be
true, this OR operation will always yield true.
4: Test if Tokens is not equal to zero, make this true or false.
5: Do an "AND operation" on the two boolean values retrieved from 3 and 4
respectively, and make this true or false. This is the overall condition for
the "if" statement.


-Tomás
 
Reply With Quote
 
Freyr
Guest
Posts: n/a
 
      03-08-2006
> What book are you reading that doesn't have an example of testing two
> different values for the same variable?


It's an old exercise book.

Ok, the && Tokens != 0 works now using the while loop, but if I enter
anything the program still picks numbers instead of turminating.

 
Reply With Quote
 
Freyr
Guest
Posts: n/a
 
      03-08-2006
Wow, it works now. Thanks alot guys, just had to reread that for a
second.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Eclipse 3.5 having weird problems, unhandled event loop exceptions,out of permgen space david.karr Java 3 07-22-2009 04:09 PM
Opening a file twice and having an if loop Slain Perl Misc 5 06-09-2007 05:10 PM
Having problems with a loop Brian C++ 5 02-25-2006 08:32 AM
newbie having trouble with control structures: loop till recieve string QUIT M. Duijkers Perl 2 12-12-2004 11:02 AM



Advertisments