-3

试图编写一个需要读入一些数据的程序。它不会像应有的那样打开文件并读取数据。它似乎与getData功能有关。

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

using namespace std;

const int NUM_STUDENTS = 20;
const int NUM_GRADES = 5;

struct StudentType {
    string name;
    int grades[NUM_GRADES];
    int average;
    char letterGrade;
};

void getData(StudentType students[]);
void printGrades(StudentType students[]);
void assignGrades(StudentType students[]);
string convert(string name);
float findHighest(StudentType students[]);
void printHighest(StudentType students[]);

int main()
{
    StudentType students[20];

    getData(students);
    printGrades(students);

    return 0;
}

void getData(StudentType students[]) {

    string name, filename;
    ifstream fin;

    cout << "Please Enter the file name: ";
    cin >> filename;
    fin.open(filename.c_str());

    while (!fin) {
        cout <<"Could not open " << filename << ", please re-enter the filename" << endl;
        cout << "Please Enter the file name: ";
        cin >> filename;
        fin.open(filename.c_str());
    }

    for (int index = 0; index < NUM_STUDENTS; ++index) {
        int total = 0;
        getline(fin, name);
        students[index].name = convert(name);

        for (int i = 0; i <NUM_GRADES; ++i) {
            fin >> students[index].grades[NUM_GRADES];
            fin.ignore(INT_MAX, '\n');
            total = total + students[index].grades[NUM_GRADES];
        }
        students[index].average = static_cast<float>(total)/NUM_GRADES;
    }
    fin.close();
}

string convert(string name) {
    string s;
    string:: size_type x;
    x = name.find(".");

    if (x != string::npos) {
        ++x;
    }
    else {
        x = name.find( " ");
    }

    s = name.substr(x+1, name.size()) + ", " + name.substr(0,x);

    return s;
}

void printGrades(StudentType students[]) {
    for (int index = 0; index < NUM_STUDENTS; ++index) {
        cout << students[index].name;
        cout << "     ";

        for (int i = 0; i <NUM_GRADES; ++i) {
            cout << students[NUM_STUDENTS].grades[NUM_GRADES];
            cout << "  ";
        }
        cout << students[NUM_STUDENTS].average;
    }
}

void assignGrades(StudentType students[]) {
    for (int index = 0; index < NUM_STUDENTS; ++index) {
        if (students[NUM_STUDENTS].average < 60)
            students[NUM_STUDENTS].letterGrade = 'F';
        else if (students[NUM_STUDENTS].average < 70)
            students[NUM_STUDENTS].letterGrade = 'D';
        else if (students[NUM_STUDENTS].average < 80)
            students[NUM_STUDENTS].letterGrade = 'C';
        else if (students[NUM_STUDENTS].average < 90)
            students[NUM_STUDENTS].letterGrade = 'B';
        else
            students[NUM_STUDENTS].letterGrade = 'A';
    }
}

float findHighest(StudentType students[]) {
    float highestAverage = 0;
    for (int index = 0; index < NUM_STUDENTS; ++index) {
        if (students[index].average > highestAverage)
            highestAverage = students[index].average;
    }
    return highestAverage;
}

void printHighest(StudentType students[]) {
    float highestAverage = findHighest(students);
    cout << "The name(s) of the student(s) with the Highest average are ";

    for (int index = 0; index < NUM_STUDENTS; ++index) {
        if (students[index].average == highestAverage)
            cout << students[index].name << "  ";
    }
}
4

1 回答 1

1

这是一个清单:

  1. 文件是否存在?
  2. 文件是否在正确的位置?
  3. 尝试用“/”替换路径中的“\”。
  4. 文件是否被锁定?
  5. 你有权限打开它吗?
  6. 您可以使用more或访问该文件less吗?
  7. 如果文件名有空格,您是否在名称周围加了双引号?
  8. 如果文件名有破折号“-”,您是否将名称用双引号括起来?
  9. 尝试编写一个临时文件以查找默认目录。
于 2013-10-14T20:02:07.983 回答