### printing cin data to an outFile

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;
}

int main()

{
outFile.open("GeoCalout.txt");

if(!outFile)
{
cout << "Error opening GeoCalout.txt!" << endl;
return 1;
}

int choice;

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();

break;

case 2:
areaOfRectangle();

break;

case 3:
areaOfTriangle();

break;

case 4:
return 1;

}
cout << "Enter your choice (1-4): " << endl;
cin >> choice;
outFile << "Enter your choice (1-4): " << endl;

outFile.close();

}
}

As you store the user's input into your declared variables, you can go ahead and store that info in the file after asking the user to input.

e.g

 `` `` ``outFile << "Enter your choice (1-4): " << choice << endl;``

Implementation in your code.

 ``123456789101112`` ``````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 ``````
The previous suggestion is ok.

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:
 ``12345`` ``````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;``````

 ``12345`` ``````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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` ``````#include #include #include #include const float PI = 3.1415927; using namespace 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; }``````

Thank you both for your input it really helped me understand and get my program up and going!!
Topic archived. No new replies allowed.