E
Eli Luong
Hi, I have the following code below. The input file is one number/line,
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful. Thanks.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("numbers.nbr");
int currNum = 0, totalSum = 0;
int count = 0;
double average;
// summation of everything
while (fin >> currNum);
{
cout << currNum << endl;
totalSum += currNum;
count++;
}
if (count > 0)
{
ofstream fout("avg.txt");
average = totalSum/count;
fout << average << endl;
cout << totalSum << "," << count << "," << average << endl;
}
//system("pause");
return 0;
}
but the while loop is only occuring once, and it's only taking in the
value of the last number (count only goes to 1 and stops there) and I
don't see why it's doing that because I don't see anything that should
be wrong at the moment. Is my input file incorrect? I just wrote up
some numbers in a text file and then renamed it. Any input to fix this
would be useful. Thanks.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("numbers.nbr");
int currNum = 0, totalSum = 0;
int count = 0;
double average;
// summation of everything
while (fin >> currNum);
{
cout << currNum << endl;
totalSum += currNum;
count++;
}
if (count > 0)
{
ofstream fout("avg.txt");
average = totalSum/count;
fout << average << endl;
cout << totalSum << "," << count << "," << average << endl;
}
//system("pause");
return 0;
}