Please help here. I cannot figure out whats wrong with this code. I need this to read each number from the file numbers.dat, store it in variable next, and sum the numbers while there are numbers to read. It keeps giving me 0 as the output.
#include <fstream>
#include <iostream>
int main( )
{
using namespace std;
ifstream ins;
ins.open("numbers.dat");
int next;
int sum = 0;
ins >> next;
while (ins >> next)
{
sum = sum + next;
ins >> next;
}
ins.close();
cout << sum << endl;
return 0;
}
Numbers.dat
1
2
3
4
5
Am obviously missing something ... pls help !!!!!!!!!!!!
#include <fstream>
#include <iostream>
int main( )
{
using namespace std;
ifstream ins;
ins.open("numbers.dat");
int next;
int sum = 0;
ins >> next;
while (ins >> next)
{
sum = sum + next;
ins >> next;
}
ins.close();
cout << sum << endl;
return 0;
}
Numbers.dat
1
2
3
4
5
Am obviously missing something ... pls help !!!!!!!!!!!!