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.

1
2
3
4
5
6
7
8
9
10
11
12
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:
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
#include <sstream>
#include <math.h>

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.