Page 1 of 1

C++ question

PostPosted: Mon Jan 23, 2006 11:28 pm
by Slater
Alright, I feel really stupid since I can't figure this out.

Ok, I have this loop that I want to terminate when the user presses any key but Y or y. Therefore, I have a boolean that evaluates as true only if the user presses Y or y. This bool determines whether or not the user stays in the do-while loop. Here's the code just for clarity...

Code: Select all

#include <iostream>
using namespace std

int main
{
    *blah blah intro blah*
    bool doagain = false;
    char checking;

    do
    {
        *code and stuff*
        cin << checking;
        if ((checking == 'y') || (checking == 'Y'))
            doagain = true;
        else
            doagain = false;
    }while (doagain = true);
    return 0;
}

or something like that. Anyways, for whatever reason, the bool doagain always evaluates to true, and the user can't exit the loop, no matter what he types. Obviously I'm doing something wrong here...

THis was so much easier in Java when we had equalsIgnoreCase...

PostPosted: Tue Jan 24, 2006 12:30 am
by Tringard
you're gonna kick yourself :P
your condition is just reassigning doagain to true, so it always evaluates to true

PostPosted: Tue Jan 24, 2006 1:18 am
by Sammy Boy
Tringard is right. In case you wanted some clarification, this line:

while (doagain = true);

should really be:

while (doagain == true);

Comparing two values should be '==', not '='.

PostPosted: Tue Jan 24, 2006 2:00 am
by Slater
OH PEWP.

ty all, I love u.