【问题标题】:C++, writing into output file from another function coming up blankC ++,从另一个函数写入输出文件变为空白
【发布时间】:2013-11-06 06:32:19
【问题描述】:

所以我在这个站点的其他人的帮助下编写了这段代码,现在我又碰上了另一堵墙。这个 CS 项目的目的是创建一个程序,该程序从输入文件中获取命令,并在输出文件上打印字符以形成图片。

我的 void 函数 printSpace、printChar 和 printNewline 有问题。他们一边阅读一边工作。 printSpace 根据数字创建空格,与 printChar 相同,只是使用字符。 printNewline 结束当前行。因为他们是,他们不工作。一切运行良好,我的所有变量都已正确定义,但我的输出文件没有改变。任何人都可以阐明我所缺少的东西吗?

目前我有:

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

using namespace std;

void printSpace(ifstream&, ofstream&, int&);
void printChar(ifstream&, ofstream&, int&, char&);
void printNewline(ifstream&, ofstream&);
int takeCommand(istream&, int&, int&, char&);

int main()
{
    string str;
    ifstream infile("DrawingInput_01.txt");
    ofstream outfile("DrawingOutput_01.txt");

    int cmd, num;
    char symb;

    outfile << "Header 1\nHeader 2\n";

    for (int i = 0; i < 3; i++)
    {
        getline(infile, str);
    }

    while(takeCommand(infile, cmd, num, symb))
    {
        cout << "Command was " << cmd << ", number was " << num << " and symbol was "<< symb
        << "\n";
        switch(cmd)
        {
            case '1': printSpace(infile, outfile, num); break;
            case '2': printChar(infile, outfile, num, symb); break;
            case '3': printNewline(infile, outfile); break;
            case '0': break;
        }
    }
    infile.close();
    outfile.close();
    return 0;
}

int takeCommand(istream& infile, int& cmd, int& num, char& symb)
{
    char firstChar;
    string str;

    infile >> firstChar;

    switch(firstChar)
    {
        case 's': infile >> str >> num; cmd = 1; break;
        case 'p': infile >> str >> num >> symb; cmd = 2; break;
        case 'n': cmd = 3; break;
        case 'q': cmd = 0; break;
    }

    infile.ignore(numeric_limits<streamsize>::max(), '\n');

    return infile;
}

void printSpace(ifstream& infile, ofstream& outfile, int& num)
{
    for(int i = 0; i != num; i++)
    {
        outfile << " ";
    }
}

void printChar(ifstream& infile, ofstream& outfile, int& num, char& symb)
{
    for(int i = 0; i != num; i++)
    {
        outfile << symb;
    }
}

void printNewline(ifstream& infile, ofstream& outfile)
{
    outfile << "\n";
}

其中一个输入文件如下所示:

; CS 1044 Fall 2010
; Project 4
; Basic Cat
space 1
print 1 /
print 1 \
print 1 _
print 1 /
print 1 \
newline
print 1 (
space 1
print 1 o
print 1 .
print 1 o
space 1
print 1 )
newline
space 1
print 1 >
space 1
print 1 ^
space 1
print 1 <
newline
quit

输出文件应该有一只兔子。

【问题讨论】:

  • 'return' : cannot convert from 'std::istream' to 'int' in takeCommand.

标签: c++ function stream void


【解决方案1】:

我将takeCommand 中的返回值更改为return infile.good(); 以使其编译。

如果您将值读入 int 中,您将无法像打开字符一样打开它们。

改变

switch(cmd)
{
    case '1': printSpace(infile, outfile, num); break;
    case '2': printChar(infile, outfile, num, symb); break;
    case '3': printNewline(infile, outfile); break;
    case '0': break;
}

switch(cmd)
{
    case 1: printSpace(infile, outfile, num); break;
    case 2: printChar(infile, outfile, num, symb); break;
    case 3: printNewline(infile, outfile); break;
    case 0: break;
}

【讨论】:

  • 哇。我从来没有见过。太感谢了!对于这样一个简单的错误,至少可以说是令人沮丧的。
  • 在调试器中逐行执行是确保代码执行您认为应该执行的操作的好方法。我看到它跳过了开关并注意到不匹配。
猜你喜欢
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多