The output should be labeled appropriately and formatted to two decimal places.
ID: 3544300 • Letter: T
Question
The output should be labeled appropriately and formatted to two decimal places.
For example, if the input is
A 6.75
your program should print something like this:
The area of a circle with radius 6.75 is 143.14.
Here are the formulas you need:
Diameter = 2*r
Circumference = 2*pi*r
Area of a circle = pi*r*r
where r is the radius. Use 3.14159265 for pi.
Error check the input data and display an error message if the choice of calculation is not an 'A', 'C' or 'D', or if the radius is not greater than zero.
Create an input file, called "circle.txt", containing input data as described.
The program should create an output file, called "results.txt", containing output data as described.
Use meaningful variable names, proper indentation, and appropriate comments.
Use the format and style described in the assignment guidelines.
In order to demonstrate that your program works, you must collect the output of your program.
Use five runs to demonstrate all components of your program work.
In our class, we never learn about "while" and "void".
Explanation / Answer
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#define pi 3.14159265
int main()
{
ifstream infile("circle.txt");
ofstream outfile("results.txt");
char type_to_calcuate;
double radius;
if(!infile)
{
cout <<"Unable to open circle.txt. so Exiting from Program " << endl;
return 0;
}
infile >> type_to_calcuate >> radius;
if(type_to_calcuate == 'A' && radius>0.0)
{
cout <<"The area of a circle with radius "<<fixed<< setprecision(2)<< radius<< " is " <<fixed << setprecision(2)<< pi*radius*radius << endl;
outfile <<"The area of a circle with radius "<<fixed<< setprecision(2)<< radius<< " is "<<fixed << setprecision(2) << pi*radius*radius << endl;
}
else if(type_to_calcuate == 'C' && radius>0.0)
{
cout <<"The Circumference of a circle with radius "<<fixed<< setprecision(2)<< radius<< " is " <<fixed << setprecision(2)<< 2.0*pi*radius << endl;
outfile <<"The area of a circle with radius "<<fixed<< setprecision(2)<< radius<< " is "<<fixed << setprecision(2) << pi*radius*radius << endl;
}
else if(type_to_calcuate == 'D' && radius>0.0)
{
cout <<"The Diameter of a circle with radius "<<fixed<< setprecision(2)<< radius<< " is "<<fixed << setprecision(2) << 2.0*radius << endl;
outfile <<"The area of a circle with radius "<<fixed<< setprecision(2)<< radius<< " is " <<fixed << setprecision(2)<< pi*radius*radius << endl;
}
else
{
cout << "Unknow Type cant calculate any thing "<< endl;
outfile<< "Unknow Type cant calculate any thing "<< endl;
} // end if
infile.close();
outfile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.