Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,132 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,795 people online right now. Registration is fast and FREE... Join Now!




File I/O

 
Reply to this topicStart new topic

File I/O, Reading from a file

noleorr
16 Apr, 2007 - 05:03 AM
Post #1

New D.I.C Head
*

Joined: 4 Apr, 2007
Posts: 11


My Contributions
In my assignment, I am supposed to create an input file and then read it into a c++ program. The following is what I have so far and at the end is the file I created. Can anyone tell me if I am on the right track?
CODE

#include <fstream>
#include <iostream>
using namespace std;
int main()
{    string name;
     int number, begBalance, ccCharges, paid, finCharges, curBalance, num_cust, sum;
     ifstream infile;
     ofstream outfile;
     infile.open("customer.dat");
     assert(!infile.fail());
     outfile.open("summary.dat");
     infile >> name;
     while (! infile.eof())
     {
           infile >> number >> name >> begBalance >> ccCharges >> paid >> finCharges;
           curBalance = (begBalance+(ccCharges-paid)+finCharges);
           cout << number << endl;
     outfile << number << endl;
     infile >> name;
     }
     infile.close();
     outfile.close();
     }


customer.dat

1000 West 5150.50 375.75 85.15 12.18
2000 Cummins 7505.80 718.15 70.87 15.15
3000 Matson 3643.75 600.00 93.54 9.85
4000 Sutton 1587.50 954.23 105.78 26.97
5000 Orr 4578.65 397.85 85.00 19.17
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: File I/O
16 Apr, 2007 - 05:10 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
You are more or less on the right track...first, however, it should be noted that you will lose all the fractional components of your variables, as you've declared them as integers - you may want to declare them as doubles or floats.
User is offlineProfile CardPM
+Quote Post

noleorr
RE: File I/O
16 Apr, 2007 - 05:17 AM
Post #3

New D.I.C Head
*

Joined: 4 Apr, 2007
Posts: 11


My Contributions
QUOTE(Amadeus @ 16 Apr, 2007 - 06:10 AM) *

You are more or less on the right track...first, however, it should be noted that you will lose all the fractional components of your variables, as you've declared them as integers - you may want to declare them as doubles or floats.


Thanks, can you tell me why when I run this program it justs outputs a stream of "1000" ? I appreciate you helping me through this
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: File I/O
16 Apr, 2007 - 05:42 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Try the following:
CODE

using namespace std;
int main()
{
string name;
int number;
float begBalance, ccCharges, paid, finCharges, curBalance, num_cust, sum;
ifstream infile;
ofstream outfile;
infile.open("customer.dat");
assert(!infile.fail());
outfile.open("summary.dat");
while (! infile.eof())
{
infile >> number >> name >> begBalance >> ccCharges >> paid >> finCharges;
curBalance = (begBalance+(ccCharges-paid)+finCharges);
cout << number << endl;
outfile << number << endl;
}
infile.close();
outfile.close();
return 0;
}

you were taking in extra variables...
User is offlineProfile CardPM
+Quote Post

noleorr
RE: File I/O
16 Apr, 2007 - 06:39 AM
Post #5

New D.I.C Head
*

Joined: 4 Apr, 2007
Posts: 11


My Contributions
QUOTE(Amadeus @ 16 Apr, 2007 - 06:42 AM) *

Try the following:
CODE

using namespace std;
int main()
{
string name;
int number;
float begBalance, ccCharges, paid, finCharges, curBalance, num_cust, sum;
ifstream infile;
ofstream outfile;
infile.open("customer.dat");
assert(!infile.fail());
outfile.open("summary.dat");
while (! infile.eof())
{
infile >> number >> name >> begBalance >> ccCharges >> paid >> finCharges;
curBalance = (begBalance+(ccCharges-paid)+finCharges);
cout << number << endl;
outfile << number << endl;
}
infile.close();
outfile.close();
return 0;
}

you were taking in extra variables...


Thanks, that seem to fix it. Now I can move on trying to work with outputting to a file and to the terminal. I appreciate all of your help.
User is offlineProfile CardPM
+Quote Post

noleorr
RE: File I/O
16 Apr, 2007 - 08:20 AM
Post #6

New D.I.C Head
*

Joined: 4 Apr, 2007
Posts: 11


My Contributions
QUOTE(noleorr @ 16 Apr, 2007 - 07:39 AM) *

QUOTE(Amadeus @ 16 Apr, 2007 - 06:42 AM) *

Try the following:
CODE

using namespace std;
int main()
{
string name;
int number;
float begBalance, ccCharges, paid, finCharges, curBalance, num_cust, sum;
ifstream infile;
ofstream outfile;
infile.open("customer.dat");
assert(!infile.fail());
outfile.open("summary.dat");
while (! infile.eof())
{
infile >> number >> name >> begBalance >> ccCharges >> paid >> finCharges;
curBalance = (begBalance+(ccCharges-paid)+finCharges);
cout << number << endl;
outfile << number << endl;
}
infile.close();
outfile.close();
return 0;
}

you were taking in extra variables...


Thanks, that seem to fix it. Now I can move on trying to work with outputting to a file and to the terminal. I appreciate all of your help.


I am trying to figure out how I am going to seperate and distinguish the different data in my file. Does it have to do with a get command or a string command?

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: File I/O
16 Apr, 2007 - 09:28 AM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Are the values not appearing in the correct variables?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month