我似乎无法正确显示学生的名字?

时间:2016-10-28 00:09:13

标签: for-loop while-loop do-while

我曾求过帮助,但是你们帮我了,我想出了如何从档案中获得成绩。

这是我最初的问题。

作业:下载D2l提供的文件gradeBook.txt。该文件包含未知数量的学生。

这是文本文件中的内容:

Melanie 84 Danielle Marie 90 Nicolas Raul 87 Michael 67 Joshua 46 Alexis Michelle 90 Jared M. 55 Andres Gabriel 78 Pierre Louis 80 Charles 60 Cin Lian 95 Carlos Manuel 81

每位学生的信息分为两行:

第一行表示学生的姓名 - 允许空格。

第二行显示学生在班级中的成绩。编写一个程序,提示用户输入文件名,并显示格式正确的报告,显示:

一个。班上年级最高的学生 - 姓名和年级

湾班上年级最低的学生 - 姓名和年级

℃。班级平均成绩

这是更新的代码,但是,现在我似乎无法正确显示名称它只显示姓氏而第二个学生它甚至没有显示它?有人可以帮帮我吗?

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main() {

    fstream gFile;
    int choice, grades;
    string gb, students, highestStudent;
    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;
                    int lowest = 100;
                    string lowestStudent;
                    while (gFile >> students) {

                        getline(gFile, students);
                        gFile >> grades;

                        do {
                            if (grades < lowest) {
                                lowest = grades;
                                lowestStudent = students;

                            }

                            if (highest < grades) {
                                highest = grades;
                                highestStudent = students;
                            }

                        } while (grades > highest);
                    }
                    counter++;

                    cout << highestStudent << endl;
                    cout << highest << endl;
                    cout << lowestStudent << endl;
                    cout << lowest << endl;

                    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;
}

0 个答案:

没有答案