I have compiled and run all of the menu options, however only number 5 and 6 work.
1 and 2 give me a message that says Segmentation Fault when I run them. Help, please!
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void PrintMenu();
int FindHighest(int scores[], int size);
int FindLowest(int scores[], int size);
float FindAverage(int scores[], int size);
int FindID(string names[], int scores[], int size);
void PrintRecords(string names[], int scores[], int size);
int main()
{
//declare local variables
string names[100]; //student id
int scores[100]; // student's score
char mselection; // menu selection
int size; // number of student's records in the file
ifstream myIn;
string fileName;
int location;
float average;
string studentid;
//Purpose of the program
cout << "This program displays the highest, lowest, average, student score,"
<< " or the student record 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());
}
size = 0;
int n = 0 ;
while (myIn)
{ myIn >> names[n] >> scores[n];
n++;
size ++;
}
//Display the menu
PrintMenu();
cin >> mselection;
switch(mselection)
{ case '1':
FindHighest(scores, size);
cout << names[location] << "has the highest score of " << scores[location] << endl;
break;
case '2':
FindLowest(scores, size);
cout << names[location] << " has the lowest score of " << scores[location] << endl;
break;
case '3':
FindAverage(scores, size);
cout << "The average of the scores is " << average << endl;
break;
case '4':
FindID(names, scores, size);
cout << "The score of " << studentid << " is " << scores[n] << endl;
break;
case '5':
PrintRecords(names, scores, size);
break;
case '6':
cout << "Thanks for using the program." << endl;
return 0;
break;
default:
cout << "This is an invalid menu selection." << endl;
break;
}
}
void PrintMenu()
{
//Purpose: Print the menu
//Pre-condition: None
//Post-condition: The menu is printed
char mselection;
cout << "Please choose from the following menu." << endl;
cout << "1. Find the highest of scores stored in a file. " << endl;
cout << "2. Find the lowest of scores stored in a file." << endl;
cout << "3. Find the average of scores stored in a file." << endl;
cout << "4. Find the student score for the student ID entered by the user." << endl;
cout << "5. Print the student records." << endl;
cout << "6. Exit the program." << endl;
cout << " Enter your choice: " << endl;
}
int FindHighest(int scores[], int size)
{
//purpose: Read from a file and find the highest score
//pre-condition: file has been opened successfully
//post-condition: return the highest value
ifstream myIn;
string names[100];
int location;
int highest = scores[0];
for (int n=0; n < size; n++)
{ if (scores[n] < highest)
{highest = scores[n];
location = n;
}
}
return location;
}
int FindLowest(int scores[], int size)
{ //purpose: Read from a file and find the lowest score
//pre-condition: file has been opened successfully
//post-condition: return the lowest value
ifstream myIn;
int location;
int n=0;
int lowest = scores[0];
while(n < size)
{ if (scores[n] < lowest)
{ lowest = scores[n];
location = n;
}
n++;
}
return location;
}
float FindAverage(int scores[], int size)
{ //purpose: Read from a file and find the average of the scores
//pre-condition: file has been opened successfully
//post-condition: return the average of the scores
ifstream myIn;
float average;
int n=0;
int sum=0;
while(n < size)
{ sum = sum + scores[n];
n++;
}
average = sum / (n+1);
return average;
}
int FindID(string names[],int scores[], int size)
{ //purpose: Read from a file and find the student score for the inputed id
//pre-condition: file has been opened successfully
//post-condition: return the student's score for the student's id
string studentid;
ifstream myIn;
cout << "Please enter a student ID: " << endl;
cin >> studentid;
int n = 0;
while(n < size)
{ if (names[n] == studentid)
{return scores[n];
}
else
cout << "The student ID was not found." << endl;
}
}
void PrintRecords(string names[], int scores[], int size)
{ //purpose: read from a file and print the records in that file
//pre-condition: file has been opened successfully
//post-condition: print the record in the file