help....thx...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 21
Reputation: ricnyx is an unknown quantity at this point 
Solved Threads: 0
ricnyx ricnyx is offline Offline
Newbie Poster

help....thx...

 
-1
  #1
Nov 26th, 2007
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main( )
  8. {
  9. ifstream fin;
  10. ofstream fout;
  11.  
  12. fin.open("income.dat");
  13. fout.open("total.dat");
  14.  
  15. char income;
  16. fin.get(income);
  17.  
  18. while (! fin.eof())
  19. {
  20. cout << income ;
  21. fin1.get (income) ;
  22. }
  23.  
  24. double next, sum = 0;
  25. int count = 0;
  26. while (fin >> next )
  27. {
  28. sum = sum + next;
  29. count ++;
  30. }
  31.  
  32. 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?
Last edited by WaltP; Nov 26th, 2007 at 9:37 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,703
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help....thx...

 
0
  #2
Nov 26th, 2007
if you wish to use >> to read the file, which is okay, then use this scheme:
  1. string dayName
  2. double income
  3.  
  4. //read one full line of file each time through the loop
  5. while(fin >> dayName)
  6. fin >> income
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: ricnyx is an unknown quantity at this point 
Solved Threads: 0
ricnyx ricnyx is offline Offline
Newbie Poster

Re: help....thx...

 
0
  #3
Nov 27th, 2007
case 1 , i write like tis....but seems the program can not run the 2nd while loop
  1. ----------------------------
  2. income.dat
  3. monday 1500
  4. tuesday 1100
  5. --------------------------
  6. char income;
  7. string name;
  8. double income1, sum = 0;
  9. int count = 0;
  10.  
  11. fin1.get(income);
  12. while (! fin2.eof())
  13. {
  14. cout << fixed_expenses ;
  15. fin2.get (fixed_expenses) ;
  16. }
  17.  
  18. while (fin >> name )
  19. {
  20. sum = income1 + sum;
  21. count ++;
  22. }
  23.  
  24. cout << sum;
------------------------------------
the output is :

monday 1500
tuesday 1100
0

-----------------------------
no have the sum...wher got problem?
Last edited by cscgal; Nov 28th, 2007 at 4:14 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: ricnyx is an unknown quantity at this point 
Solved Threads: 0
ricnyx ricnyx is offline Offline
Newbie Poster

Re: help....thx...

 
0
  #4
Nov 27th, 2007
case 2 :
  1. while (! fin1.eof() && fin1 >> name)
  2. {
  3.  
  4. cout << income;
  5. fin1.get (income);
  6. fin1 >> income1;
  7. sum = income1 + sum;
  8. count ++;
  9.  
  10. }
  11.  
  12. cout << sum;
------------------------------------------
the output become :
P 2600
--------------------------------------------
any hint or suggestioin? thx ^^
Last edited by cscgal; Nov 28th, 2007 at 4:15 pm. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,703
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help....thx...

 
0
  #5
Nov 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: ricnyx is an unknown quantity at this point 
Solved Threads: 0
ricnyx ricnyx is offline Offline
Newbie Poster

Re: help....thx...

 
0
  #6
Nov 27th, 2007
sorry for type wrong....
-------------------------------
data inside income.dat:

monday 1500
tuesday 1100
------------------------------
the program i write :
  1. char income;
  2. string name;
  3. double income1, sum = 0;
  4. int count = 0;
  5.  
  6. fin1.get(income);
  7.  
  8. //case 1
  9.  
  10. while (! fin1.eof())
  11. {
  12. cout << income;
  13. fin1.get (income) ;
  14. }
  15.  
  16. cout << endl;
  17.  
  18. while (fin1 >> name )
  19. {
  20. sum = income1 + sum;
  21. count ++;
  22. }
  23.  
  24. cout << sum;
  25.  
  26. ---------------------------------
  27. //case 2 :
  28.  
  29. while (! fin1.eof() && fin1 >> name)
  30. {
  31.  
  32. cout << income;
  33. fin1.get (income);
  34. fin1 >> income1;
  35. sum = income1 + sum;
  36. count ++;
  37.  
  38. }
  39.  
  40. 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...
Last edited by cscgal; Nov 28th, 2007 at 4:15 pm. Reason: Please use code tags
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: help....thx...

 
0
  #7
Nov 27th, 2007
Originally Posted by ricnyx View Post
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.

Originally Posted by ricnyx View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: ricnyx is an unknown quantity at this point 
Solved Threads: 0
ricnyx ricnyx is offline Offline
Newbie Poster

Re: help....thx...

 
0
  #8
Nov 27th, 2007
  1. sorry for type wrong....
  2. -------------------------------
  3. data inside income.dat:
  4.  
  5. monday 1500
  6. tuesday 1100
  7. ------------------------------
  8. the program i write :
  9.  
  10. char income;
  11. string name;
  12. double income1, sum = 0;
  13. int count = 0;
  14.  
  15. fin1.get(income);
  16.  
  17. //case 1
  18.  
  19. while (! fin1.eof())
  20. {
  21. cout << income;
  22. fin1.get (income) ;
  23. }
  24.  
  25. cout << endl;
  26.  
  27. while (fin1 >> name )
  28. {
  29. sum = income1 + sum;
  30. count ++;
  31. }
  32.  
  33. cout << sum;
  34.  
  35. ---------------------------------
  36. //case 2 :
  37.  
  38. while (! fin1.eof() && fin1 >> name)
  39. {
  40.  
  41. cout << income;
  42. fin1.get (income);
  43. fin1 >> income1;
  44. sum = income1 + sum;
  45. count ++;
  46.  
  47. }
  48.  
  49. cout << sum;
  50.  
  51. -------------------------------
  52. the screen output which i wish make:
  53. monday 1500
  54. tuesday 1100
  55. 2600
  56. ------------------------------
  57. but case 1 output :
  58.  
  59. monday 1500
  60. tuesday 1100
  61. 0
  62. ------------------------------
  63. after that,i try to use case 2 ,
  64. the output is :
  65. m 2600
  66. ------------------------
  67.  
  68. what should i do to get the output i wish?

like this??
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: help....thx...

 
0
  #9
Nov 27th, 2007
Only around your code. And it needs to be formatted code.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 21
Reputation: ricnyx is an unknown quantity at this point 
Solved Threads: 0
ricnyx ricnyx is offline Offline
Newbie Poster

Re: help....thx...

 
0
  #10
Nov 27th, 2007
omg........7 am..... good ''night''~
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC