problem with code

1
2
3
4
5
6
do
{
cin>>location2;

}
while(location2!=1 && location2!=2 && location2!=3  && location2!=4  && location2!=5  && location2!=6  && location2!=7  && location2!=8  && location2!=9 && location2==location1);
how can i make the do keep going if location2 is equal to loaction1?
- Exactly what are you trying to do?
i want the loop to keep going if location2 is the same as location1 while the loop is still going if its not a number 1-9
Use OR,
while (location2==location1 || location2>9)
Last edited on
- You can basically use the or || operator as the previous user stated for that.
@alen1234di

I really hate dislike constructs like what you have on line 6. Much better to use a switch statement with a quit case & a default clause to catch bad input, and put the whole thing in a bool controlled while loop.

I have written a number of replies about this, here is one of them:

http://www.cplusplus.com/forum/beginner/99203/#msg534078
hahaha. If you ask me, as long as it works, im fine
corbett it still needs to be x<=1
alen1234di wrote:
hahaha. If you ask me, as long as it works, im fine


Just trying to show some better methods. IMO it is much better to write elegant code, and coders should always be prepared to learn new things. If you aren't ready for this at your early stage, then you may be making life difficult for yourself in the long run.

Also consider the effort that is put in by people who answer questions - are you still happy to go with your bad code, and ignore the advice given that was aimed at trying to help you out?

Further to my suggestion in my previous post, I assert that your code is not scalable - what if you had 20 or 50 different options? I can think of lots of situations where this is the case.
TheIdeasMan wrote:

Just trying to show some better methods. IMO it is much better to write elegant code, and coders should always be prepared to learn new things. If you aren't ready for this at your early stage, then you may be making life difficult for yourself in the long run.

Also consider the effort that is put in by people who answer questions - are you still happy to go with your bad code, and ignore the advice given that was aimed at trying to help you out?

Further to my suggestion in my previous post, I assert that your code is not scalable - what if you had 20 or 50 different options? I can think of lots of situations where this is the case.


- I would hope that he wouldn't use that format with 20-50+ different options.
thejman250 wrote:
- I would hope that he wouldn't use that format with 20-50+ different options.


Even if there are 3 choices, a switch is worthwhile.
TheIdeasMan wrote:
Even if there are 3 choices, a switch is worthwhile.



- Yes, i was agreeing with you.
Last edited on
Topic archived. No new replies allowed.