Thread: C Programming HELP!!

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    2

    C Programming HELP!!

    Can anybody explain to me why the second Array is getting a value from the first set??
    My keylist.txt file is simply "2 14 74 8 36 4 11" but for some reason it keeps assigning the '2' value to the second array.

    *********************************************
    Code:
    #include <stdio.h>
    
    int main(){
        int keylist[6], guess_list[6], game = 1;
        char file_name[30];
    
        FILE * ifp;
    
        for(int i = 0; i<7; i++){
            keylist[i] = 0;
            guess_list[i] = 0;
        }
    
    
        ifp = fopen("keylist.txt", "r");
    
        for(int i=0; i<7; i++){
            fscanf(ifp, "%d", &keylist[i]);
        }
    
        for(int i=0; i<7; i++){
            printf("%d, %d\n", keylist[i], guess_list[i]);
        }
    
    return 0;
    }
    *********************************************

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > for(int i = 0; i<7; i++)
    Because 7 is greater than 6.

    for(int i = 0; i<6; i++)
    is how you index an array of 6 elements.

    Otherwise known as a fence-post error.
    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
    Mar 2019
    Posts
    2
    Quote Originally Posted by Salem View Post
    > for(int i = 0; i<7; i++)
    Because 7 is greater than 6.

    for(int i = 0; i<6; i++)
    is how you index an array of 6 elements.

    Otherwise known as a fence-post error.
    But the array holds 7 numbers, 0,1,2,3,4,5,6..... it still is not working.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    No, the arrays hold 6 values.

    > But the array holds 7 numbers, 0,1,2,3,4,5,6..... it still is not working.
    7, 8, 100, a gigabyte - it's all the same (a buffer overrun).

    If you consider "works" as being the same as "doesn't crash" then you're in for some painful lessons.
    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.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I think that you are getting confused with how to use arrays in the c language.
    Code:
    int grape[5];
    This creates an array of 5 ints

    To access each element you have
    Code:
    apple = grape[0]; // 1st element
    
    apple = grape[1]; // 2nd element
    
    apple = grape[2]; // 3rd element
    
    apple = grape[3]; // 4th element
    
    apple = grape[4]; // 5th element
    If I were to try an access grape[5], this would be the sixth element...

    Arrays in C - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-01-2019, 12:27 PM
  2. Replies: 0
    Last Post: 02-01-2019, 12:22 PM
  3. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  4. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  5. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM

Tags for this Thread