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)