Unresolved externals?

Pages: 12
Thanks, volatile. I think I'm starting to get it now.

I have one more question for anyone willing to answer...I'm getting the error that the variable solution is not initialized...but if I initialize it to 0, for instance, it outputs the "The average of the numbers is 0". Not sure what's wrong here.

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
void average()
{
	int count=0;
	int num;
	int sum=0;
	float solution;
	infile.open("E:\\CFiles\\DataFile2.txt");

	while (infile)
	{
		infile>>num;
	}

	while (count>0)
	{
		infile>>num;
		sum=sum+num;
		count++;
		solution=sum/count;
	}

	cout<<setprecision(5)<<"The average of the numbers is  "<<solution<<endl;
}


First off, always post your entire program, it's much easier for people to look at the big picture to see what they can help you with.

1
2
3
4
while (infile)
	{
		infile>>num;
	}


Each time this loop executes, you're reading in a new value into num. So let's say you read in 3, then a 7, then a 4, then 8 into num. After all of that work, num only equals 8, the other numbers are pretty much useless.

1
2
3
4
5
6
7
	while (count>0)
	{
		infile>>num;
		sum=sum+num;
		count++;
		solution=sum/count;
	}


So, as long as count is greater than 0, this loop will continue executing. There's a problem with that, unless count is initialized to 0 or less, this loop should execute forever. And once you've read all of the numbers in the text file, who knows what the heck is getting read into num.

But there's another problem, this loop currently doesn't execute period. count is initialized to 0, so unless you have a text expression of ( count >= 0 ) the body of your while loop will never execute.


Also, before manipulating a value, you should always first test to ensure you have the value you intended to.

1
2
3
4
5
...
fin >> number;
cout << number << end;
cin.get(); // then look at the number to ensure it's the right one
...


Try to build your program from the ground up, thoroughly testing after each step. If you don't understand something, then do some research until you do (whether through searching google, or asking on a forum).

Edit: while (!eof) is generally what you want if you want to read everything in a text file. BUT I don't think that's the exact syntax, so look it up. It basically keeps looping until it reaches the end of the file. (eof == end of file)
Last edited on
A few things to know about reading from a while is that:
1) Always ensure there is no empty lines at the end. This can cause some issues that make it hard to debug.
2) This is bad practice:
1
2
3
4
	while (infile)
	{
		infile>>num;
	}

Instead of using while (infile), use while (!infile.eof()). I have heard of certain programs that used the same code as you will actually read the last line twice messing up your variables.

@Phil123
filename.eof() will return true when the eof character is reached.
@Phil123
filename.eof() will return true when the eof character is reached.


Thanks, I should know that, but I use a different method.
Topic archived. No new replies allowed.
Pages: 12