Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ Loop Structure Question/Poll (http://www.go4expert.com/forums/cpp-loop-structure-question-poll-t3232/)

eyebrow 3Mar2007 02:20

C++ Loop Structure Question/Poll
 
If you were writing a basic input error trap loop to make sure that the user entered a value between 1 and 100, which of these three loop structures would you use and why. If you have others, feel free to share them.
The three are listed in random order (I actually rolled a die to determine the order).

1)
Code:

while(true)
{
    cout << "Enter number: ";
    cin >> num;
    if(num >= 1 && num <= 100)
        break;
    cout << "Error\n";
}

2)
Code:

cout << "Enter number: ";
cin >> num;
while(num < 1 || num > 100)
{
      cout << "Error\n";
      cout << "Enter number: ";
      cin >> num;
}


3)
Code:

do
{
      cout << "Enter number: ";
      cin >> num;
      if(num < 1 || num > 100)
          cout << "Error\n";
}while(num < 1 || num > 100);

I appreciate any input you have.
--eye

shabbir 5Mar2007 09:42

Re: C++ Loop Structure Question/Poll
 
No 1 because the others have wrong conditions check. Is this a question of some quiz or what?

DaWei 5Mar2007 17:39

Re: C++ Loop Structure Question/Poll
 
There are often many ways to solve a problem. If you're interested in performance, make sure your code works, then examine the emitted machine code. In this day and age, where availability in the on-chip cache is a plus, the size of the loop and the number/destination of jumps will have a decided effect on performance.

eyebrow 5Mar2007 20:38

Re: C++ Loop Structure Question/Poll
 
Quote:

Originally Posted by shabbir
No 1 because the others have wrong conditions check. Is this a question of some quiz or what?

No. It's not a quiz. and you're lucky it's not because you would have failed. :P

The purpose of each loop is to keep the user in the loop until they enter an integer between 1 and 100. They all do that. The first one breaks out of the loop once the user has entered a value in the correct range. The other two do the same, but do not use an infinite loop/break combination. They all "trap" the user (i.e. stay in the loop) as long as the user enters values that are not in the correct range.

Let me be more specific on what I'm asking for: Which one of these loops would you choose to use and why?

DaWei 5Mar2007 21:53

Re: C++ Loop Structure Question/Poll
 
None of them. Anyone who doesn't test the operation of cin when it's required to convert input as well as gather it is either a novice or a schlock. Number two may never enter the loop though it gathered invalid input. All three are subject to infinite looping with a single bad input. Read your documentation regarding stream failures.

eyebrow 5Mar2007 23:41

Re: C++ Loop Structure Question/Poll
 
Quote:

Originally Posted by DaWei
None of them. Anyone who doesn't test the operation of cin when it's required to convert input as well as gather it is either a novice or a schlock. Number two may never enter the loop though it gathered invalid input. All three are subject to infinite looping with a single bad input. Read your documentation regarding stream failures.

*sigh*

Can't you just take the question at face value and make a choice?

I thought this forum was supposed to be helpful, but it seems to be used only by smartasses who either don't read the questions carefully, or don't really care about giving meaningful answers.

I asked a simple question in which I'm concerned only about the structural differences between the loops and which one you would prefer.

If I wanted to talk about technical issues regarding the cin statement, I would have asked about it.

I wondered why so few people posted on this forum when I first posted my question. Now I know why. The posters here are useless.

Thanks for wasting my time fvoucktard.

DaWei 6Mar2007 01:03

Re: C++ Loop Structure Question/Poll
 
You may take it as a smart *** answer, but let me tell you to get your hand fitted for a janitorial mop if you try to produce code like that in the real world. I can break any of those loops with two keypresses.

If you want to know the importance of the structural differences, then you should have read my other reply and taken it to the bank. The fact that you didn't means you are just dicking around.

If you don't want replies that will advance your choices in the programming world, rather than just tickle around on your ding-a-ling, then go to some kiddie wannabe cracker site. I, for one, won't lose one second of sleep or miss you one bit.

eyebrow 6Mar2007 02:13

Re: C++ Loop Structure Question/Poll
 
Quote:

Originally Posted by DaWei
You may take it as a smart *** answer, but let me tell you to get your hand fitted for a janitorial mop if you try to produce code like that in the real world. I can break any of those loops with two keypresses.

If you want to know the importance of the structural differences, then you should have read my other reply and taken it to the bank. The fact that you didn't means you are just dicking around.

If you don't want replies that will advance your choices in the programming world, rather than just tickle around on your ding-a-ling, then go to some kiddie wannabe cracker site. I, for one, won't lose one second of sleep or miss you one bit.


As if I didn't know that anyone with a week of programming basics couldn't break that code. That was beside the point, which is what you've been all along.

It's one thing to offer insightful, realistic help to someone who is stuck on a particular problem. It's another entirely to insult, berate, or otherwise belittle someone who wants a general opinion.

I never asked a damn thing about if you could break the code. Congratulations on your amazing programmatical skill in noticing that it could be done! You should be proud that you put people in their place with such feats of insight while you ignore their actual questions!

And about missing me: from my standpoint I benefit greatly by not being missed by you. You don't even understand how to answer posts! And the only other loser on this site that answered my post couldn't even read the code! What a bunch of bright lights illuminate the world here!

And finally, if you think my posts are dicking around, DON'T RESPOND! Wow! What an amazing concept! It's called keeping you mouth shut when your answer is meaningless!

And speaking of tickling you ding-a-ling, does it get you off to automatically assume that people don't know what their code does? Is that why you lurk here? Your first post was not an answer to my question, but advise on how to solve some imaginary problem you thought I had. Your complete disregard of the meaning of my post speaks volumes about the kind of 'help' you must give out day to day here. I'm sure it satisfies your ego to some degree, but it sure as hell doesn't help.

DaWei 6Mar2007 03:17

Re: C++ Loop Structure Question/Poll
 
Of course not. You're beyond help. Your question was as worthless as your code, and more meaningless. You couldn't even properly post -- the admin had to format it for you. You also said, "any input", did you not? You got input. Now, don't let the door slam you on the *** when you leave.

eyebrow 6Mar2007 04:24

Re: C++ Loop Structure Question/Poll
 
Quote:

Originally Posted by DaWei
Of course not. You're beyond help. Your question was as worthless as your code, and more meaningless. You couldn't even properly post -- the admin had to format it for you. You also said, "any input", did you not? You got input. Now, don't let the door slam you on the *** when you leave.


And yet you bothered to answer. How condescendingly kind of you. You must really suffer dealing with all of us lesser folk, but be assured, it's all for the good, really.

I really thank the admin for formatting my post, it was the only decent help I got here.

Have fun with all both of your pals here. I'm sure you feel that there are a select few who merit your time and I should be grateful to bask in your wisdom even for a little bit.

It's ironic that this site is called "go4expert" because that's exactly what I'll have to do. Go somewhere else to find one.


All times are GMT +5.5. The time now is 03:58.