I have this assignment for an intro C++ class and I'm confused as to why my getline seems to be working but it doesn't output my functions into the outfile.txt
.My teacher said my getline syntax is incorrect, but I'm confused as to how.
My infile.txt
reads:
T & 4
S @ 6
T x 5
R * 5 7
D $ 7
D + 5
R = 4 3
E
And my code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void draw_rect (char out_char, int rows, int columns); // Draws a rectangle shape
void draw_square (char out_char, int rows); //Draws a square shape
void draw_triangle (char out_char, int rows);// Draws a triangle shape
void draw_diamond (char out_char, int rows); // Draws a diamond shape
int main()
{
ofstream outfile;
ifstream infile;
int row, col;
bool exit = false;
char value;
infile.open("infile.txt");
outfile.open("outfile.txt");
if(!infile.good())
{
cout << "failed to open\n";
}else
{
string buffer;
while(!infile.eof() || !exit)
{
getline(infile, buffer);
switch(buffer[0])
{
case 'R':
value = buffer[2];
row = buffer[4];
col = buffer[6];
draw_rect(value, row, col);
break;
case 'T':
value = buffer[2];
row = buffer [4];
draw_triangle(value, row);
break;
case 'D':
value = buffer[2];
row = buffer[4];
draw_diamond(value, row);
break;
case 'S':
value = buffer[2];
row = buffer[4];
draw_square(value, row);
break;
case 'E':
cout << "Files Written.\nExiting." << endl;
exit = true;
break;
default:
cout << "Invalid input, try again" << endl;
}
}
}
return 0;
}
void draw_diamond (char out_char, int rows)
{
ofstream outfile;
int space = 1;
space = rows - 1;
for (int i = 1; i <= rows; i++)
{
for (int k = 1; k <= space; k++)
{
outfile << " ";
}
space--;
for( int k = 1; k <= 2*i-1; k++)
{
outfile << out_char;
}
outfile << endl;
}
space = 1;
for (int i = 1; i <= rows; i++)
{
for(int k = 1; k <= space; k++)
{
outfile << " ";
}
space++;
for(int k = 1; k <= 2*(rows-i)-1; k++)
{
outfile << out_char;
}
outfile << endl;
}
}
void draw_triangle (char out_char, int rows)
{
ofstream outfile;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
outfile << out_char;
}
outfile << endl;
}
}
void draw_square (char out_char, int rows)
{
ofstream outfile;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
outfile << out_char;
}
outfile << endl;
}
}
void draw_rect (char out_char, int rows, int columns)
{
ofstream outfile;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
outfile << out_char;
}
outfile << endl;
}
}