Question
Problem description For this assignment, you will write a program that reads from one file, interprets what it reads and writes to a second file.
Problem description
For this assignment, you will write a program that reads from one file, interprets what it reads and writes to a second file. The program will use a number of functions as listed below. This program will be a review of loops, menus, and function calls. It will also introduce the use of stringstream for writing to a string and ifstream and ofstream for reading and writing files
There is an example input file in Moodle: infile.txt. You will need to place that file onto your computer in the appropriate location for your program to read it. The exact location depends on which IDE you are using. For assistance, read the document Working Director or Where is my File?.
Provided file:
T & 4 S @ 6 T x 5 R * 5 7 D $ 7 D + 5 R = 4 3 E
Program Functionality
Your main function should have the following functionality:
-Open infile.txt for reading.
-Open outfile.txt for writing.
-Read the information on infile.txt, one line at a time, until the file is at EOF or an E is read.
1.Read a code and select the proper function for that code
2.Read the remaining input values, based on the function code
3.Call the appropriate function, passing in the input values
4. Save the returned string
5. Call writeShape, passing in the string and a reference to your ofstream object
Close both files.
Interpreting infile.txt
Each record (line) of infile.txt will have the following format.
code char value1 value2 (optional) except for any records starting with E
The code will be a single character from this list: D, R, S, T, E.
The char will also be a single character, this will be used to make the output shape
For the codes D, S, and T, there will be a single integer value that is the dimension
For the code R, there will be two integer values that are the dimensions
For the code E, there will be nothing to read after the E
Required Functions
At a minimum the program should have the following functions.
makeRectangle
three parameters, symbol, length, width
uses stringstream to create a rectangle shaped string of symbols
returns the string
makeSquare
two parameters, symbol and side
uses stringstream to create a square shaped string of symbols
returns the string
makeTriangle
two parameters, symbol and height
uses stringstream to create a triangle shaped string of symbols
returns the string
makeDiamond
two parameters, symbol and width
uses stringstream to create a diamond shaped string of symbols
returns the string
writeShape
two parameters, a shapeString and a reference parameter outfile
writes the shapeString to outfile
no return values
The program will need a reading loop that is terminated with end of file or the code E, some way to determine which code was entered, and a function to draw each shape. The function will need to have as parameters the character to use to draw the shape, and the dimension(s) of the shape.
You should check for an improper input file. That is, if you get an EOF when trying to read something, you should exit the program with an error message.
Codes and Functions
The codes and their meanings are as follows:
End
If the code is an E, you are to end the program.
Diamond
If the code is a D, you are to create a diamond of the char. Diamonds should always have an odd number for their dimension. For example, for the following input line.
D ! 3
You should generate a diamond that looks like this.
!
!!!
!
For the intput line
D * 5
You should create a diamond that looks like this.
*
***
*****
***
*
If you happen to get a diamond with an even dimension, increase it by one.
Square
If the code is an S, you are to create a square of the char. For example, with the following input line
S ! 3
You should generate a square that looks like this:
!!!
!!!
!!!
For the line
S @ 5
You should generate a square that looks like
@@@@@
@@@@@
@@@@@
@@@@@
@@@@@
Triangle
If the code is a T, you are to create a triangle of the char. For example, with the line
T = 3
You should generate a triangle that looks like
=
==
===
For the line
T ^ 5
You should generate a triangle that looks like
^
^^
^^^
^^^^
^^^^^
Rectangle
If the code is an R, you are to create a rectangle of the char. Rectangles have two values: rows
and columns. For example, with the line
R a 2 4
You should generate a rectangle that looks like
aaaa
aaaa
For the line
R & 6 3
You should generate a rectangle that looks like
&&&&&&
&&&&&&
&&&&&&
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started