Thread: Need help on my code.

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Question Need help on my code.

    I run it with my compiler and the code crashed without any error. Anything wrong with this code? Please help me out. Thank you guys.

    Code:
    /*
     * A sample program to read-from and write-to files. Also shows how to
     * send a file as a parameter and display error messages to standard
     * error.
     */
    
    #include "test.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    
    
    int
    main ( void )
    {
       void rmNl ( char s[] );
       int lineWithBlanks ( char *s );
       FILE *fin, *fout;
       STUDENT stuArr[MAX_STUDENTS];
       int numItems = 0; // counter to keep track of items processed
       int i, FinalGrade;
       
       /* Open file to read input data */
       if ( ( fin = fopen ( "input.dat", "r" ) ) == NULL ) {
          fprintf ( stderr, "Could not open file input.dat\n" );
       }
       else {
          for(i=1;i<=MAX_STUDENTS;i++)   
          fscanf(fin,"%s%s\n%d\n%d%d%d%d%d\n%d%d%d\n",stuArr[i].fName,stuArr[i].lName,&stuArr[i].id,stuArr[i].hw[0],&stuArr[i].hw[1], &stuArr[i].hw[2],&stuArr[i].hw[3], &stuArr[i].hw[4],&stuArr[i].exm[0],&stuArr[i].exm[1],&stuArr[i].exm[2]);
    
       }
       fclose(fin);
       
       /* Open file for writing output */
       if ( ( fout = fopen ( "output.dat", "w" ) ) == NULL ) {
          fprintf ( stderr, "Could not open file output.dat\n" );
       }
       else {
        fprintf(fout, "%s\n", "==============================================");
        fprintf(fout, "%s\n", "              PRINTING ALL RECORDS            ");
        fprintf(fout, "%s\n", "==============================================");
        fprintf(fout, "%-10s%-10s%-8s%-8s%-8s%s\n","Student","Name","Id","HAvg","EAvg","Grade"); 
        
           while (!feof(fin))   {
                 
              fprintf( fout,"%-10s%-10s%-8d%-8.2f%-8.2f",stuArr[i].fName,stuArr[i].lName,&stuArr[i].id,stuArr[i].hwAvg,&stuArr[i].exmAvg);
          
              stuArr[i].hwAvg = (stuArr[i].hw[0] + stuArr[i].hw[1] + stuArr[i].hw[2] + stuArr[i].hw[3] + stuArr[i].hw [4]) / 5;
              stuArr[i].exmAvg = (stuArr[i].exm[0] + stuArr[i].exm[1] +  stuArr[i].exm[2]) / 3;
              FinalGrade = (stuArr[i].hwAvg * 0.4) + (stuArr[i].exmAvg * 0.6);
       
              if (FinalGrade >= 85 && FinalGrade <= 100.00) stuArr[i].grade = 'A';
                 else if (FinalGrade >= 75 && FinalGrade <= 84) stuArr[i].grade = 'B';
                 else if (FinalGrade >= 60 && FinalGrade <= 74) stuArr[i].grade = 'C';
                 else if (FinalGrade >= 50 && FinalGrade <= 59) stuArr[i].grade = 'D';
                 else if (FinalGrade < 50) stuArr[i].grade == 'F';
              fprintf( fout,"%c\n", stuArr[i].grade);
           }
       }   
    
       fclose(fout);    
       return 0;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    MAX_STUDENTS isn't declared. Show your struct, also you should try compiling a .c not a .cpp cause that's not a valid struct declaration.
    Sent from my iPad®

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Your loop goes from 1 to MAX_STUDENTS, C arrays are indexed from 0 through the number of elements - 1. As a result, you're accessing beyond the end of your array.

    I can't swear this is why the program errors out, but it's a start.
    Insert obnoxious but pithy remark here

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    I put the structure in test.h which code is being posted below. I put .c for the file. I'll try to fix the problem u said, filker0.

    Code:
    #define NAME_LEN 30
    #define NUM_HW 5 
    #define NUM_EXM 3
    #define MAX_STUDENTS 80
    #define BUFLEN  80
    
    typedef struct STUDENT
       {
          char fName[NAME_LEN];
          char lName[NAME_LEN];
          int id;
          int hw[NUM_HW];
          int exm[NUM_EXM];
          float hwAvg;
          float exmAvg;
          char grade;
       } STUDENT;

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Get rid of the STUDENT before the last semicolon, and what filker0 said was the problem is correct. It's also what I imagined would be it.

    You can't be compiling this as a C file because you aren't declaring an object correctly.

    Code:
    struct foo {};
    
    int main() {
      struct foo one;  // Is C legal
      foo two;         // Not C legal
    
      return 0;
    }
    Sent from my iPad®

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    After I carefully editted and examined my code, I still have some warning and error in my code such as:

    grades.c:12:19: conio.h: No such file or directory
    grades.c: In function `main':
    grades.c:36: warning: format argument is not a pointer (arg 6)
    grades.c:58: warning: int format, pointer arg (arg 5)
    grades.c:58: warning: double format, pointer arg (arg 7)
    grades.c:73:32: missing terminating " character
    grades.c:74: error: syntax error before "exists"
    grades.c:74: error: stray '\' in program
    grades.c:74:9: missing terminating " character
    grades.c:69: warning: statement with no effect

    Please help me out on those errors and warnings, and here is my code:

    grades.c
    Code:
    
    #include "grades.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    
    
    int
    main ( void )
    {
       FILE *fin, *fout;
       STUDENT stuArr[MAX_STUDENTS];
       int i = 0, FinalGrade;
       
       /* Open file to read input data */
       if ( ( fin = fopen ( "grades.in", "r" ) ) == NULL ) {
          fprintf ( stderr, "Could not open file grades.in\n" );
          fclose (fin);
          exit ( -1 );
       }
       else {
               
          while (i < MAX_STUDENTS) {   
          fscanf(fin,"%s%s\n%d\n%d%d%d%d%d\n%d%d%d\n",stuArr[i].fName,stuArr[i].lName,&stuArr[i].id,stuArr[i].hw[0],&stuArr[i].hw[1], &stuArr[i].hw[2],&stuArr[i].hw[3], &stuArr[i].hw[4],&stuArr[i].exm[0],&stuArr[i].exm[1],&stuArr[i].exm[2]);
          fclose (fin);
          }
       }
       
       /* Open file for writing output */
       if ( ( fout = fopen ( "grades.out", "w" ) ) == NULL ) {
          fprintf ( stderr, "Could not open file grades.out\n" );
          fclose ( fout);
          exit ( -1 );
       }
       else {
    
    
          fprintf(fout, "%s\n", "==============================================");
          fprintf(fout, "%s\n", "              PRINTING ALL RECORDS            ");
          fprintf(fout, "%s\n", "==============================================");
          fprintf(fout, "%-10s%-10s%-8s%-8s%-8s%s\n","Student","Name","Id","HAvg","EAvg","Grade"); 
        
          while (!feof(fin))  {  
              if (strcmp(stuArr[0].fName,stuArr[1].fName)!= 0) {  
                 fprintf( fout,"%-10s%-10s%-8d%-8.2f%-8.2f",stuArr[i].fName,stuArr[i].lName,&stuArr[i].id,stuArr[i].hwAvg,&stuArr[i].exmAvg);
          
                 stuArr[i].hwAvg = (stuArr[i].hw[0] + stuArr[i].hw[1] + stuArr[i].hw[2] + stuArr[i].hw[3] + stuArr[i].hw [4]) / 5;
                 stuArr[i].exmAvg = (stuArr[i].exm[0] + stuArr[i].exm[1] +  stuArr[i].exm[2]) / 3;
                 FinalGrade = (stuArr[i].hwAvg * 0.4) + (stuArr[i].exmAvg * 0.6);
       
                    if (FinalGrade >= 85 && FinalGrade <= 100.00) stuArr[i].grade = 'A';
                    else if (FinalGrade >= 75 && FinalGrade <= 84) stuArr[i].grade = 'B';
                    else if (FinalGrade >= 60 && FinalGrade <= 74) stuArr[i].grade = 'C';
                    else if (FinalGrade >= 50 && FinalGrade <= 59) stuArr[i].grade = 'D';
                    else if (FinalGrade < 50) stuArr[i].grade == 'F';
                    fprintf( fout,"%c\n", stuArr[i].grade);
              }
              else {
                 fprintf ( stderr, "Could not add entry of %s%s_ErrorInAcct with id: %d - id already exists\n", stuArr[i].fName, stuArr[i].lName, stuArr[i].id);
                 fprintf ( stderr, "Incorrect number of entries for the name field of %s_ErrorInAcct", stuArr[i].fName);
                 fprintf ( stderr, "Incorrect number of entries for id field of %s_ErrorInAcct", stuArr[i].fName);
                 fprintf ( stderr, "Incorrect number of entries for homework field of %s_ErrorInAcct", stuArr[i].fName);
                 fprintf ( stderr, "Incorrect number of entries for exam field of %s_ErrorInAcct", stuArr[i].fName);        
              }   
           }
       fclose (fout);
       }
    return 0;
    }
    grades.h
    Code:
    #define NAME_LEN 30
    #define NUM_HW 5 
    #define NUM_EXM 3
    #define MAX_STUDENTS 80
    
    typedef struct STUDENT
       {
          char fName[NAME_LEN];
          char lName[NAME_LEN];
          int id;
          int hw[NUM_HW];
          int exm[NUM_EXM];
          float hwAvg;
          float exmAvg;
          char grade;
       }STUDENT;

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    The warnings tell you exactly where to go....

    remove #include <conio.h> it's not standard and you do not use it at all.

    Missing an &
    Code:
    fscanf(fin,"%s%s\n%d\n%d%d%d%d%d\n%d%d%d\n",stuArr[i].fName,stuArr[i].lName,&stuArr[i].id,&stuArr[i].hw[0],&stuArr[i].hw[1], &stuArr[i].hw[2],&stuArr[i].hw[3], &stuArr[i].hw[4],&stuArr[i].exm[0],&stuArr[i].exm[1],&stuArr[i].exm[2]);
    Remove the 2 &s
    Code:
    fprintf( fout,"%-10s%-10s%-8d%-8.2f%-8.2f",stuArr[i].fName,stuArr[i].lName,&stuArr[i].id,stuArr[i].hwAvg,&stuArr[i].exmAvg);
    Should be = not ==
    Code:
    else if (FinalGrade < 50) stuArr[i].grade == 'F';

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    Get rid of the STUDENT before the last semicolon, and what filker0 said was the problem is correct. It's also what I imagined would be it.

    You can't be compiling this as a C file because you aren't declaring an object correctly.

    Code:
    struct foo {};
    
    int main() {
      struct foo one;  // Is C legal
      foo two;         // Not C legal
    
      return 0;
    }
    It's a typedef. While it's not a good idea to use the same name like they did, it is C. Consider:
    Code:
    typedef struct foo { ... } bar;
    We have defined that 'bar' is in essence an alias for 'struct foo'. Thus, we can now do:
    Code:
    struct foo x; /* x is an instance of struct foo */
    bar y; /* y is an instance of bar, which is really a 'struct foo' */

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Hmm... I see.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM