Having problems with loop

Discussion in 'C++' started by Freyr, Mar 7, 2006.

  1. Freyr

    Freyr Guest

    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
     
    Freyr, Mar 7, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Mar 7, 2006
    #2
    1. Advertising

  3. Freyr

    Mikael Guest

    "Freyr" <> wrote in message
    news:...
    > if(Pull == 'Y' || 'y' && Tokens != 0) {


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

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


    while((Pull == 'Y' || Pull == 'y') && Tokens != 0) {
     
    Mikael, Mar 7, 2006
    #3
  4. Freyr

    Tomás Guest


    > 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
     
    Tomás, Mar 7, 2006
    #4
  5. Freyr

    Freyr Guest

    > 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.
     
    Freyr, Mar 8, 2006
    #5
  6. Freyr

    Freyr Guest

    Wow, it works now. Thanks alot guys, just had to reread that for a
    second.
     
    Freyr, Mar 8, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M. Duijkers
    Replies:
    2
    Views:
    478
    Joe Smith
    Dec 12, 2004
  2. Brian
    Replies:
    5
    Views:
    299
    Marco Wahl
    Feb 25, 2006
  3. david.karr
    Replies:
    3
    Views:
    2,686
    David Karr
    Jul 22, 2009
  4. Slain
    Replies:
    5
    Views:
    357
    Slain
    Jun 9, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    382
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page