processing information from a file

I am working on a program that calculates the area, and circumference of a circle. It reads the radius from a file. I can get it to read the file, process the information, and print to another file. The issue i have is it is only doing this for the last number in the list. It is supposed to read the file and process every number until the list is completed.

I think it is a very simple mistake i am making, but after 6 hours of trying to figure it out im spent.

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
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <fstream>

using namespace std;

double FindCircumference(const double, double);
double FindArea (const double, double);


int main()
{
	cout << fixed << showpoint << setprecision(1);

	const double PI = 3.1416;
	ifstream inData;
	ofstream outData;
	double radius;
	double circumference;
	double area;
	int i;
         
	inData.open("C:\\Users\\CaseySmith\\Documents\\Richland\\Proramming\\circles.txt");  
	outData.open("C:\\Users\\CaseySmith\\Documents\\Richland\\Proramming\\circlesOutput.txt");
	
	inData >> radius;

	outData << fixed << showpoint << setprecision(1) 
	<< "This program has been modified by ###########" << '\n' <<
	setw(7) << "Radius" << setw(15) << "Area" << setw(28) << "Circumference" << '\n';
		

	while (inData)
        {
		inData >> radius;
		circumference = FindCircumference (PI, radius);
		area = FindArea (PI, radius);
        }

	for (i = 0; i < 6; i++)
	{
		outData << setw(7) << radius << setw(15) << area << setw(28) << circumference << '\n';
	}

		
	inData.close();
	outData.close();

    return 0;
}

double FindCircumference (const double PI, double radius)
{
	return PI * 2 * radius;
}

double FindArea (const double PI, double radius)
{
	return PI * radius * radius;
}


Here is the output

This program has been modified by ########### 
 Radius           Area               Circumference
  123.5        47877.7                       775.7
  123.5        47877.7                       775.7
  123.5        47877.7                       775.7
  123.5        47877.7                       775.7
  123.5        47877.7                       775.7
  123.5        47877.7                       775.7



And this is the list

5.6
8.12
10.959
0.88
15.4
123.45

The loop on line 34 gets the circumference and area OK, but it doesn't do anything with it.

Note that each time the loop runs and you re-calculate the area and circumference... you are dropping the old value (variables can only hold 1 value at a time -- so when you calculate the area for the 2nd circle, you lose the area of the 1st circle).

Then on the loop on line 41 -- you're printing the same area/circumference over and over (the last one processed, which was the 6th circle)


Since you don't need to do anything with the area/circ other than print it, I would simply get rid of that line 41 loop and print the data directly from the line 34 loop. That way you print each circle as it's computed, so it doesn't matter that you don't remember each circle's area/circ.
Topic archived. No new replies allowed.