Help in looping files...

Hey all,

Yes, this is part of a HW assignment but I broke it down into parts. Im trying to loop a file and display the first number. Whats driving me crazy is that the book does a horrible job of explaining why things are working. I don't want a simple answer to this but want to understand how the loop actually works. Also, I have already looked at the tutorials several times and don't understand it. The code that I have is almost straight from the book. You will see in my code but these are my questions:
1) What is this saying?? while (edges >> next) "while edges file is inputting into next?"
2) I don't understand how the variables are being used. Count, sum, next?
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
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

int main()
{
	double next, sum = 0;
	int a, b, c,count=0;
	ifstream edges;
	edges.open("edges.txt");
	if (edges.is_open())
	{
		while (edges >> next)
		{
			edges >> a
				>> b
				>> c;
			edges >> next;
			cout << a << endl;
			sum = sum + next;
			count++;
		}
	}
	else
	{
		cout << "Input file open failed.\n";
		exit (1);
	}
	edges.close();
	return 0;
}


The file is:

12 15 7
8 9 14
11 2 15
6 5 4
18 7 10
16 11 17

My program is putting out:
15
11
4
11

Thank you for any help!
I don't have enough context to help you on the variables, but I can help you to understand while (edges >> next).

As you know, the edges is an instance of std::ifstream. The operator >> method of std::ifstream class returns a reference to the std::ifstream instance itself. And how do you check if the stream is still good? Right. Check the return value of operator().

So the line can be explained to:

Read next from the stream edges
If the edges stream is good, do the listed things. Otherwise break the loop.
Your book is absolutely horrible... They include unused variables in this program, which would confuse any beginner! I also think it was moronic of them to use a variable called "next" and then, expect you to talk about the next value in the file... They should have called it "after" or something.... I hope my explanation below helps you.

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
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

int main()
{
	double next, sum = 0;
	int a, b, c, count = 0;
	ifstream edges;
	edges.open("edges.txt"); // open the edge file
	if (edges.is_open()) // if it is open, do the following
	{
		while (edges >> next) // while next is being read in from edges ('next' stored as first value in file (12))
		{
			edges >> a // next value is stored as 'a' (15)
				>> b // next value is stored as 'b' (7)
				>> c; // next value is stored as 'c' (8)
			edges >> next; // next value is stored as 'next' (9)
			cout << a << endl; // right now, a = 15.  15 will be displayed.
			sum = sum + next; // sum = sum (0) + next (9).  So 'sum' is now stored as 9.
			count++; // add one to the count (count was 0, now it is 1).
		} // return to beginning of while loop, and check if the condition is still true (next still able to be read in from edge file)
	}
	else
	{
		cout << "Input file open failed.\n"; // file not found
		exit(1);
	}
	edges.close(); // close the edge file
	return 0;
}
Last edited on
ok, so when the list is finally done and there is no number then the loop will end? If so then what is the point of the variables sum, count, and next? The book is "Problem Solving with C++" by Walter Savitch. Got the loop after the explanation but don't see the point of throwing in those other variables.


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
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

int main()
{
	int a, b, c;
	ifstream edges;
	edges.open("edges.txt");
	if (edges.is_open())
	{
		while (edges >> a)
		{
			edges >> b
				>> c;
			cout << a << endl;
		}
	}
	else
	{
		cout << "Input file open failed.\n";
		exit (1);
	}
	edges.close();
	return 0;
}

Last edited on
A loop ends when a condition is met that makes it false. For example, when another value is unable to be read from edges, it makes the while (edges >> a) false, because it reached the end of the file. So this terminates the loop.

Same idea with the first if loop. if (edges.is_open())...
This is why you have edges.close(); in the appropriate spot, so that it will eventually make that condition false, getting it out of the loop.

You are correct, the other variables are garbage in this context. Only 'a' and 'next' are used. I think they put the other ones in there to show you that you can read in values to variables, even if they are unused.

You could
1
2
3
4
5
 cout << next << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << sum << endl;

Just for reference and, perhaps, to help you understand the flow of the program better.
Last edited on
Topic archived. No new replies allowed.