Thread: While loop trouble!!

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    55

    While loop trouble!!

    My problem has been fixed thankyou
    Last edited by Astra; 10-21-2006 at 05:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (iAgeError = 1);
    Use == for comparison.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    rookie mistake! thanks Salem!
    Last edited by Astra; 10-21-2006 at 10:59 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Okay i've changed it to == now on the final while statement.

    It works when i input a string of numbers, and exits the loop and program.
    HOWEVER! (dun dun dun..) If i enter an invalid number (ie. x) it says Invalid Number and asks me again (correct)
    but when i now input a number, it says Valid Number but asks me again! (when it should exit the program)

    Help me! I am assuming it is because the invalid value is still in the stream?
    Last edited by Astra; 10-21-2006 at 11:02 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 new.c
    new.c: In function `main':
    new.c:6: warning: 'iAgeError' might be used uninitialized in this function
    You need to set your valid flag to 0 when you detect success.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    after "printf ("Valid number: %ld.\n", iAge);" you need to put "iAgeError=0".
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Thanks, both of you!! I can finally make my program now, using this system of repeating do while's and checking for errors! You have saved my life by helping me learn this!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. trouble with a for loop with an if statement nested in
    By phoenix-47 in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2005, 04:24 PM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM