simple while loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 1
Reputation: free2rhyme2k is an unknown quantity at this point 
Solved Threads: 0
free2rhyme2k free2rhyme2k is offline Offline
Newbie Poster

simple while loop

 
0
  #1
Feb 1st, 2008
Hi there

I am writing a simple while loop which terminates on the condition that a certain value is found, as follows
  1.  
  2. std::string question = q.quest().que();
  3. std::string answer = q.ans().answer();
  4. std::string graphic = q.inf().info();
  5. std::string english1;
  6. std::string english2;
  7. std::string arabic1;
  8. std::string arabic2;
  9.  
  10.  
  11. while((answer != english1) || (answer != english2)) {
  12. random_shuffle(opt.begin(),opt.end());
  13. english1 = opt[0].english();
  14. arabic1 = opt[0].arabic();
  15. english2 = opt[1].english();
  16. arabic2 = opt[1].arabic();
  17. }

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: simple while loop

 
0
  #2
Feb 1st, 2008
english1 and english2 don't appear to be initialized. Also, in this line:

  1. 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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: simple while loop

 
0
  #3
Feb 2nd, 2008
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 37
Reputation: neosomosis is an unknown quantity at this point 
Solved Threads: 0
neosomosis's Avatar
neosomosis neosomosis is offline Offline
Light Poster

Re: simple while loop

 
0
  #4
Feb 2nd, 2008
Yup...
and you cannot use a comparison for string using '==' or '!='.

The condition for your while should be &&
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC