【发布时间】:2019-05-12 21:46:59
【问题描述】:
我正在编写一个程序来显示来自文本文件的学生成绩报告。我在正确列出学生的课程时遇到问题,因为第一门课程被跳过并替换为文件下一行的第一个字符串。我也只能显示文件中一个学生的结果,而不是所有学生的结果。
我不确定如何解决数据中跳过的课程。我尝试了几种不同的方式调整开始循环,但我要么得到我目前拥有的结果,要么得到一个只有一个学生的连续循环
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string ID, lastName, firstName;
char midInt, sex, letterGrade;
string phoneNum, classLevel, courseName;
int regCourses, courseCredit;
double numGrade, totalNumGrade;
int credits2018, credits2019;
double cumGPA2018, cumGPA2019;
int i;
ofstream out_stream;
ifstream in_stream;
in_stream.open("C:/Users/mss3s_000/Desktop/Stud.txt");
if (in_stream.fail()) {
cout << "Input file opening fail.\n";
exit(1);
}
if (!in_stream.eof()) { //I think the issue with not reading all the data is with this loop, but I'm not sure.
while (in_stream >> ID >> lastName >> firstName >> midInt >> phoneNum >> sex >> classLevel >> credits2018 >> cumGPA2018 >> regCourses >> courseName >> courseCredit >> letterGrade) {
switch (sex) {
case 'M':
/*sex = "Male";*/
break;
case 'F':
/*sex = "Female";*/
break;
}
if (classLevel == "FR")
classLevel = "Freshman";
else if (classLevel == "SO")
classLevel = "Sophomore";
else if (classLevel == "JR")
classLevel = "Junior";
else if (classLevel == "SR")
classLevel = "Senior";
if(letterGrade == 'A')
numGrade = 4.0;
else if(letterGrade == 'B')
numGrade = 3.0;
else if(letterGrade == 'C')
numGrade = 2.0;
else if(letterGrade == 'D')
numGrade = 1.0;
else if(letterGrade == 'F')
numGrade = 0.0;
cout << "**************************************************" << endl;
cout << "Student ID number: " << ID << endl;
cout << "Name: " << lastName << ", " << firstName << " " << midInt << endl;
cout << "Tel: " << phoneNum << endl;
cout << "Gender: " << sex << endl;
cout << "Class Level: " << classLevel << endl;
if (regCourses == 0) {
cout << "Registration of Spring 2019: " << "No" << endl;
cout << "**************************************************" << endl;
}
cout << "Registration of Spring 2019: " << "Yes" << endl;
cout << "\n Unofficial Report Card" << endl;
cout << "COURSE CREDITS GRADE" << endl;
cout << "====== ======= ======" << endl;
credits2019 = 0;
cumGPA2019 = 0.0;
for (i = 1; i <= regCourses; i++) { //I thought this loop would show all the courses but it is skipping the first course
in_stream >> courseName;
in_stream >> courseCredit;
in_stream >> letterGrade;
if (letterGrade != 'W') {
credits2019 += courseCredit;
totalNumGrade += (courseCredit * numGrade);
cumGPA2019 = totalNumGrade / credits2019;
}
cout << courseName << " " << courseCredit << " " << letterGrade << endl;
out_stream << courseName << " " << courseCredit << " " << letterGrade << endl;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\nCredits for Spring 2019: " << credits2019 << endl;
cout << "GPA till Fall 2018: " << cumGPA2018 << endl;
cout << "Total Credits: " << (credits2018 + credits2019) << endl;
cout << "New Cumulative GPA: " << ((credits2019 * cumGPA2019) + (credits2018*cumGPA2018)) / (credits2019 + credits2018) << endl;
cout << "**************************************************\n" << endl;
}
}
in_stream.close();
return 0;
}
我希望在课程标题下列出所有课程,并在文件中显示所有学生的信息。
【问题讨论】:
-
您的数据是什么样的?您是否有机会将其减少很多以仅显示您遇到的问题?阅读minimal reproducible example 以获得一些指示。
-
如果您的输入是基于行的,则使用
std::getline一次读取一行,然后使用std::istringstream读取该行的数据。这种问题在这个网站上被问了很多很多次。我相信一些搜索将为您带来解决方案。 Here is one I wrote a while ago