Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete and correct C++ program that calculates and prints the diameter

ID: 3544213 • Letter: W

Question

Write a complete and correct C++ program that calculates and prints the diameter, the circumference, or the area of a circle, given the radius. The program inputs two Data items. The first is a character - 'D' (for diameter), 'C' (for circumference), or 'A' (for area) - to indicate the calculation needed. The next data value is a floating-point number indicating the radius of the particular circle.

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

NOTE- can't use switch case method have to use if statement and fstream. can u help me with documention as well please

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;
}
while(!infile.eof())
{
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
} //end while
infile.close();
outfile.close();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote