| | |
help....thx...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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?
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...
•
•
Join Date: Jul 2005
Posts: 1,703
Reputation:
Solved Threads: 274
if you wish to use >> to read the file, which is okay, then use this scheme:
C++ Syntax (Toggle Plain Text)
string dayName double income //read one full line of file each time through the loop while(fin >> dayName) fin >> income
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
case 1 , i write like tis....but seems the program can not run the 2nd while loop
------------------------------------
the output is :
monday 1500
tuesday 1100
0
-----------------------------
no have the sum...wher got problem?
C++ Syntax (Toggle Plain Text)
---------------------------- 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?
Last edited by cscgal; Nov 28th, 2007 at 4:14 pm. Reason: Added code tags
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
case 2 :
------------------------------------------
the output become :
P 2600
--------------------------------------------
any hint or suggestioin? thx ^^
C++ Syntax (Toggle Plain Text)
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 ^^
Last edited by cscgal; Nov 28th, 2007 at 4:15 pm. Reason: Code tags
•
•
Join Date: Jul 2005
Posts: 1,703
Reputation:
Solved Threads: 274
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.
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.
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
sorry for type wrong....
-------------------------------
data inside income.dat:
monday 1500
tuesday 1100
------------------------------
the program i write :
-------------------------------
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...
-------------------------------
data inside income.dat:
monday 1500
tuesday 1100
------------------------------
the program i write :
C++ Syntax (Toggle Plain Text)
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...
Last edited by cscgal; Nov 28th, 2007 at 4:15 pm. Reason: Please use code tags
By reading the Rules, which has been suggested many times to you already. And you obviously missed Read Me: Read This Before Posting too.
And how many times do we have to be reminded of this? We really don't care.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Nov 2007
Posts: 21
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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??
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- LCD vs CRT (Monitors, Displays and Video Cards)
- Review of a simple website. Thx! (Website Reviews)
- PHP, ASP, ColdFusion, what's your fav? (IT Professionals' Lounge)
- Connected ATX and AT PSU (Technical) (Motherboards, CPUs and RAM)
- JTextField (Java)
- Writing to an Access Database using Java... (Java)
- PCMCIA card question......... (Windows NT / 2000 / XP)
- i'll be scarce 'til early October... (Geeks' Lounge)
- Lets start this stuff back up! (DaniWeb Community Feedback)
Other Threads in the C++ Forum
- Previous Thread: Inventory using structs
- Next Thread: C++ arrays
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






