【问题标题】:Code outputs nonsense to output file instead of string?代码将废话输出到输出文件而不是字符串?
【发布时间】:2016-11-01 03:13:23
【问题描述】:

以下代码用于我必须执行的一个项目,我收到一个文本文件,其中包含学生的名字和姓氏,后跟他的成绩。然后我必须将其转换为一个输出文件,其中包含他的名字和他的平均分数。我收到的文件中有多个学生逐行排列。输出应该看起来像

Rzam, Look           = 0.00
Bambi, Lambi         = 40.47
Coop, Jason          = 27.31

但我的只是打印诸如

之类的垃圾

0x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.000x7fffb08e8698 = 0.00 P>

这是我目前所拥有的:

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

struct Student
{
    string fname;
    string lname;
    double average;
};

int read(ifstream &fin, Student s[]);

void print(ofstream &fout, Student s[], int amount);


int main()
{
    const int size = 10;
    ifstream fin;
    ofstream fout;
    string inputFile;
    string outputFile;
    Student s[size];

    cout << "Enter input filename: ";
    cin >> inputFile;
    cout << "Enter output filename: ";
    cin >> outputFile;
    cout << endl;

    fin.open(inputFile.c_str());
    fout.open(outputFile.c_str());

    read(fin , s);
    print(fout, s, size);

    fin.close();
    fout.close();

}

int read(ifstream &fin, Student s[])
{
    string line;
    string firstName;
    string lastName;
    double score;
    double total;
    int i=0;
    int totalStudents=0;
    Student stu;

    while(getline(fin, line)){
        istringstream sin;
        sin.str(line);

        while(sin >> firstName >> lastName){
            stu.fname = firstName;
            stu.lname = lastName;

            while(sin >> score){
            total *= score;
            i++;
            }
            stu.average = (total/i);
        }
        s[totalStudents]=stu;
        totalStudents++;
    }
    return totalStudents;
}

void print(ofstream &fout, Student s[], int amount)
{
    ostringstream sout;
    for(int i = 0; i<amount; i++)
    {
        sout << left << setw(20) << s[i].lname << ", " << s[i].fname;
        fout << sout << setprecision(2) << fixed << "= " << s[i].average;
    }
}

【问题讨论】:

  • 看起来您将内存地址称为垃圾...尝试使用 c_str() 处理 fname 和 lname 字符串...例如s[i].fname.c_str()
  • 试过但无济于事

标签: c++ string file filestream stringstream


【解决方案1】:

您有一些错误,这些错误已导致您的问题:

  1. 在您的print 函数中,您写入ostringstream,然后尝试将其写入文件流。这很好,但它正在打印ostringstream 缓冲区的地址。因此进行此更改将导致它打印内容:

    fout << sout.str() << setprecision(2) << fixed << "= " << s[i].average;
    

注意.str() 的用法。虽然你根本不需要临时流...

  1. 您没有在输出中放置换行符,因此所有内容都以一行结束,难以阅读:

所以再做一次改变,让它看起来像这样:

fout << sout.str() << setprecision(2) << fixed << "= " << s[i].average << '\n';
  1. 您需要将ostringstream sout; 放在循环内,因此每次都会重置它。否则你会得到奇怪的复合输出。

  2. 您没有使用您的阅读功能计算的学生人数!所以它总是尝试打印 10!做这样的事情:

    int count = read(fin , s);
    print(fout, s, count);
    
  3. 如果没有读取分数,我想你会被零除。所以你应该加一张支票。

  4. 您应该确保阅读的学生不超过size。或者更好的是,只需将它们放在 std::vector 中并从函数中返回。它更简单,更不容易出错。

  5. 每次开始阅读学生时都需要重新设置i,否则后面的学生会被分得太多。每个都需要有一个独立的计数。

我不知道这些是否是唯一的问题,但它肯定会让你走上正确的道路:-)

【讨论】:

  • 非常感谢。这是我的第一个大学水平的 Comp sci 课程,所以意义重大
  • 没问题。我还刚刚注意到我将编辑到答案中的另一件小事。祝你好运!
  • 另外,不要气馁,虽然您的代码有几个问题,但它也做了很多正确的事情! while(getline(fin, line)){ 是一个漂亮的模式,我希望更多新的 c++ 开发人员能够使用它!您在适当的地方使用了std::string,而不是同时处理原始缓冲区。
  • 再次感谢您所做的一切。我真的希望在这门课上取得成功,尽管有时我会迷失方向。
猜你喜欢
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多