Thread: Array problems

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    6

    Array problems

    Okay, I want to make a program that needs 5 grades to be input, and then show the 5 inputted grades after. The problem is when using a for loop statement for the scanf, it always loop 6 times.
    Any idea?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    
    int main()
    {
        int grade[4],average=0,cnt=0;
    
    
    
    
        for(cnt=0;cnt<=4;cnt++)
        {
            scanf("%d ", &grade[cnt]);
        }
    printf("\n\n");
        for(cnt=0;cnt<=4;cnt++)
        {
            printf("%d\n", grade[cnt]);
        }
     return 0;
    }
    Also, nevermind the average variable, I'll be using that after I solve it.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You only have room for 4 elements in grade. Make it 5.
    Also, the common idiom for a for loop is:
    Code:
    for (cnt = 0; cnt < 5; cnt++)
    instead of cnt <= 4.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    First, make sure your code is consistently indented - it should look more like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
     
    int main()
    {
        int grade[4],average=0,cnt=0;
     
        for(cnt=0;cnt<=4;cnt++)
        {
            scanf("%d ", &grade[cnt]);
        }
    
        printf("\n\n");
        
        for(cnt=0;cnt<=4;cnt++)
        {
            printf("%d\n", grade[cnt]);
        }
     
        return 0;
    }


    Code:
    int grade[4]
    This declares an array with 4 elements, not 5.

    Code:
    for(cnt=0;cnt<=4;cnt++)
    The more common idiom is to use less-than here:

    Code:
    for(cnt=0;cnt<5;cnt++)
    This allows you to used a named constant in lieu of a magic number:

    Code:
    #define NUM_GRADES  5
    
    // ...
    
    int grade[NUM_GRADES];
    
    // ...
    
    for(cnt=0; cnt<NUM_GRADES; cnt++)
        // ...


    Code:
    scanf("%d ", &grade[cnt]);
    That space after %d is causing the unexpected behavior you are seeing - try deleting it.



    Also, "conio.h" is a non-portable and non-standard header, so you shouldn't include it - especially since you're not using any functions from it.

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    6
    Oh, the space after the "%d" is the problem. Thank you! That fixed the problem.
    Any idea why space after %d made the difference?

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The pattern "%d " essentially asks scanf() to convert an integer, then consume any whitespace that follows it. If you provide the input interactively, and say press Enter after the number you supply, scanf() is still waiting to see if what you will supply next starts with spaces or tabs or newlines (as all those are counted as whitespace), because it will consume it if it does. It stops only when it sees a non-whitespace character (the next number if not last) or end-of-input.

    You can verify this yourself by supplying the last number with a letter following it. That too will stop scanf() looking for extra whitespace.

    The correct fix is, as stated, just using "%d" without the space.
    Last edited by Nominal Animal; 01-07-2016 at 06:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array problems
    By Annihilate in forum C Programming
    Replies: 4
    Last Post: 02-09-2011, 12:51 PM
  2. I/O and Array Problems
    By carrotcake1029 in forum C Programming
    Replies: 4
    Last Post: 04-08-2008, 04:53 AM
  3. Array problems
    By xmltorrent in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:26 PM
  4. Array problems
    By lzhaol in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2006, 02:52 PM
  5. problems with array
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 02-16-2002, 02:31 PM

Tags for this Thread