我正在为我的 CS 课程做一个实验室,包括打开三个文件并根据获得的数据向第四个文件提供输出。我遇到了一些问题(我对 C++ 相当生疏),代码让我头疼。我遇到了字符串错误和分段错误
#include <iostream>
#include <iomanip>
#include <fstream>
#include <list>
#include <vector>
#include <algorithm>
using namespace std;
//Necessary objects/structures
struct student { //create a structure to contain all data consistent to the student
string ID, name, address, phone;
bool is_In_Query;
list<string> class_Grade; //Dynamic list to contain all letter grades since we don't know how many there will be
};
//Prototypes
bool readStudentFile(string, vector<student>);
bool readGradeFile(string, vector<student>);//returning a boolean will report if read was a success
bool readQueryFile(string, vector<student>);
void outputReportFile(string, vector<student>);
float calculateGPA(student); //Pass all calculations and reading to functions to simplify and manage code
void reportFileError(int); //Reports a file error. "1" is Student, "2" is Grade, "3" is Query
int isSame(string, vector<student>);
int main(int argc, const char* argv[])
{
vector<student> studentList; //Allow a range in number of students
if (!readStudentFile("students.txt", studentList)){
if (!readGradeFile("grades.txt", studentList)){
if(!readQueryFile("query.txt", studentList))
outputReportFile("report.txt", studentList);
else
reportFileError(3);
}
else
reportFileError(2);
}
else
reportFileError(1);
return 0; //Termination
/*
if (!readStudentFile(argv[1], studentList)){
if (!readGradeFile(argv[2], studentList)){
if(!readQueryFile(argv[3], studentList))
outputReportFile(argv[4], studentList);
else
reportFileError(3);
}
else
reportFileError(2);
}
else
reportFileError(1);
return 0; //Termination
*/
}
bool readStudentFile(string fileToRead, vector<student> studentInfo)
{
ifstream inFile;
int index = 0;
//inFile.open(fileToRead.c_str()); Original opening line
inFile.open(fileToRead.c_str(), ifstream::in);
if (!inFile.good())
return false;
else while (!inFile.eof()) { //Read in order
getline(inFile, studentInfo[index].ID);
getline(inFile, studentInfo[index].name);
getline(inFile, studentInfo[index].address);
getline(inFile, studentInfo[index].phone);
++index; //We use a prefix to insure that index is incremented BEFORE the jump back to the else-while
}
inFile.close();
return true;
}
bool readGradeFile(string fileToRead, vector<student> studentInfo)
{
ifstream inFile;
string temp;
int sameID = 0;
inFile.open(fileToRead.c_str(), ifstream::in);
if (inFile.fail())
return false;
else while (!inFile.eof()) { //Got more? Ok, let's go!
cin.ignore(); //The class ID is not necessary for our computation
getline(inFile, temp); //Obtain student ID
sameID = isSame(temp, studentInfo); //Find it in our list of IDs
if (sameID != -1){ //If there is a match, get letter grade
getline(inFile, temp);
studentInfo[sameID].class_Grade.push_back(temp);
}
//No match? Ignored.
}
inFile.close();
return true;
}
bool readQueryFile(string fileToRead, vector<student> studentInfo)
{
ifstream inFile;
int sameID = 0;
string temp;
inFile.open(fileToRead.c_str(), ifstream::in);
if (inFile.fail())
return false;
else while (!inFile.eof()) {
getline(inFile, temp);
sameID = isSame(temp, studentInfo); //We get an ID, compare it
if (sameID != -1) //If it's in there, we'll flag it to not report a GPA of 0 due to a bug
studentInfo[sameID].is_In_Query = true;
}
inFile.close();
return true;
}
void outputReportFile(string fileToWrite, vector<student> studentInfo)
{
ofstream outFile;
outFile.open(fileToWrite.c_str(), ifstream::out);
outFile.setf(ios::fixed | ios::showpoint);
for(int x = 0; x < studentInfo.size(); x++){
if (studentInfo[x].is_In_Query){
outFile << setw(12) << studentInfo[x].ID;
outFile << fixed << setprecision(2) << setw(7) << calculateGPA(studentInfo[x]);
outFile << studentInfo[x].name;
}
}
cout << "Finished.\n";
}
float calculateGPA(student scholar)
{
float gpa = 0;
if (scholar.class_Grade.size() == 0)
return 0;
else {
for(list<string>::iterator it = scholar.class_Grade.begin(); it != scholar.class_Grade.end(); ++it)
{
if(*it == "A")
gpa += 4.0;
else if (*it == "A-")
gpa += 3.7;
else if (*it == "B+")
gpa += 3.4;
else if (*it == "B")
gpa += 3.0;
else if (*it == "B-")
gpa += 2.7;
else if (*it == "C+")
gpa += 2.4;
else if (*it == "C")
gpa += 2;
else if (*it == "C-")
gpa += 1.7;
else if (*it == "D+")
gpa += 1.4;
else if (*it == "D")
gpa += 1.0;
else if (*it == "D-")
gpa += 0.7;
else if (*it == "E")
gpa += 0;
}
}
gpa = gpa / scholar.class_Grade.size();
return gpa;
}
int isSame(string id, vector<student> studentInfo)
{
for (int x = 0; x < studentInfo.size(); x++)
{
if (id.compare(studentInfo[x].ID) == 0)
return x;
}
return -1;
}
void reportFileError(int report_num)
{
switch(report_num){
case 1 :
cout << "No valid student file" << endl;
break;
case 2 :
cout << "No valid grade file" << endl;
break;
case 3 :
cout << "No valid query file" << endl;
break;
}
}
(我希望我把它正确地放入代码块中)
如果我尝试使用预定义的文件名或 CLI 参数中的一个,我会得到相同的错误。我猜这要么与我制作的结构有关,要么与我过度使用的复杂性有关。
所有文件都包含在我的代码和可执行文件的目录中