944,158 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1232
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 26th, 2007
-1

help....thx...

Expand Post »
C++ Syntax (Toggle Plain Text)
  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...
Similar Threads
Reputation Points: 8
Solved Threads: 0
Newbie Poster
ricnyx is offline Offline
21 posts
since Nov 2007
Nov 26th, 2007
0

Re: help....thx...

if you wish to use >> to read the file, which is okay, then use this scheme:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 27th, 2007
0

Re: help....thx...

case 1 , i write like tis....but seems the program can not run the 2nd while loop
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
ricnyx is offline Offline
21 posts
since Nov 2007
Nov 27th, 2007
0

Re: help....thx...

case 2 :
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
ricnyx is offline Offline
21 posts
since Nov 2007
Nov 27th, 2007
0

Re: help....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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 27th, 2007
0

Re: help....thx...

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

monday 1500
tuesday 1100
------------------------------
the program i write :
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 8
Solved Threads: 0
Newbie Poster
ricnyx is offline Offline
21 posts
since Nov 2007
Nov 27th, 2007
0

Re: help....thx...

Click to Expand / Collapse  Quote originally posted by ricnyx ...
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.

Click to Expand / Collapse  Quote originally posted by ricnyx ...
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.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Nov 27th, 2007
0

Re: help....thx...

C++ Syntax (Toggle Plain Text)
  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??
Reputation Points: 8
Solved Threads: 0
Newbie Poster
ricnyx is offline Offline
21 posts
since Nov 2007
Nov 27th, 2007
0

Re: help....thx...

Only around your code. And it needs to be formatted code.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Nov 27th, 2007
0

Re: help....thx...

omg........7 am..... good ''night''~
Reputation Points: 8
Solved Threads: 0
Newbie Poster
ricnyx is offline Offline
21 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inventory using structs
Next Thread in C++ Forum Timeline: C++ arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC