simple while loop

Discussion in 'C' started by free2rhyme2k, Feb 1, 2008.

  1. free2rhyme2k

    free2rhyme2k New Member

    Joined:
    Feb 1, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi there

    I am writing a simple while loop which terminates on the condition that a certain value is found, as follows


    Code:
    std::string question = q.quest().que();
                std::string answer = q.ans().answer();
                std::string graphic = q.inf().info();
                std::string english1;
                std::string english2;
                std::string arabic1;
                std::string arabic2;
               
                
                while((answer != english1) || (answer != english2)) { 
                           random_shuffle(opt.begin(),opt.end()); 
                                english1 = opt[0].english();
                                arabic1 = opt[0].arabic();
                                english2 = opt[1].english();
                                arabic2 = opt[1].arabic();
                }

    The program gets stuck in the while loop. I guess this is some sort of error in the while loops condition. If I take out the second condition and make it just based upon “English1”, then it works fine.

    Any help?

    Thanks in advance
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
  3. logical_brain

    logical_brain New Member

    Joined:
    Feb 6, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    using my mind
    Location:
    India
    Code:
    while((answer != english1) || (answer != english2))
    
    Think over this code.
    let answer != english1 is X
    and answer != english2 is Y

    Code:
    Possible combination
    X   Y   Result
    0   0    0
    0   1    1
    1   0    1
    1   1    1
    
    Only condition when while loop breaks is When Result is 0 Which is only possible when answer = english1 and answer = english2 (Think how it can be possible)

    TRy to change you while loop test condition

    Regards
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice