0

Hi there

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

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

4
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by neosomosis
0

english1 and english2 don't appear to be initialized. Also, in this line:

while((answer != english1) || (answer != english2))

you are comparing answer to the addresses, not the contents of the variables english1 and english2. String comparisons don't work the way integer comparisons do. Instead of using == or != you usually use the "compare" function from the string library if you want to compare the contents of strings rather than the addresses.

http://www.cplusplus.com/reference/string/string/compare.html

0
while((answer != english1) || (answer != english2)) {

It is guaranteed that one of the two comparisons is TRUE if english1 and english2 are not identical. Therefore the while is always TRUE.

0

Yup...
and you cannot use a comparison for string using '==' or '!='.

The condition for your while should be &&

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.