Hi! I'm new to this forum and C++ programming and I am currently taking and introductory course to C++. I have a program that compiles fine but will not execute. I am posting my code and a sample input file....Please if anyone can get it to run or give me suggestions on why I am getting a segmentation fault/access violation when trying to run it.. I would greatly appreciate it

The assignment is:
This program requires you to input a student’s name and test scores from a file, average the scores, and determine a letter grade. In addition, you should calculate the average for the class. To receive full credit your program must use at least four functions. Pass parameters; do not use global variables.
Notes:
1. For each student, the input file will contain a first name, last name and 5 integer scores. Data items are separated by blanks.
2. You may assume that the data file is error free but you do not know the number of items in the file.
3. Calculate the average for each student by dropping the low score using the high score twice; round to the nearest int.
4. Output is to the screen. Print out last name, comma, first name followed by the five scores, the average, and a letter grade.
5. After all students are processed, print out the number of students, the class average, and the number of students in each of these three categories: excellent ( grade of A) satisfactory ( grade of B or C) Failing ( grade of D or F). Class average should be rounded to the nearest integer.
6. Use function prototypes. Declare the main function before you declare additional functions.


A sample text file is as follows:
Harry Hacker 93 94 89 77 97
Hilda Hacker 79 79 28 93 78
Horatio Hacker 77 78 79 80 45
Horaldo Hacker 68 68 62 70 68


Also I have tried to use the struct in a function and without structs and just arrays, we haven't learned classes yet, but I think my problem is with reading the file?

My code is:

#include <iostream>   
#include <fstream>  
#include <string>          
#include <iomanip>


using namespace std;

struct StudentData
{
       string firstname;
       string lastname;
       int tests[5];  
};

int gethighest(const StudentData list[], int listsize);
int getlowest(const StudentData list[], int listsize);
void CalcAverage(const StudentData list[], double Sum[], double Average[], int listsize);
void getGrade(const StudentData list[], double Average[], char Grade[], int listsize);
void printheading();
void PrintResults(const StudentData list[],double Average[], char Grade[], int listsize);



int main()
{
    int NoOfStudents = 200; 
    StudentData item;
    int students;
    StudentData list[students];    
    char Grade[students];
    double Sum[students];
    double Average[students];
  
    ifstream inFile;
    string filename;
    
    cout << "Enter the file name of student data: \t";
    cin >> filename;
    cout << endl;
    
    inFile.open(filename.c_str()); 
  
    if (!inFile) 
    {    
         cout << "Missing File \n\n";
         system ("pause");
         return 1;
    }

      int test;
      students = 1;
       
      inFile >> list[students].firstname >> list[students].lastname; 
      while (inFile) 
      {
      for(test = 0; test < 5; test++)
      {
        inFile >> list[students].tests[test];
      }
      students++;  
      }            
    CalcAverage(list, Sum, Average, students);     
    getGrade(list, Average, Grade, students);
    printheading();
    PrintResults(list, Average, Grade, students);
    

    
    cout << "The program is complete. " << endl;
    inFile.close();
    system ("pause");
    return 0;
}


void CalcAverage(const StudentData list[], double Sum[], double Average[], int listsize) 
{
     int row, col, test; 
     double lowest, highest;
     
     highest = static_cast<double>(gethighest(list, listsize));
     lowest = static_cast<double>(getlowest(list, listsize));
     
     for (int row = 0; row < listsize; row++)
     {
         Sum[row] = 0.0;
         for(test = 0; test < 5; col++)
         {
             Sum[row] = Sum[row] + static_cast<double>(list[row].tests[test]); 
         }
             Average[row] = (Sum[row] + highest + highest - lowest /(5));
      }
}
     
int gethighest(const StudentData list[], int listsize)
{
    int row, col;
    int highest;

    for (row = 0; row < listsize; row++)
    {
        highest = list[row].tests[0];
    for (col = 1; col < 5; col++)
    {
           if(highest < list[row].tests[col])
              highest = list[row].tests[col];
     }
    }
    return highest;
}


int getlowest(const StudentData list[], int listsize)
{
    int row, col;
    int lowest;

    for (row = 0; row < listsize; row++)
    {
        lowest = list[row].tests[0];   
    for (col = 0; col < 5; col++)
    {
           if(lowest > list[row].tests[col])
              lowest = list[row].tests[col];
    }
    }
    return lowest;
} 

void getGrade(const StudentData list[], double Average[], char Grade[], int listsize)
{

     int row;
     
     for (int row =0; row < listsize; row++)
     {
         
               if(Average[row] >= 90)

                    Grade[row] = 'A';

               else if (Average[row] >= 80)

                    Grade[row] = 'B';

               else if (Average[row] >= 70)

                    Grade[row] = 'C';

               else if (Average[row] >= 60)

                    Grade[row] = 'D';
               else

                  Grade[row] =  'F'; 
   }
}


void printheading()
{
     cout << "-*-*-*-*-*-*-*-*-*-*-*-*Classroom Results*-*-*-*"
          << "-*-*-*-*-*-*-*-*-*-*-*-*-*" << endl;
     cout << "------------------------------------------------"
          << "---------------------------------" << endl;
     cout << " Student        Test1   Test2   Test3   Test4   Test5"
          << "        Average     Grade" << endl;   
     cout << "------------------------------------------------"
          << "---------------------------------" << endl;
}     

void PrintResults(const StudentData list[], double Average[], char Grade[], int listsize)
{
        int row, col;
        double SumAverage = 0;
        double ClassAverage;
        int Excellent = 0,
            Satisfactory = 0,
            Failing = 0;
        int looper ;
        
    cout << "Programmer Name: Jane Hanger"
         << endl;
    cout << "Lab CRN: 11145"
         << endl; 
         
         
         for ( looper = 0; looper <  listsize ; looper++)
         {
             cout << list[looper].lastname << " , " << list[looper].firstname;
             for(int test = 0; test < 5; test++)
             {
               cout << list[looper].tests[test] << "     ";
             }  
             cout << Average[looper] << "     "
                  << Grade[looper] << endl;
             if (Grade[looper] == 'A')
                 Excellent++;
             else if (Grade[looper] == 'B' || Grade[looper] == 'C')
                  Satisfactory++;
             else if (Grade[looper] == 'D' || Grade[looper] == 'F')
                  Failing++;    
         }
         
         cout << "The number of students in the class: \t\t"
              << listsize << endl;
                  
         for(row = 0; row < listsize; row++)
         {
            SumAverage = SumAverage + Average[row];     
         }  
         ClassAverage = SumAverage / listsize;
         cout << "Class Average = " <<  static_cast<int>ClassAverage) << endl;
         cout << "Number of Students in each Category:\n\n " 
              << "Excellent:\t\t" << Excellent << "\n"
              << "Satisfactory:\t\t" << Satisfactory << "\n"
              << "Failing:\t\t" << Failing << "\n\n\n";
              
                 
}

Try these.

1) line 212 add "(" before ClassAverage
2) make deterministic initializations i.e in your initializations after main() use the following

int NoOfStudents = 200;
StudentData item;
int students;
StudentData list[0];
char Grade[0];
double Sum[0];
double Average[0];

3) Then you may try to keep adding elements to the list.
4) line 86 change col++ to test++
5) line 186 change loop condition to looper < listsize - 1

Max

Thank you! I made the changes you suggested, and the program now executes, but after it prompts the user for the input filename, and I enter the sample text file, it stops executing again and gives me a notice that it has stopped working. When I debug it, it tells me I have a segmentation fault/access violation again. I have my text file saved in the correct folder and have it saved as a text file. I've opened other text files that are predefined in the program, so I'm not sure where the fault comes from....any other advice on how to fix this??

I find your print () function towards the end of the program somewhat elusive due to earlier variable values you have used. Verify if the limits in the loops of this function have the correct values (they are not overshooting). Its pretty hard for me to tell if the limits are correctly set. I think most of the segmentation faults in your code are due to accessing array values which are beyond what is declared. The solution I posted earlier was a temporary solution to see if the initializations work for fixed list sizes. This was to make sure everything else in your code was alright. Check the limits and post your updates.

Max

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.