【问题标题】:Ofstream Text Writing Issue with Arrays C++数组 C++ 的 Ofstream 文本写入问题
【发布时间】:2013-12-19 04:58:10
【问题描述】:

好的,我已经两次遇到问题的臭名昭著的代码项目已在很大程度上重新格式化。现在,它看起来像这样:

#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 函数似乎也无法正常工作,如果您也可以查看的话。

【问题讨论】:

  • 首先,myCourses[10] 不是您的阵列的一部分。其次,它输出一个Course 对象。除非你告诉它,否则它不知道该怎么做,但我猜运算符重载不是本练习的重点,所以更明显的方法是这样做。
  • @DJHead-On 您可以使用链表而不是数组作为容器

标签: c++ visual-c++ visual-studio-2012


【解决方案1】:

您的 Course 课程没有 ostream 运算符,因此您不能在此行中调用该运算符:

   outputFile << myCourses[10] << endl;

您可以尝试删除该行并放入:

display_courses(myCourses,courseCount)

但遗憾的是,这会打印到 cout。因此,您确实需要重新编写 display_courses 以获取 ostream&amp; 参数并输出到它,而不是 cout

【讨论】:

  • 你认为我怎么能做到这一点?此外,这个问题涉及 ofstream,而不是 ostream。
  • @DJHead-On 你想要std::ostream,如果你正在写理查德所指的成员。以这种方式,您可以将outputFile std::cout 传递给函数,输出将转到指定的目标。
【解决方案2】:

在您的 fileOutput 函数中执行以下操作:

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

 }

outputFile.close();

" " 替换为您想用来分隔文件内容的任何内容。

或者,您可以研究运算符重载并查看 ofstream 运算符的重载。

【讨论】:

  • @DJHead-On 是你原来的错误信息还是我的代码导致的错误信息?因为我无法确定,因为您的问题没有更改代码以反映我的建议。
  • 我真的收回了你说的话。该功能与主代码分离的问题,而不是您的代码。我实现了它并且它有效。
  • 我认为这是正确的,哈哈,不过我已经有一段时间没有用 C++ 编程了。您需要解释为什么会这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多