C++ question

The geek forum. PHP, Perl, HTML, hardware questions etc.. it's all in here. Got a techie question? We'll sort you out. Ask your questions or post a link to your own site here!

C++ question

Postby Slater » Mon Jan 23, 2006 11:28 pm

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...
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia

Postby Tringard » Tue Jan 24, 2006 12:30 am

you're gonna kick yourself :P
your condition is just reassigning doagain to true, so it always evaluates to true
Calm seas do not make skillful sailors.
- African proverb

I choose to follow
Focusing on forever
Day I see His face
Tringard
 
Posts: 89
Joined: Thu Jan 13, 2005 2:28 pm

Postby Sammy Boy » Tue Jan 24, 2006 1:18 am

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 '='.
User avatar
Sammy Boy
 
Posts: 1410
Joined: Wed May 25, 2005 7:04 am
Location: Autobase, Cybertron

Postby Slater » Tue Jan 24, 2006 2:00 am

OH PEWP.

ty all, I love u.
Image
User avatar
Slater
 
Posts: 2671
Joined: Sat May 22, 2004 10:00 am
Location: Pacifica, Caliphornia


Return to Computing and Links

Who is online

Users browsing this forum: No registered users and 58 guests