Segmentation Fault

I have looked at this for a long time and cannot make it work. After the menu selection, 1, 2, 3, and 5, give me a seg fault. Please help!


#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void PrintMenu();
int FindHighest(int scores[][10], int noOfStudent, int noOfTest);
int FindLowest(int scores[][10], int noOfStudent, int noOfTest);
float FindAverage(int scores[][10], int noOfTest);
int FindID(string names[], int size, string idToFind);
void PrintRecords(string names[], int scores[][10], int size, int noOfTest);

int main()
{
//declare local variables
int scores[100][10]; //student's scores
int noOfStudent; //number of students in the file
int noOfTest; //number of tests per student
string names[100]; //student id
int size; //number of students
string idToFind; //student id of a particular record
char mselection; //menu selection
int location; //location of scores


ifstream myIn;
string filename;

//Purpose of the program
cout << "This program displays the highest, lowest, average, student's " <<
"individual record, or the all the student's records based on menu selection." << endl;

//Read in the file
cout << "Enter input file name:" << endl;
cin >> filename;

//Open file
myIn.open(filename.c_str());

while(!myIn)
{
cout << "This is not a file." << endl;
cout << "Enter input file name." << endl;
cin >> filename;
myIn.open(filename.c_str());

}

//read in array

size = 0;
int n = 1; //rows
int m; //columns

myIn >> noOfTest;

for (m = 0; m < noOfTest; m++)
{
myIn >> names[m] >> scores[m][n];

while(myIn)
{
n++;
size++;
myIn >> names[m] >> scores[m][n];
}

}


noOfStudent = n;


do{
//Display the menu
PrintMenu();
cin >> mselection;

switch(mselection)
{ case '1':
location = FindHighest(scores, noOfStudent, noOfTest);
cout << names[location]<< " has the highest score of " << scores[location][noOfTest] << endl;
break;

case '2':
location = FindLowest(scores, noOfStudent, noOfTest);
cout << names[location]<< " has the lowest score of " << scores[location][noOfTest+2] << endl;
break;

case '3':
scores[n+1][m] = FindAverage(scores, size);
break;

case '4':
FindID(names, size, idToFind);
break;

case '5':
PrintRecords(names, scores, size, noOfTest);
break;

case '6':
cout << "Thanks for using the program." << endl;
break;

default:
cout << "This is an invalid menu selection." << endl;
break;
}
}while(mselection != '6');

return 0;
}

void PrintMenu()
{
//Purpose: Print the menu
//Pre-condition: None
//Post-condition: The menu is printed

cout << "Please choose from the following menu." << endl;
cout << "1. Find the highest average score and print the student name along with the score." << endl;
cout << "2. Find the lowest average score and print the student name along with the score." << endl;
cout << "3. Find the average of scores for each test." << endl;
cout << "4. Find the student record for the student ID entered by the user." << endl;
cout << "5. Print all student records." << endl;
cout << "6. Exit the program." << endl;
cout << "Enter your choice: " << endl;
}

int FindHighest(int scores[][10], int noOfStudent, int noOfTest)
{
//Purpose: Read from a file and find the highest average score
//Pre-condition: File has been opened successfully and scores have been read.
//Post-condition: Return the highest average score along with student id

string names[100];
int location;
int m;
int n;
for( n = 1; n < noOfStudent; n++)
{ int sum = 0;
for ( m = 0; m < noOfTest; m++)
{ sum = sum + scores[n][m];
}
scores[n][m] = sum / noOfTest;
}

int highest = scores[0][m];

for(n = 1; n < noOfStudent; n++)
{ if (scores[n][m] > highest)
{highest = scores[n][m];
location = n;
}
}

return location;
}

int FindLowest(int scores[][10], int noOfStudent, int noOfTest)
{
//Purpose: Read from a file and find the lowest average score
//Pre-condition: File has been opened successfully and scores have been read.
//Post-condition: Return the lowest average score along with student id

string names[100];
int location;
int m;
int n;
for( n = 1; n < noOfStudent; n++)
{ int sum = 0;
for ( m = 1; m < noOfTest; m++)
{ sum = sum + scores[n][m];
}
scores[n][m+1] = sum / noOfTest;
}

int lowest = scores[0][m+1];

for(n = 1; n < noOfStudent; n++)
{ if (scores[n][m+1] < lowest)
{lowest = scores[n][m+1];
location = n;
}
}

return location;
}

float FindAverage(int scores[][10], int noOfTest)
{
//purpose: read from a file and find the average for each test
//pre-condtion: file has been opened successfully and scores have been read.
//post-condtion: return the average of each test score
int m;
int n;

for( m = 1; m < noOfTest; m++)
{ int sum = 0;


int noOfStudent;
for ( n = 1; n < noOfStudent; n++)
{ sum = sum + scores[n][m];
}
scores[n+1][m] = sum / noOfStudent;
return scores[n+1][m];
}


}

int FindID(string names[], int size, string idToFind)
{
//purpose: read from a file and find the student record for the inputed id
//pre-condition: file has been opened successfully and scores have been read
//post-condtion: return the student's record for the student's id

int location;
int n;
int m;
int noOfTest;
int scores[100][10];

cout << "Please enter a student ID: " << endl;
cin >> idToFind;

for(n=1; n < size; n++)
{
if (names[n] == idToFind)
cout << names[n] << " ";
for(m=0; m < noOfTest; m++)
cout << scores[n][m] << " ";
cout << endl;
}
cout << "The student ID was not found." << endl;
}

void PrintRecords(string names[], int scores[][10], int size, int noOfTest)
{
//purpose: read from a file and print the records in that file
//pre-condtion: file has been opened successfully and scores have been read
//post-condtion: print the records in the file



int n;
int m;

for( n = 0; n < size; n++)
{
cout << names[n] << " ";
for(m =1; m < noOfTest; m++)
{
cout << scores[n][m] << " ";
}
cout << endl;
}

cout << endl;
}
Topic archived. No new replies allowed.