Thread: Need help finding error

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Need help finding error

    Hi there! I recently started to learn C Language.
    I managed to write program for getting sum and average score for english and spanish.

    But the problem is that i'm keep getting "source.c has stopped working" error.

    This is the program that i wrote
    Code:
    #include <stdio.h>
    
    FILE *file;
    
    
    void main(void)
    {
        int num[10], spa[10], eng[10], a, sum[10], avg[10];
        char name[10];
    
    
        printf("Stud # Name Spanish English Sum Average\n");
    
    
        file = fopen("Data.txt", "r");
        for (a = 0; a < 10; a++)
        {
            fscanf(file, "%d %s %d %d", &num[a], &name[a], &spa[a], &eng[a]);
            sum[a] = spa[a] + eng[a];
            avg[a] = sum[a] / 2;
            printf("%d %s %d %d %d %d", num[a], name[a], spa[a], eng[a], sum[a], avg[a]);
        }
        fclose(file);
    }
    And this is the "Data. txt"
    Code:
    1111 Guy1 82 91
    1112 Guy2 75 29
    1113 Guy3 88 75
    1114 Guy4 50 62
    1115 Guy5 70 56
    1116 Guy6 96 98
    1117 Guy7 75 90
    1118 Guy8 88 82
    1119 Guy9 82 88
    1120 Guy10 91 99
    As far as i know, this program can open "data.txt" but can go further than that.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    As far as i know, this program can open "data.txt" but can go further than that.
    How do you know that since you never check that the file opened successfully? By the way why the global variable for that FILE*? You should avoid global variables whenever possible.

    Also your program doesn't even compile for me and you really should be getting at least one warning that you need to fix.
    main.c|6|error: return type of ‘main’ is not ‘int’ [-Wmain]|
    main.c||In function ‘main’:|
    main.c|21|warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]|
    ||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
    If you're not getting any warnings you should see about increasing your compiler warning level. If you are getting some warnings, don't ignore the warnings, fix them.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help finding an error
    By ghozo in forum C Programming
    Replies: 3
    Last Post: 04-09-2016, 08:49 AM
  2. Please help me finding the error... :(
    By fredsilvester93 in forum C Programming
    Replies: 4
    Last Post: 01-04-2012, 01:15 AM
  3. need help in finding the error in my code
    By zeybek258388 in forum C Programming
    Replies: 5
    Last Post: 12-23-2011, 04:38 PM
  4. Help finding the error
    By C++Noob316 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2009, 01:16 PM
  5. Need help finding error
    By chorney in forum C++ Programming
    Replies: 13
    Last Post: 01-15-2007, 12:16 AM

Tags for this Thread