Thread: Do While Loop Need Help Fast!!!

  1. #16
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ...It's one or the other. Make up your mind. Also, try listening to what people have been telling you. What's the point of asking for help if you aren't going to pay attention to it?

    Quzah.
    So that's what we've been doing wrong. Expecting people that are asking for our help are actually going to listen to us.

    And mack, put some of that code in FUNCTIONS instead of cramming it all down main()'s throat.

  2. #17
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    Quote Originally Posted by quzah
    try listening to what people have been telling you. What's the point of asking for help if you aren't going to pay attention to it?
    To be fair, he did apply code tags when told to, and did reindent when told to. So it's not that he hasn't been listening. It's that he hasn't been understanding. And he hasn't been understanding because the root of what he's not understanding hasn't been addressed.

    Mackology101: Looking at your code, it looks like your goal is to have a loop to do certain things repeatedly if the user enters choices 1, 2, or 3, but to stop looping if the user enters choice 4.

    So what you have done is to put code for choices 1, 2, and 3 inside a loop, and then put the stuff for choice 4 outside the loop.

    Not so unreasonable; although, as I will point out below, you don't have to put the stuff for choice 4 outside the loop just because you don't want it repeated more than once.

    But now, you have made both a logical and a syntactic mistake.

    What's logically wrong is that the test condition for whether or not to loop "while (choice == 4)" is the opposite of what you want: you want it to STOP looping when choice==4, and to KEEP looping when choice is not equal to 4, right?

    You have also made a syntax error: the statement "do {/*stuff to be repeated */} while (/* some test condition */);" has to be terminated by a semicolon, which you have forgotten.

    Finally, why have you enclosed the stuff for choice==4 within curly braces? If you did that because you were trying to make it into a "else if (choice==4) {/* do this */}" block just like all the other "if/else if" blocks, then it would be logically better to include it inside the loop, along with all the other choices.

    Here's the key point about this: just because the body of the code is inside the loop, doesn't mean that it will be done repeatedly: even if you put the code for choice 4 inside the loop, as long as you correctly set the loop test condition to loop only while choice is NOT equal to 4, then the code for choice 4 will only be done once, and then the loop will stop after it sees that choice is no longer unequal to 4. I think this is what you are not understanding. You don't have to put code you don't want repeated OUTSIDE the loop.

    Good luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. For loop takes time
    By Shakti in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2004, 08:25 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM