简体   繁体   中英

Ofstream Text Writing Issue with Arrays C++

Alright, so my infamous code project I've already had problems with twice has been largely reformatted. Now, it looks like this:

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

using namespace std;


struct Course
{
    string name;
    double grade;
    int block;
};

Course enter_course()
{
    Course foo;

    cout << "What is the name of the course you wish to enter?\n";
    cin >> foo.name;
    cout << "What block is " << foo.name << " ?\n";
    cin >> foo.block;
    cout << "What is your current grade as a percent?";
    cin >> foo.grade;

    return foo;
}

void display_courses(Course courseList[10], int courseCount)
{
    for (int i=0; i<courseCount; i++){
        cout << i+1 << "\t" << courseList[i].name 
            << "\t\tBlock: " << courseList[i].block
            << "\tGrade: " << courseList[i].grade << "%" << endl;
    }
}

double get_gpa(Course courseList[10], int courseCount)
{
    double gradePoints;
    double total = 0.0;
    for (int i=0; i<courseCount; i++){
        if (courseList[i].grade < 100){
            gradePoints = 4;
        } 
        if (courseList[i].grade < 90){
            gradePoints = 3;
        }
        if (courseList[i].grade < 80){
            gradePoints = 2;
        }
        if (courseList[i].grade < 70){
            gradePoints = 1;
        }
        if (courseList[i].grade < 90){
            gradePoints = 0;
        }
        total += gradePoints;
    }

    return total*1.0/courseCount;

}

void fileOutput()   
{   
    ofstream outputFile;
    outputFile.open("userGrades.txt");
    outputFile << myCourses[10] << endl;
    outputFile.close();
    cout << "Grades saved to file!" << endl;
}

void display_options()
{
    cout << "1. Exit\n";
    cout << "2. Enter a Course\n"; 
    cout << "3. Display Courses\n";
    cout << "4. Display GPA\n";
    cout << "5. Request a text file output\n";

    cout << "\n\n";
}

int main()
{
    bool exit=0;
    int option;
    int courseCount=0;
    Course myCourses[10]; //nobody should ever take more than 10 courses! 

    while (exit == 0)
    {
        cout << "GradeBook 2.0\n";
        display_options();
        cout << "Enter a command.\n";
        cin >> option;

        switch (option)
        {
            case 1: 
                exit = 1;
                break;
            case 2:
                myCourses[courseCount] = enter_course();
                courseCount++;
                break;
            case 3:
                display_courses(myCourses, courseCount);
                break;
            case 4:
                cout << get_gpa(myCourses, courseCount) << endl;
                break;
            case 5:
                fileOutput();
                break;
        }
    }

    return 0;
}

However, at the fileOutput() function, I am experiencing these errors for the same line of code:

error C2065: 'myCourses' : undeclared identifier IntelliSense: identifier "myCourses" is undefined

The only thing I can make sense of is that I need to declare myCourses elsewhere, but I don't know how.

Anyone think they can fix this? If so, compile the code and see. Also, the get_gpa function doesn't seem to be working right, if you can look at that as well.

You have no ostream operator for your Course class, so you cant call that operator in this line:

   outputFile << myCourses[10] << endl;

You could try deleting that line and putting in:

display_courses(myCourses,courseCount)

But sadly that prints to cout . You thus really need to re-write display_courses to take a ostream& parameter and output to it, rather than cout .

In your fileOutput function do the following:

for (int i = 0; i < courseCount; i++)
 {
     outputFile << myCourses[i].name << " " << myCourses[i].grade << " " << myCourses[i].block << endl;

 }

outputFile.close();

Replace the " " with whatever you want to use to delimit the file contents.

Alternatively you could investigate operator overloading and look at overloading the ofstream operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM