0

So I wrote this code with some help from others from this site, now I've hit another wall. The purpose of this CS project is to create a program that takes commands from the input file, and prints characters to form a picture on the output file.

I'm having a problem specifically with my void functions printSpace, printChar, and printNewline. They work as they read. printSpace creates spaces based on the number, same with printChar, just with characters. printNewline ends the current line. As they are, they don't work. Everything runs fine and all my variables are correctly defined, but my output files do not change. Can anyone shed some light on what I'm missing?

Currently I have:

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

One of the input files look like:

; 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

The output files supposed to have a rabbit.

hanipman
  • 79
  • 1
  • 2
  • 7

1 Answers1

0

I changed the return in takeCommand to return infile.good(); to get it to compile.

If you read values into an int you can't switch on them as if they were chars.

Change

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

to

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;
}
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • Wow. I would've never had seen that. Thank you so much! For such a simple error, it was frustrating to say the least. – hanipman Nov 06 '13 at 07:03
  • Stepping line by line in the debugger is a great way to make sure the code does what you think it should. I saw it skip the switch and noticed the mismatch. – Retired Ninja Nov 06 '13 at 07:06