My program runs and executes perfectly, but I do not know how to get the data you cin to print to my outFile....help??!!
#include <iostream>
#include <fstream>
#include <math.h>
#define PI 3.14159
using namespace std;
ofstream outFile;
void areaOfCircle()
{
float radius,area;
cout <<"Enter the radius of the circle: " << endl;
cin >> radius;
outFile << "Enter the radius of the circle: " << endl;
area=PI*pow(radius,2);
cout << "The area of a circle with a radius of " << radius << " is equal to " << area << endl;
outFile << "The area of a circle with a radius of " << radius << " is equal to " << area << endl;
}
void areaOfRectangle()
{
float width,length,area;
cout << "Enter the length of the rectangle: " << endl;
cin >> length;
outFile << "Enter the length of the rectangle: " << endl;
cout << "Enter the width of the rectangle: " << endl;
cin >> width;
outFile << "Enter the width of the rectangle: " << endl;
area=length*width;
cout << "The area of a rectangle with the length " << length << " and the width " << width << " is equal to " << area << endl;
outFile << "The area of a rectangle with the length " << length << " and the width " << width << " is equal to " << area << endl;
}
void areaOfTriangle()
{
float base,height,area;
cout << "Enter the base of the triangle: " << endl;
cin >> base;
outFile << "Enter the base of the circle: " << endl;
cout << "Enter the height of the triangle: " << endl;
cin >> height;
outFile << "Enter the height of the triangle: " << endl;
area = base*height*.5;
cout << "The area of a triangle with a base of " << base << " and the height of " << height << " is equal to " << area << endl;
outFile << "The area of a triangle with a base of " << base << " and the height of " << height << " is equal to " << area << endl;
}
cout << "1. Calculate the Area of a Circle. " << endl;
cout << "2. Calculate the Area of a Rectangle. " << endl;
cout << "3. Calculate the Area of a Triangle. " << endl;
cout << "4. Quit. " << endl;
cout << "Enter your choice (1-4): " << endl;
cin >> choice;
outFile << "1. Calculate the Area of a Circle. " << endl;
outFile << "2. Calculate the Area of a Rectangle. " << endl;
outFile << "3. Calculate the Area of a Triangle. " << endl;
outFile << "4. Quit. " << endl;
outFile << "Enter your choice (1-4): " << endl;
while(choice!=4)
{
switch(choice)
{
case 1:
areaOfCircle();
cout << "1. Calculate the Area of a Circle. " << endl;
cout << "2. Calculate the Area of a Rectangle. " << endl;
cout << "3. Calculate the Area of a Triangle. " << endl;
cout << "4. Quit. " << endl;
cout << "Enter your choice (1-4): " << endl;
cin >> choice;
outFile << "1. Calculate the Area of a Circle. " << endl;
outFile << "2. Calculate the Area of a Rectangle. " << endl;
outFile << "3. Calculate the Area of a Triangle. " << endl;
outFile << "4. Quit. " << endl;
outFile << "Enter your choice (1-4): " << choice << endl; //<---------------------------here
By the way the outFile.close(); is inside the while loop, so only the data from the first pass is saved to the file. Move that line outside the loop.
One thing which makes me uneasy is seeing duplicated (or almost duplicated) lines of code like this:
1 2 3 4 5
cout << "1. Calculate the Area of a Circle. " << endl;
cout << "2. Calculate the Area of a Rectangle. " << endl;
cout << "3. Calculate the Area of a Triangle. " << endl;
cout << "4. Quit. " << endl;
cout << "Enter your choice (1-4): " << endl;
1 2 3 4 5
outFile << "1. Calculate the Area of a Circle. " << endl;
outFile << "2. Calculate the Area of a Rectangle. " << endl;
outFile << "3. Calculate the Area of a Triangle. " << endl;
outFile << "4. Quit. " << endl;
outFile << "Enter your choice (1-4): " << endl;
There are several problems, one is that it requires more work. Another is that sooner or later the two sets of output will almost certainly get out of line, as one is edited without doing the same to the other. It also makes the code longer and thus harder to read.
I'm sure there are many solutions. One is to use a stringstream to temporarily buffer the output, and then echo the contents of the buffer to both the screen and to the output file. Here's an example of that approach, which also uses an overloaded output() function to deal with the cin values, as these are already on-screen and only need to go to the file.
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>
constfloat PI = 3.1415927;
usingnamespace std;
ofstream outFile;
ostringstream out;
void output(ostringstream & s);
void output(int n);
void output(float n);
void areaOfCircle()
{
float radius,area;
out << "Enter the radius of the circle: " << endl;
output(out);
cin >> radius;
output(radius);
area = PI*pow(radius,2);
out << "The area of a circle with a radius of "
<< radius << " is equal to " << area << endl;
output(out);
}
void areaOfRectangle()
{
float width,length,area;
out << "Enter the length of the rectangle: " << endl;
output(out);
cin >> length;
output(length);
out << "Enter the width of the rectangle: " << endl;
output(out);
cin >> width;
output(width);
area = length*width;
out << "The area of a rectangle with the length "
<< length << " and the width "
<< width << " is equal to "
<< area << endl;
output(out);
}
void areaOfTriangle()
{
float base,height,area;
out << "Enter the base of the triangle: " << endl;
output(out);
cin >> base;
output(base);
out << "Enter the height of the triangle: " << endl;
output(out);
cin >> height;
output(height);
area = base*height*.5;
out << "The area of a triangle with a base of "
<< base << " and the height of "
<< height << " is equal to "
<< area << endl;
output(out);
}
int main()
{
outFile.open("D:\\temp\\GeoCalout.txt");
if (!outFile)
{
cout << "Error opening GeoCalout.txt" << endl;
return 1;
}
int choice;
out << "1. Calculate the Area of a Circle. " << endl
<< "2. Calculate the Area of a Rectangle. " << endl
<< "3. Calculate the Area of a Triangle. " << endl
<< "4. Quit. " << endl
<< "Enter your choice (1-4): " << endl;
output(out);
cin >> choice;
output(choice);
while (choice!=4)
{
switch(choice)
{
case 1:
areaOfCircle();
break;
case 2:
areaOfRectangle();
break;
case 3:
areaOfTriangle();
break;
case 4:
return 1;
}
out << "Enter your choice (1-4): " << endl;
output(out);
cin >> choice;
output(choice);
}
outFile.close();
}
void output(ostringstream & s)
{
cout << s.str() << flush;
outFile << s.str() << flush;
s.str("");
}
void output(int n)
{
outFile << n << endl;
}
void output(float n)
{
outFile << n << endl;
}