0
#include <iostream>
#include <fstream>



using namespace std;


int main( )
{
ifstream fin;
ofstream fout;


fin.open("income.dat");
if (fin.fail( ))
{
cout << "Input file opening failed.\n";
exit(1);
}


fout.open("total.dat");
if (fout.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}


double next, sum = 0;
int count = 0;
while (fin >> next )
{
sum = sum + next;
count ++;


}



fin.close( );
fout.close( );


cout << "End of program.\n";
return 0;
}

i wan to make a program tat can read all the number in the file.....but it cant .... y?

Edited by happygeek: fixed formatting

2
Contributors
9
Replies
10
Views
9 Years
Discussion Span
Last Post by ricnyx
0

yaya....just realise...thx for help.....hmmm.....if i hav number and text in file.dat......how to make it sum up all the number in the file?

0

for example (xx.dat):
2day 1000
2molo 1000

the code i write can not sum up the number...wat should i change for make it read number only?

0

sorry...the data example should wirte like this :
today 1000
yesterday 1000

0

Hope you figured this out already. If not, what basically you need to do is use "get" or "ignore" to eat the word before the number, then you can do your summation as usual.

0

hmm...i get it a bit....but seem some problem occur again.....i post new 1 out already...thx for u help.....u all r so kind .... ^^

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.