943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 517
  • C++ RSS
Jan 11th, 2009
0

while loop issue

Expand Post »
Can I do this?

c++ Syntax (Toggle Plain Text)
  1. while(variable=functionCall()!=0){
  2.  
  3. /* ... */
  4.  
  5. }

the function I'm calling, is returning a 3, but the variable in the while loop is equal to 1...
I'm not sure what is happening in this statement, Originally I thought it would just always evaluate to true and loop forever since, variable would be successfully set to functionCall()'s return value, therefore evaluating to 1, and 1 does not equal zero, so that too would evaluate to 1 (true) and loop forever... but it sets variable to 1 and then exits the while loop--what's going on?

functionCall() gets input from the user so I can only call it once really, I was going for while(functionCall()!=0){ } , but then I wouldn't be able to check the return value within the loop. I could do something else but I wanted to use this method, so I'm just wondering what it's actually doing.
Similar Threads
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
Jan 11th, 2009
0

Re: while loop issue

Quote ...
variable would be successfully set to functionCall()'s return value
no way, variable is set to true, because functionCall()!=0 is true. in this case true means 1
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 11th, 2009
0

Re: while loop issue

p.s. And yes, this does loop forever if functionCall() really returns 3
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 11th, 2009
0

Re: while loop issue

you should try something like this:
C++ Syntax (Toggle Plain Text)
  1. while ((variable = functionCall())!=0)
...
because it seems to me that variable is given the value of evaluation: functionCall()!=0, so you just need to use one more pair of brackets...
I hope this helps
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Jan 11th, 2009
0

Re: while loop issue

@da penguin, oh right, I forgot about right->left order, and you're right it was looping forever--there are two loops working together and I was mistaken with which loop I was in at what time.

@Alibeg, it worked great, thank you!
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: extracting a sentence
Next Thread in C++ Forum Timeline: CHAR Key Up and Key Down





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC