#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main( )
{
ifstream fin;
ofstream fout;

fin.open("income.dat");
fout.open("total.dat");

char income;
fin.get(income);

while (! fin.eof())
{
cout << income ;
fin1.get (income) ;
}

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

------------------------------------
example file (income.dat)
monday 1000
tuesday 1500
wednesday 1200

the program seems cant sum up the number......any hint or suggestion can help me change to program to solve the problem?

Comments
It's time you figured out the code tags.

if you wish to use >> to read the file, which is okay, then use this scheme:

string dayName
double income

//read one full line of file each time through the loop
while(fin >> dayName)
  fin >> income

case 1 , i write like tis....but seems the program can not run the 2nd while loop

----------------------------
income.dat
monday 1500
tuesday 1100
--------------------------
char income;
string name;
double income1, sum = 0;
int count = 0;

fin1.get(income);
while (! fin2.eof())
{
cout << fixed_expenses ;
fin2.get (fixed_expenses) ;
}

while (fin >> name )
{
sum = income1 + sum;
count ++;
}

cout << sum;

------------------------------------
the output is :

monday 1500
tuesday 1100
0

-----------------------------
no have the sum...wher got problem?

case 2 :

while (! fin1.eof() && fin1 >> name)
{

cout << income;
fin1.get (income);
fin1 >> income1;
sum = income1 + sum;
count ++;

}
		
cout << sum;

------------------------------------------
the output become :
P 2600
--------------------------------------------
any hint or suggestioin? thx ^^

case 1:

where did fixed_expenses come from?

what's with three ifstream objects (fin, fin1 and fin2????

Don't use eof() in a loop conditional. It's bound to trip you up sooner or later.

Doesn't it make more sense to give income a numerical value and name a string value?

The basic approach here seems to be to try to read through the file twice, once with each loop, which is illogical.


case 2:
declare name to be a string and income1 to be of type double.

make the while conditional just fin1 >> name.

Drop these two lines:
cout << income;
fin1.get (income);

if sum is declared type double and initialized to zero before the loop starts, it should work. Basically you read in the first token in the line and ignore whatever it is. Then, assuming there was a first token, you read in the second token, and add it's value to sum. When you reach the end of the file and fin1 tries to read EOF into name, then the stream will fail, which will cause a value of zero to return, which will stop the loop.

sorry for type wrong....
-------------------------------
data inside income.dat:

monday 1500
tuesday 1100
------------------------------
the program i write :

char income;
string name;
double income1, sum = 0;
int count = 0;

fin1.get(income);

//case 1

while (! fin1.eof())
{
cout << income;
fin1.get (income) ;
}

cout << endl;

while (fin1 >> name )
{
sum = income1 + sum;
count ++;
}

cout << sum;

---------------------------------
//case 2 : 

while (! fin1.eof() && fin1 >> name)
{

cout << income;
fin1.get (income);
fin1 >> income1;
sum = income1 + sum;
count ++;

}

cout << sum;

-------------------------------
the screen output which i wish make:
monday 1500
tuesday 1100
2600
------------------------------
but case 1 output :

monday 1500
tuesday 1100
0
------------------------------
after that,i try to use case 2 ,
the output is :
m 2600
------------------------

what should i do to get the output i wish?
pls help me ... im newbie .... just study c++ for a few week...

what should i do to get the output i wish?

By reading the Rules, which has been suggested many times to you already. And you obviously missed Read Me: Read This Before Posting too.

pls help me ... im newbie .... just study c++ for a few week...

And how many times do we have to be reminded of this? We really don't care.

sorry for type wrong....
-------------------------------
data inside income.dat:

monday 1500
tuesday 1100
------------------------------
the program i write :

char income;
string name;
double income1, sum = 0;
int count = 0;

fin1.get(income);

//case 1

while (! fin1.eof())
{
cout << income;
fin1.get (income) ;
}

cout << endl;

while (fin1 >> name )
{
sum = income1 + sum;
count ++;
}

cout << sum;

---------------------------------
//case 2 : 

while (! fin1.eof() && fin1 >> name)
{

cout << income;
fin1.get (income);
fin1 >> income1;
sum = income1 + sum;
count ++;

}

cout << sum;

-------------------------------
the screen output which i wish make:
monday 1500
tuesday 1100
2600
------------------------------
but case 1 output : 

monday 1500
tuesday 1100
0
------------------------------
after that,i try to use case 2 ,
the output is :
m 2600
------------------------

what should i do to get the output i wish?

like this??

declare string for name
declare numerical variable for income
declare and initialize sum to zero 
declare stream to read file and associate stream with file

confirm file opened

while input name
  input income
  output name and income
  add income to sum
output sum after loop completed
This article has been dead for over six months. Start a new discussion instead.