C++ Loop Structure Question/Poll

Discussion in 'C++' started by eyebrow, Mar 2, 2007.

  1. eyebrow

    eyebrow New Member

    Joined:
    Mar 2, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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
     
    Last edited by a moderator: Mar 5, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    No 1 because the others have wrong conditions check. Is this a question of some quiz or what?
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  4. eyebrow

    eyebrow New Member

    Joined:
    Mar 2, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  6. eyebrow

    eyebrow New Member

    Joined:
    Mar 2, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    *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.
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  8. eyebrow

    eyebrow New Member

    Joined:
    Mar 2, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0

    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.
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  10. eyebrow

    eyebrow New Member

    Joined:
    Mar 2, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0

    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.
     
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You don't need an expert. A beginner could see your code sucked and your question was silly. What you apparently need is your mama telling you what a good boy you are. Well, go there for that, we don't dole out sugar tits here.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice