Having problems with loop

F

Freyr

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
 
V

Victor Bazarov

Freyr said:
[...]
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
 
T

Tomás

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
 
F

Freyr

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top