My C++ project purpose is to output a file with a picture made from simple characters.
The first part of my project requires that I "include a function that read a command from the input file and returns an integer value representing the command that was read". That's my takeCommand function. Seems horribly inefficient, but I guess necessary if, I understood it correctly.
The current problem is that my the while loop in my main function runs an infinite loop. When outputting the text "Works", it constantly repeats. My intention was to make an End-of-File controlled loop. Inside the takeCommand function, when it reads the last line, the takeCommand function ceases to run, thus the while loop ends.
I need ideas as to how to get the while loop to work without repeating constantly.
What I currently have is:
int takeCommand(int& num, int& cmd);
ifstream infile;
ofstream outfile;
int main()
{
const int MAX_FOR = 3;
string str;
int num, cmd;
infile.open("DrawingInput_01.txt");
outfile.open("DrawingOutput_01.txt");
for (int i = 0; i < MAX_FOR; i++)
{
getline(infile,str);
}
takeCommand(num,cmd);
while(infile)
{
cout << "Works";
/*switch(cmd)
{
case '1': printSpace(infile, outfile); break;
case '2': printChar(infile, outfile); break;
case '3': printNewline(infile, outfile); break;
case '0': break;
}*/
takeCommand(num, cmd);
}
infile.close();
outfile.close();
return 0;
}
int takeCommand(int& num, int& cmd)
{
string str;
char firstChar;
infile.open("DrawingInput_01.txt");
for (int i = 0; i < 3; i++)
{
getline(infile.str);
}
infile >> firstChar >> str >> num;
switch(firstChar)
{
case 's': cmd = 1; break;
case 'p': cmd = 2; break;
case 'n': cmd = 3; break;
case 'q': cmd = 0; break;
}
return cmd;
}
One of the input files looks 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 file is supposed to make a bunny.
The void functions are in my code, it's just not the problem right now.