So I just need some guidance as to how I could go about fixing this program, So I need to read a file titled "infile.txt" and inside the file are the instructions that describe the shape that should be drawn (A single uppercase char, namely R,T,D,S,E) then it gives the char that the shape should be filled with then the number of columns and rows in an int. The txt file looks like this:
T & 4
S @ 6
T x 5
R * 5 7
D $ 7
D + 5
R = 4 3
E
Now, I'm not even quite sure if I can even accomplish this using a switch statement since the infile is read as a string. But then I'm confused as to how to even change from a switch statement. Anyways, after the infile is read I have to output the shape that is drawn to an outfile. Hopefully this makes sense, I'm super entry level and barely know what I'm doing. So here's 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
//void dimension_instructions(char value);
int main()
{
ofstream outfile;
ifstream infile;
int row, col;
bool exit = false;
char value;
char code;
infile.open("infile.txt");
outfile.open("outfile.txt");
if(!infile.good())
{
cout << "failed to open\n";
}else
{
string buffer;
while(!infile.eof())
{
getline(infile, buffer);
cout << buffer << endl;
}
while(!exit)
{
cout << "Enter your shape R for rectangle, T for triangle, D for diamond, S for square, and E to exit" << endl;
cin >> code;
switch(code)
{
case 'R':
dimension_instructions(code);
cin >> value >> row >> col;
draw_rect(value, row, col);
break;
case 'T':
dimension_instructions(code);
cin >> value >> row;
draw_triangle(value, row);
break;
case 'D':
dimension_instructions(code);
cin >> value >> row;
draw_diamond(value, row);
break;
case 'S':
dimension_instructions(code);
cin >> value >> row;
draw_square(value, row);
break;
case 'E':
cout << "Exiting";
exit = true;
break;
default:
cout << "Invalid input, try again" << endl;
}
}
infile.close();
}
outfile.close();
return 0;
}
/*void dimension_instructions(char value)
{
if (value == 'R')
{
cout << "Enter your character rows and columns values." << endl;
}else
{
cout << "Enter your character and row values" << endl;
}
}*/
void draw_diamond (char out_char, int rows)
{
int space = 1;
space = rows - 1;
for (int i = 1; i <= rows; i++)
{
for (int k = 1; k <= space; k++)
{
cout << " ";
}
space--;
for( int k = 1; k <= 2*i-1; k++)
{
cout << out_char;
}
cout << endl;
}
space = 1;
for (int i = 1; i <= rows; i++)
{
for(int k = 1; k <= space; k++)
{
cout << " ";
}
space++;
for(int k = 1; k <= 2*(rows-i)-1; k++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_triangle (char out_char, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= i; j++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_square (char out_char, int rows)
{
for (int i = 0; i < rows; i++)
{
for (int i = 0; i < rows; i++)
{
cout << out_char;
}
cout << endl;
}
}
void draw_rect (char out_char, int rows, int columns)
{
for (int i = 0; i < rows; i++)
{
for (int i = 0; i < columns; i++)
{
cout << out_char;
}
cout << endl;
}
}