| | |
simple while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi there
I am writing a simple while loop which terminates on the condition that a certain value is found, as follows
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
I am writing a simple while loop which terminates on the condition that a certain value is found, as follows
C++ Syntax (Toggle Plain Text)
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
Last edited by WaltP; Feb 2nd, 2008 at 12:09 am. Reason: Fixed CODE tags -- try the PREVIEW button before SUBMITting
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
english1 and english2 don't appear to be initialized. Also, in this line:
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/s...g/compare.html
C++ Syntax (Toggle Plain Text)
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/s...g/compare.html
C++ Syntax (Toggle Plain Text)
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. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- trying to loop my program (C++)
- need help on simple turbo C program.. thx (C)
- another simple question concerning nodes (C++)
- A simple problem (C)
- Can't get simple loop to work (C++)
- trying to make a "Do While" loop; loop (C++)
- Getting funky errors with a simple for loop, anyone care to help? (JavaScript / DHTML / AJAX)
- newcommer to c++ need help with simple program (C++)
Other Threads in the C++ Forum
- Previous Thread: std::
- Next Thread: Help me to develop this programme in C++
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






