Problem: Write a C++ program to write to an output file different figures depend
ID: 3673272 • Letter: P
Question
Problem: Write a C++ program to write to an output file different figures depending on the input from an input file. In main, open the input and output files (MAKE SURE THE OUTPUT FILE HAS A DIFFERENT NAME FROM THE INPUT FILENAME, AND A DIFFERENT NAME FOR EACH DIFFERENT INPUT FILE). If they don't open, print an error message and end the program. If they do open, call the following in a loop, which you'll stay in the loop as long as the first function returns true: Function to read a line of input into main variables from an input file (see 1. below) Function to process one set of input values (see 2. below) 1. In the input function, read from the input file a char. If you can't read the char (see hint in Class Notes), then return false. If you were able to read a char, CHANGE IT TO UPPER CASE (hints given in class, so see Class Notes under Topic 6). If the char is 'P' or 'R', read 2 ints (I'm calling int1 and int2) from the input file. If the char is 'S' or 'T', read one int (int1) from the input file. Return true. You MUST use reference parameters for the input values in this function! 2. In the function that processes the shape code, Use a switch to determine which subfunction to call based on the shape code In the switch, call the subfunction (call 3. for 'P', 4. for 'R', 4. for 'S' passing int1 twice, and 5. for 'T') that matches the choice (if 'P', 'R', 'S', or 'T'), or display to the screen an error message (if none of those) 3. In the function writes a parallelogram, write to the output file (parameter) a parallelogram int1 rows of int2 '*'s, where each row is shift to the right by one more space than the previous row. YOU MUST USE NESTED FOR LOOPS HERE (see test runs) 4. In the function writes a rectangle, write to the output file (parameter) a rectangle of int1 rows of int2 '*'s . YOU MUST USE NESTED FOR LOOPS HERE (see test runs) 5. In the function writes a right-triangle (half-square), write to the output file (parameter) a right-triangle of int1 '*'s in the first row, (int1-1) '*'s in the 2nd row, ..., 2 '*'s in the next to last row, 1 '* in the last row. YOU MUST USE NESTED FOR LOOPS HERE (see test runs) Hints: The output file (ofstream) opened in main MUST be a reference parameter to functions 2, 3, 4, and 5, along with the needed value parameters! DO NOT USE ANY EXTERNAL VARIABLES (i.e., variables MUST be declared inside of main or inside of functions). (External const is OK, because they're not variables). Your function names (as with your variable and parameter names)
FUNCTION DOES or points will be deducted (DON’T give names like function1, function2, etc., but names like readFileInput or processInput)!!! Include in your programs: nested for loops where indicated above while loop or do-while loop for the main loop functions (as specified above, WITH REFERENCE PARAMETERS ONLY WHERE NEEDED, value parameters if you’re NOT CHANGING the parameter!) switch statement (where indicated above) Comments and indentation as usual
Explanation / Answer
Here ,I have written the code for the problem.Although I have not run it on the compiler but the algorithm and logic will remain same.
#include<iostream>
#include<fstream>
#include<cctype> // for tolower function
#include<stdio.h>
using namespace std;
void writeFileOutput(ofstream outfile,char c,int x,int y=0)
{
switch(c)
{
case 'R' : {
for(int i=0;i<x;i++) //It draws rectangle of y columns and x rows
{ for(int j=0;j<y;)
outfile<<"*";
outfile<<endl;
} break;
}
case 'P': {
for(int i=0;i<x;i++)
{ //It draws parallelogram of y columns and x rows
for(int k=0;k<i;) outfile<<" ";
for(int j=0;j<y;)
outfile<<"*";
outfile<<endl;
} break;
}
case 'S' :{ //It draws square of x columns and x rows
for(int i=0;i<x;i++)
{ for(int j=0;j<x;)
outfile<<"*";
outfile<<endl;
} break;
}
case 'T' :{
for(int i=0;i<x;i++) //It draws right angled triangle of x columns and x rows
{ for(int j=0;j<x;j++)
outfile<<"*";
outfile<<endl;
} break;
}
default : { printf(" Error"); break; }
}
}
bool readFileInput(ifstream inFile,ofstream outFile)
{
char END_OF_FILE = '#';
char singleCharacter;
int x,y;
//Get a character from the input file
inFile.get(singleCharacter);
//Read the file until it reaches #
//When read pointer reads the # it will exit loop
//This requires that you have a # sign as last character in your text file
while (singleCharacter != END_OF_FILE)
{
if(islower(singleCharacter)) { toupper(singleCharacter);} //Changes the lowercase characters to uppercase
if(singleCharacter == 'R' || singleCharacter == 'P')
{
inFile>>x>>y; writeFileOutput(outFile,singleCharacter,x, y); return true;
}
if(singleCharacter == 'S' || singleCharacter == 'T')
{
inFile>>x; writeFileOutput(outFile,singleCharacter,x); return true;
}
} return false;
}
int main()
{
ifstream myfile ("read.txt");
ofstream outfile("write.txt");
if (myfile.is_open())
{
if(readFileInput(myfile,outfile))
{ }
else
{ cout<<" Error"; }
myfile.close();
outfile.close();
}
else cout << "Unable to open file";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.