Thread: having trouble using loops

  1. #31
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ahem, I suggested it first!

    Actually it's a well-known solution, so neither of us deserves praise . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, it got lost somewhere in the thread! I see it now.
    You outsmarted as all Actually, I didn't know there was such an optimization. Well, I don't really work with loops that way. Often... anyway... I think...

  3. #33
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    lmao.. found my stupid mistake..

    thanks guys..

  4. #34
    Registered User
    Join Date
    Nov 2007
    Posts
    8
    one more quick questions guys

    how can i make a program reject a negative value from being stored in array. e.g if a user inputs a negative value, how can i display a message that will tell the user to enter a positive value.

  5. #35
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Guess. Maybe something along the lines of
    1. Get a number from the user.
    2. If the number is negative:
      • Print an error message.
      • Get another number from the user.
      • Go to step 2.
    3. Viola! You have your number.

    That can be simplified, of course, for example if you use a do-while loop instead . . . but try that and see what happens.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. a little trouble with for loops
    By melee in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 01:46 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM