簡體   English   中英

數組C ++的Ofstream文本編寫問題

[英]Ofstream Text Writing Issue with Arrays C++

好的,所以我臭名昭著的代碼項目已經兩次遇到問題,現在已經重新格式化。 現在,它看起來像這樣:

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

但是,在fileOutput()函數中,同一行代碼遇到這些錯誤:

錯誤C2065:“ myCourses”:未聲明的標識符IntelliSense:未定義標識符“ myCourses”

我唯一能理解的就是我需要在其他地方聲明myCourses,但是我不知道如何。

有人認為他們可以解決此問題嗎? 如果是這樣,請編譯代碼並查看。 另外,如果您也可以查看get_gpa函數,則該函數似乎工作不正常。

您的課程類沒有ostream運算符,因此您不能在此行中調用該運算符:

   outputFile << myCourses[10] << endl;

您可以嘗試刪除該行並放入:

display_courses(myCourses,courseCount)

但可悲的是打印到cout 因此,您確實需要重寫display_courses以獲取ostream&參數並將其輸出,而不是cout

在您的fileOutput函數中執行以下操作:

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

 }

outputFile.close();

" "替換為要用來分隔文件內容的任何內容。

或者,您可以調查運算符重載,並查看ofstream運算符的重載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM