Segmentation Fault (Core Dumped)

This is a discussion on Segmentation Fault (Core Dumped) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int numstudents, totalstudents = 0, gradesum, totalsum = 0, highscore = 0, totalhs = ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Segmentation Fault (Core Dumped)

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            int numstudents, totalstudents = 0, gradesum, totalsum = 0, highscore = 0, totalhs = 0, cavg, highestavg = 0, totalavg, class, x, highavgclass;
            char fihs, sihs;
    
    
            for(class = 1; class < 5; class++)                                                                      //Repeats for the # of classes
            {
                    totalhs = 0;
                    printf("How many students for this class?:");
                    scanf("%d", &numstudents);
    
    
                            for(x = 1; x <= numstudents; x++)                                                       //Repeated Input per class
                            {
                                    char fi, si;
                                    int tempscore;
    
    
                                    printf("\nEnter data for student %d.  (F.I., S.I., score b/w 0 and 100):", x);
                                    scanf("%c, %c, %d", fi, si, tempscore);
    
    
                                    FILE *ofp;
                                    ofp = fopen("errorfile.txt", "w");
    
    
                                    if(0 < tempscore < 100)
                                            gradesum += tempscore;
                                    else{
                                            fprintf(ofp, "Student Inititals:%c.%c.\nScore:%d", fi, si, tempscore);
                                            printf("Error, score not in range.  Reinput student information");
                                            x--;
                                            continue;
                                    }
                                            if(tempscore > highscore){
                                            fihs = fi;
                                            sihs = si;
                                            highscore = tempscore;
                                    }
                            }
    
    
                    cavg = gradesum / numstudents;
                    printf("\nClass Average: %d\nStudent with Highest Score:%c.%c.\nStudent's Score:%d", cavg, fihs, sihs, highscore);
    
    
                    totalsum += gradesum;
                    totalstudents += numstudents;
                    if(cavg > highestavg){
                            highestavg = cavg;
                            highavgclass = class;
                    }
                    if(highscore > totalhs)
                            totalhs = highscore;
    
    
            }
    
    
            totalavg = totalsum / totalstudents;
    
    
            printf("\nTotal Average for All Classes: %d\nHighest Class Average: Class %d with a %d Average\nHighest Overall Grade: %c.%c. with a %d",
    totalavg, highavgclass, highestavg, fihs, sihs, totalhs);
    
    
    return 0;
    }
    I run the program and right after outputting "Enter data for student 1 .................." i get the seg fault. I'm pretty darn new to C so I have no idea what this means, but since its my first time with file input and output i think it might be that, but I commented those parts out and still got the error. Help please?

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Scanf takes the addresses of the variables, rather than the variables themselves:

    Code:
    scanf( "%d", number ); /* WRONG */
    scanf( "%d", &number ); /* BETTER */
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Or use more compiler options.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:24: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘int’
    bar.c:24: warning: format ‘%c’ expects type ‘char *’, but argument 3 has type ‘int’
    bar.c:24: warning: format ‘%d’ expects type ‘int *’, but argument 4 has type ‘int’
    bar.c:31: warning: comparisons like ‘X<=Y<=Z’ do not have their mathematical meaning
    bar.c:24: warning: ‘si’ may be used uninitialized in this function
    bar.c:24: warning: ‘fi’ may be used uninitialized in this function
    bar.c:24: warning: ‘tempscore’ may be used uninitialized in this function
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    You repeatedly open a file for writing within a loop with no intervening calls to close the file prior to the next open call for the same file. I believe you mean to open the file prior to the nested loop and you also need to add a call to close the file after the loop.

    Usually averages are floating-point values though your code perhaps does not need to make this distinction.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 02:18 PM
  2. Help with 'Segmentation fault (core dumped)' error?
    By Von Fuzzball in forum C Programming
    Replies: 12
    Last Post: 05-03-2011, 03:29 PM
  3. Replies: 7
    Last Post: 03-17-2011, 11:55 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 04:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21