Thread: Problem with WHILE loop.

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Problem with WHILE loop.

    Code:
    while(grade[i] != -1)
    {
    		printf("GRADE: ");scanf("%f",&grade[i]);
    		i++;count++;
    }
    I don't get a compilation error, but if I enter in -1 as a number the program just continues to ask for the grade over and over again.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is grade an array of floats or ints?

    As you've incremented i, the test within the while won't be looking at the number you've just entered.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Problem with WHILE loop.

    Originally posted by kinghajj
    Code:
    while(grade[ i ] != -1)
    {
    		printf("GRADE: ");scanf("%f",&grade[ i ]);
    		i++;count++;
    }
    I don't get a compilation error, but if I enter in -1 as a number the program just continues to ask for the grade over and over again.
    You are reading a value into grade[ i ], you then increment i past the read in value, and compare the wrong grade (which is zero)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>and compare the wrong grade (which is zero)
    Where'd you get the idea it's zero? More than likely it's undefined (at least, that's my guess).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM