Thread: while loop with array

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693

    while loop with array

    why wont this loop exit after I have typed a negative number?

    Code:
    int main()
    {
    int scores[10]={0,0,0,0,0,0,0,0,0,0};
    int ids[10]={0,0,0,0,0,0,0,0,0,0};
    int i;
    
    for(i=0;i<10;i++)
    printf("scores are %i\nand IDS are %i\n\n", scores[i], ids[i]);
    
    i=0;
    
    while(ids[i]>=0)
    {
                    scanf("%i", &ids[i]);
             printf("scores are %i\nand IDS are %i\n\n", scores[i], ids[i]);
             i++;      
                   
                   }
    
        system("pause");
     return 0;   
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Because at the end of the loop, you increment i, so it's no longer the index of the negative number you typed.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    You are checking the currently what's in the array not the input value.

    you're just replacing the ids[i] till you a seg fault because there is no bound

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    so should i increment 'i' before I input?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Nope. In C, if you have an array with 10 elements, the valid indexes are 0 to 9. This is how you are currently storing the elements, but the problem is, after you store the element, you check the next element to see if it's less than zero, so you miss the negative number you entered. Try something like:
    Code:
    i = 0;
    do {
        if (i == 10)
            break;  // don't allow more that 10 numbers, otherwise there's an overflow
        scanf("%i", &ids[i]);
        printf("scores are %i\nand IDS are %i\n\n", scores[i], ids[i]);
    } while (ids[i++] >= 0);

  6. #6
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    A more straightforward solution that avoids the use of the break statement is to to alter the while statement's conditional test:
    Code:
        i = 0;
    
        while ( i < 10 ) {
            scanf( "%i", &ids[i] );
            printf( "scores are %i\nand IDS are %i\n\n", scores[i], ids[i] );
            i++;
        }
    Kevin

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    A more straightforward solution that avoids the use of the break statement is to to alter the while statement's conditional test:
    But then he loses his exit on -1...
    Code:
        int ids[10] = {0};  // intialize to 0 and avoid influence on outcome
    
        i = 0;
    
        while ( i < 10 ) {
            scanf( "%i", &ids[i] );
            if (ads[i] < 0)
                { ads[i] = 0;
                   break; }
            printf( "scores are %i\nand IDS are %i\n\n", scores[i], ids[i] );
            i++;
        }
    Plus this still doesn't do anything with the scores array....

  8. #8
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Indeed, but my comment focused solely on the conditional test expression and the elimination of the break statement proposed earlier, not on the program as a whole.

    Kevin

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The only problem is, he needs to stop the loop if it's full or if he's entered a negative number. I suppose if you wanted to avoid the use of a break statement, you could do something like the following:
    Code:
    int i, arr[MAX];
    i = 0;
    do {
        printf("Enter a number, negative to stop: ");
        scanf("%d", &arr[i]);
        i++;
    } while (i < MAX && i > 0 && arr[i-1] >= 0);
    EDIT: What Tater said...I'm slow today.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    Indeed, but my comment focused solely on the conditional test expression and the elimination of the break statement proposed earlier, not on the program as a whole.

    Kevin
    So... now you're telling us you're narrow minded?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While loop with array
    By Lego_TeCh in forum C Programming
    Replies: 3
    Last Post: 08-15-2009, 09:42 AM
  2. array and loop help
    By hockey1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 09:47 PM
  3. Help Array and Loop
    By Mike1982 in forum C Programming
    Replies: 5
    Last Post: 10-17-2007, 02:41 PM
  4. Need help on for loop and array
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2003, 10:17 AM
  5. Employee array/loop help
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-21-2002, 02:01 PM