所以我在这个网站的其他人的帮助下编写了这段代码,现在我又碰了壁。这个 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
输出文件应该有一只兔子。