【发布时间】:2016-10-27 22:09:14
【问题描述】:
好的,伙计们,我对如何创建这个程序感到非常困惑。我写了一篇很好的文章,但是我陷入了停顿,我不知道我能做到这一点。那么这是我必须做的以下任务吗?
任务: 下载可通过 D2l 获得的文件gradeBook.txt。该文件包含未知数量的 学生。
这是文本文件中的内容:
梅兰妮 84 丹妮尔玛丽 90 尼古拉斯·劳尔 87 迈克尔 67 约书亚 46 亚历克西斯·米歇尔 90 贾里德 M。 55 安德烈斯·加布里埃尔 78 皮埃尔·路易斯 80 查尔斯 60 钦莲 95 卡洛斯·曼努埃尔 81
每个学生的信息以两行显示:
第一行表示学生的姓名——允许使用空格。
第二行显示学生在班级中的成绩。 编写一个程序,提示用户输入文件名并显示格式良好的 报告显示:
一个。班上成绩最高的学生 - 姓名和年级
b.班上成绩最低的学生 - 姓名和成绩
c。全班平均成绩
这是我目前所拥有的:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
fstream gFile;
int choice, grades;
string gb,students;
do {
cout << "Student Grade Book Info Program....\n";
cout << "\tPlease Select an Option: (1 or 2) \n" << endl
<< "\t1. Review Grades"
<< "\n\t2. Quit"
<< "\n\tChoose: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\n\tPlease Enter the Name of the File you wish to View the Grades for: " << endl;
cout << "\n\tAvailable Grade Books: gradeBook\n" <<
"\tType in the Grade Book you would like to view. ";
cin.ignore();
getline(cin, gb);
if (gb == "gradeBook") {
gFile.open("gradeBook.txt", ios::in);
if (gFile) {
cout << "\t" << setw(20) << left << "Student"
<< setw(30) << left << "Grade";
int counter = 0;
int highest = 0;
while (gFile >> students) {
getline(gFile, students);
gFile >> grades;
if (grades >= highest) {
highest = grades;
}
counter++;
}
cout << highest;
cout << endl;
}
else {
cout << "\tError... Could open gradeBook.txt ";
return 5;
}
}
else {
cout << "\n\tError there is no such entry in the system.\n" << endl;
}
case 2:
break;
}
} while (choice != 2);
return 0;
}
我需要显示我能够获得的最高成绩并显示它,但是如何在gradeBook.txt文件中显示成绩值之前的名称?
请帮助我吗?我好糊涂???
【问题讨论】:
-
如何拥有两个大小合适的数组——一个是 int 类型,一个是 string 类型。然后用 getline 等读入整个 txt 文件,将名称字符串和 int 交替推送到具有相同索引号的相应数组中。然后平均和谁得到最高或最低将容易得多。有意义。
-
在获取学生成绩之前,您还拥有学生姓名。添加一个名为highestStudent 的新变量,并在您升级最高的同一位置更新它。虽然我确实认为数组解决方案更有说服力。
-
问题是@Chris 我不能使用数组,我们在课堂上没有“覆盖”数组,所以我可以使用 dowhile、while 或 for 循环
-
是的,这让它变得更加困难。好的,让我再看看。
-
@Chris 谢谢你,我很困惑???
标签: c++ loops if-statement for-loop while-loop