【问题标题】:How can I output the student name before a desired value?如何在所需值之前输出学生姓名?
【发布时间】: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


【解决方案1】:

万一你仍然卡住了。作为练习,为 2 名相同年级的学生修改... ;)

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using std::cout;
using std::cin;

int main() {
    int choice, intBuffer, subtotalGrades, countGrades,
         average, highGrade, lowGrade = 100;
    choice = intBuffer = subtotalGrades = countGrades = average = highGrade = 0;
    std::string nameBuffer, studentHigh, studentLow;

    cout << "\tStudent Grade Book Info Program....\n";
    cout << "\tPlease Select an Option: (1 or 2) \n" << "\n"
        << "\t1. Review Grades"
        << "\n\t2. Quit"
        << "\n\tChoose: ";
    cin >> choice;
    if (choice == 2) {
        return 0;
    }
    else {
        std::ifstream gFile("gradeBook.txt", std::ios::in);
        if (!gFile) {
            cout << "\tError... Could not open gradeBook.txt\n";
            return 1;
        }
        while (gFile) {
            gFile.ignore();
            getline(gFile, nameBuffer);
            gFile >> intBuffer;
            if (intBuffer > highGrade) {
                highGrade = intBuffer;
                studentHigh = nameBuffer;
            }
            else if (intBuffer < lowGrade) {
                lowGrade = intBuffer;
                studentLow = nameBuffer;
            }
            subtotalGrades += intBuffer;
            countGrades++;
        }
        gFile.close();
    }
    average = subtotalGrades / countGrades;

    // Report section.
    cout << "\t" << std::setw(20) << "Student" << std::setw(30) << "Grade" << "\n";
    cout << "\t" << std::setw(20) << studentHigh << std::setw(30) << highGrade << "\n";
    cout << "\t" << std::setw(20) << studentLow << std::setw(30) << lowGrade << "\n";
    cout << "\t" << std::setw(20) << "Average" << std::setw(30) << average << "\n";

    return 0;
}

【讨论】:

  • 哦,哇,我已经知道如何获得两者的最小值,但不知道如何显示名称!这真的很有帮助,我不知道我可以对 a 文件使用 ignore() 函数,这怎么可能,我知道像忽略这样的函数会这样工作吗?
  • 嘿,克里斯,你能解释一下 ignore() 到底是做什么的吗???
  • 调用 gFile >> intBuffer 在 ifstream 对象中留下一个换行符“\n”。 gFile.ignore() 将其删除。放置在循环顶部,但也可以直接放置在 gFile >> intBuffer 调用之后。看看cplusplus.com/forum/articles/6046cplusplus.com/reference/istream/istream/ignore Cplusplus.com 是一个重要资源。
  • 非常感谢,这是一个很好的解释,非常感谢。我会调查的
【解决方案2】:

您无需将信息存储在程序中。

提示:您只需要存储以下内容:

  • 最高等级价值
  • 成绩最高的学生姓名
  • 最低等级值
  • 成绩最低的学生姓名
  • 成绩总和(使用平均值)
  • 成绩数量(按平均使用)

算法如下:

While reading name and grade succeeds do:
  compare to highest value -- if greater, replace highest grade with new grade
  compare to lowest value -- if lower, replace lowest grade with new grade.
  increment number of grades
  add grade to sum.

记得把名字和等级值一起替换。

如果您需要更多帮助,请尝试在互联网上搜索“stackoverflow c++ 最高成绩平均值”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多