Why is my program only reading last line of file?

I'm brand new to this and I don't know why my program (posted below) only reads the last line of data from the input file. Our assignment is to write this program with a while loop that terminates when there's no more data in the file. Why does it skip down to the last line of data in the file? It skips the first four lines and only picks up the last line. I've checked that the input file is OK.

We have to use the value-returning functions, too.

Here's my program. Please tell me where I'm not getting it. I've been stuck on this for hours.

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
/*
This program reads the radius of a circle from a file
called circles2.txt and outputs its area and circumference
into a file called circlestuff2.txt
*/

#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPES

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

// NAMED CONSTANT

const double PI = 3.1416;

int main()
{

// DECLARE VARIABLES

ifstream inData;
ofstream outData;
double radius;
double circumference;
double area;

// OPEN FILES

inData.open("f:\\circles2.txt");
outData.open("f:\\circlestuff2.txt"); 

// INPUT SECTION

cout << "Data is being processed" << endl << endl
<< endl << endl;

inData >> radius;
while (!inData.eof())
{
	inData >> radius;
}

// PROCESSING SECTION

circumference = FindCircumference(PI, radius);
area = FindArea(PI, radius);


// OUTPUT SECTION

outData << fixed << showpoint << setprecision(1);

outData << setw(15) << "Radius" << setw(30)
<< "Area" << setw(45) << "Circumference" << endl << endl;

outData << setw(15) << radius;
outData << setw(30) << area;
outData << setw(45) << circumference << endl;
while (!inData.eof())
{
outData << setw(15) << radius;
outData << setw(30) << area;
outData << setw(45) << circumference << endl;
}

// CLOSE THE FILES

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

getch();
return 0;
}

// VALUE-RETURNING FUNCTIONS


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


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

The input section reads a radius on line 45, then repeatedly reads in radii in the loop afterward, repeatedly overwriting the old "radius" value until it reaches the end of the input file. Then it calculates the area and circumference once, then starts printing in a loop that doesn't run as infile is already at the end of file.
Zhuge, thank you for telling me the concept of the problem. I get what it's doing now, but I don't know how to code it so it won't overwrite. Can you help me with fixing the code? Thanks millions for helping me start to see the problem. I'm totally neo at this!
You should put the code calculating the circumference/area in the loop that is reading the radius. So basically, something like this:
1
2
3
4
5
6
while(infile.good()) {
    // read radius
    // calc area
    // calc circumference
    // print results
}

You'll notice I loop on good() instead of eof(). That's because eof() isn't true until you have already tried and failed to read data from the file.
Zhuge, we don't even use infile.good in our class yet! Here's my latest try, but it's more messed up than ever. I understand your concepts, but I can't seem to get them down in code. Now I don't know what it's reading in the file, but it's still only putting out one line.

I'm gonna keep trying, but if you can think of a way to break it down even simpler for a programming midget like me, then I'd appreciate it. But already you've helped me so much by showing me what I need to try to figure out.

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

#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPES

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

// NAMED CONSTANT

const double PI = 3.1416;

int main()
{

// DECLARE VARIABLES

ifstream inData;
ofstream outData;
double radius;
double circumference;
double area;

// OPEN FILES

inData.open("f:\\circles.txt");
outData.open("f:\\circlestuff3.txt"); 

// INPUT SECTION

cout << "Data is being processed" << endl << endl
<< endl << endl;

inData >> radius;
inData >> area;
inData >> circumference;
while (inData.good())
{
	inData >> radius;
	inData >> area;
	inData >> circumference;
}

// PROCESSING SECTION

circumference = FindCircumference(PI, radius);
area = FindArea(PI, radius);


// OUTPUT SECTION

outData << fixed << showpoint << setprecision(1);

outData << setw(15) << "Radius" << setw(30)
<< "Area" << setw(45) << "Circumference" << endl << endl;

outData << setw(15) << radius;
outData << setw(30) << area;
outData << setw(45) << circumference << endl;
while (inData.good())
{
outData << setw(15) << radius;
outData << setw(30) << area;
outData << setw(45) << circumference << endl;
}

// CLOSE THE FILES

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

getch();
return 0;
}

// VALUE-RETURNING FUNCTIONS


double FindCircumference(double PI, double radius)
{
	double circumference;

return circumference = (2 * radius) * PI;
}


double FindArea(double PI, double radius)
{
	double area;

return area = pow(radius, 2) * PI;
}
With both Zhuge's and my teacher's help, I figured out the basic concept of what I was doing wrong. My teacher showed me corrections for my program. Following is the new program.

But get this: Now, for some reason I can't figure out, it only reads every other line. It skips the first line, reads the second, skips the third, etc. How can that happen? I've verified that the input file is valid.

Can someone please give me an idea of why it's skipping every other number?

Here's the input:

6.4
8.12
33.481
0.88
14.7
123.45

Here's a copy of the output file:

 Radius                          Area                                Circumference

            8.1                         207.1                                         51.0
            0.9                           2.4                                          5.5
          123.5                       47877.7                                        775.7



Here's my latest program, which my teacher helped me with:


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

using namespace std;

// FUNCTION PROTOTYPES

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

// NAMED CONSTANT

const double PI = 3.1416;

int main()
{

// DECLARE VARIABLES

ifstream inData;
ofstream outData;
double radius;
double circumference;
double area;

// OPEN FILES

inData.open("f:\\circleinput.txt");
outData.open("f:\\circleoutput.txt"); 

// INPUT SECTION

cout << "Data is being processed" << endl << endl
<< endl << endl;

// NON-LOOPED OUTPUT SECTION

outData << setw(15) << "Radius" << setw(30)
<< "Area" << setw(45) << "Circumference" << endl << endl;

// BEGIN LOOP

inData >> radius;		// PRIMING STATEMENT
while (inData)
{
	inData >> radius;

// PROCESSING SECTION

circumference = FindCircumference(PI, radius);
area = FindArea(PI, radius);

// OUTPUT SECTION

outData << fixed << showpoint << setprecision(1);

outData << setw(15) << radius;
outData << setw(30) << area;
outData << setw(45) << circumference << endl;

inData >> radius;		// UPDATE THE VARIABLE
}

// END LOOP

// CLOSE THE FILES

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

getch();
return 0;
}

// VALUE-RETURNING FUNCTIONS


double FindCircumference(double PI, double radius)
{
	double circumference;

return circumference = (2 * radius) * PI;
}


double FindArea(double PI, double radius)
{
	double area;
return area = pow(radius, 2) * PI;
}





BTW, it is always good to check for failure immediately after attempting to read input. Even with something like
1
2
3
4
5
while (infile.good())
  {
  // read input...
  // do stuff..
  }
you have a recipe for error. Better to do one of:

read input in the loop test
1
2
3
4
while (infile >> fooey)
  {
  // do stuff...
  }

break from loop on read failure (useful for complicated reads)
1
2
3
4
5
6
while (true)
  {
  // read stuff...
  if (!infile) break;
  // do stuff..
  }

Once your loop terminates you can test to make sure that infile.eof() is signalled and not some error.

Hope this helps.
Duoas,

It'll help once I get that far. I'm in something like C++ for Neonates. We're not allowed to use the infile.good() thingy yet.

Once again my teacher helped me figure out the problem.

In the latest version of my program above, I inserted an extra read statement on line 49, and that was causing the program to skip every other line.

Thanks again and I'll remember your loop advice for when we get that far.
Topic archived. No new replies allowed.