Welcome to Dream.In.Code
Become a C++ Expert!

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




file input/output

2 Pages V  1 2 >  
Reply to this topicStart new topic

file input/output, Really stuck

mistic857
28 Nov, 2006 - 05:23 AM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
I got this far and I just don't know what to do next.
I have items in a file and I need to add them up, and then save them to a file.
I can get them to show up and that is all.....it always comes out zero.
I am really stuck on this one, I have no idea even how to go about it.
Can some one help me out?

thanks....

CODE

include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{



    float price =0;
    char items;
    float total;
        
  string line;
  ifstream cost;

// name of file

  cost.open("cost.txt");
  if (cost.is_open())
  {
    while (! cost.eof() )

    {
    total = price + price;
      getline (cost,line);
      cout << line << endl;
    }
    cost.close();
  }

  else cout << "Unable to open file";
  
  
  
  cout << total << endl;
    





  return 0;
}

User is offlineProfile CardPM
+Quote Post

horace
RE: File Input/output
28 Nov, 2006 - 05:51 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
should the statement
CODE

total = price + price;

be
CODE

total = total + price;

also where do read the value of price?
make sure you initialise total to 0 when you define it

This post has been edited by horace: 28 Nov, 2006 - 05:52 AM
User is offlineProfile CardPM
+Quote Post

mistic857
RE: File Input/output
28 Nov, 2006 - 05:58 AM
Post #3

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
QUOTE(horace @ 28 Nov, 2006 - 06:51 AM) *

should the statement
CODE

total = price + price;

be
CODE

total = total + price;

also where do read the value of price?
make sure you initialise total to 0 when you define it



Yes, I did change it to
total=total + price
I also set total = 0
Price=0
I still get 0....

"also where do read the value of price?"

I think this is where I am getting stuck. I am not sure how to go about it
to read it.


This post has been edited by mistic857: 28 Nov, 2006 - 06:00 AM
User is offlineProfile CardPM
+Quote Post

BitByte
RE: File Input/output
28 Nov, 2006 - 06:14 AM
Post #4

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
Because you are reading the line, which is a string. Then you are adding total to price, which are both floats with the value 0. So your output will be zero. There are no values in them.

Edit: What is your text file like?

This post has been edited by BitByte: 28 Nov, 2006 - 06:21 AM
User is offlineProfile CardPM
+Quote Post

bluesuus
RE: File Input/output
28 Nov, 2006 - 06:58 AM
Post #5

D.I.C Head
Group Icon

Joined: 26 Dec, 2005
Posts: 57


Dream Kudos: 25
My Contributions
hey ma in the first place why are u using the getline() function.You are not interested in reading the whole.Use the get() function to read in the ( int, double or float values) one by one.So the conclusin us read the values one by one using get() till u reach eof.
User is offlineProfile CardPM
+Quote Post

horace
RE: File Input/output
28 Nov, 2006 - 06:58 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
QUOTE(mistic857 @ 28 Nov, 2006 - 01:58 PM) *


"also where do read the value of price?"

I think this is where I am getting stuck. I am not sure how to go about it
to read it.

what does your data file look like, if it is a list of prices such as
10.0 20.0 30.0
40.0 50.0
etc
you replace your statement
CODE

getline (cost,line);

with something like
CODE

cost >> price;

then add price to total, etc

User is offlineProfile CardPM
+Quote Post

mistic857
RE: File Input/output
28 Nov, 2006 - 07:05 AM
Post #7

New D.I.C Head
*

Joined: 1 Oct, 2006
Posts: 45


My Contributions
QUOTE(BitByte @ 28 Nov, 2006 - 07:14 AM) *

Because you are reading the line, which is a string. Then you are adding total to price, which are both floats with the value 0. So your output will be zero. There are no values in them.

Edit: What is your text file like?



My text file is
sale items

Paper 10.00
dog food 15.00

I just kept it simple so I could figure it out.

QUOTE(horace @ 28 Nov, 2006 - 07:58 AM) *

QUOTE(mistic857 @ 28 Nov, 2006 - 01:58 PM) *


"also where do read the value of price?"

I think this is where I am getting stuck. I am not sure how to go about it
to read it.

what does your data file look like, if it is a list of prices such as
10.0 20.0 30.0
40.0 50.0
etc
you replace your statement
CODE

getline (cost,line);

with something like
CODE

cost >> price;

then add price to total, etc



So complete replace that line?
Do I have to change my variable to a sting?
I really am confused....

User is offlineProfile CardPM
+Quote Post

BitByte
RE: File Input/output
28 Nov, 2006 - 07:33 AM
Post #8

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
If you use getline, it will read the whole line, text and the value. You will have to put a delimiter after the paper or put the value onto another line. Unless you get the whole line and extract the value. But that might be a bit complicated for you. I don't know. For the delimiter use a character like ':' or something. So your getline will read:

CODE
getline( cost, line, ':' );


Then read the price:

CODE
cost >> price;


And your text file will look like this:

Paper:12.00
Dog food:15.00

This post has been edited by BitByte: 28 Nov, 2006 - 07:34 AM
User is offlineProfile CardPM
+Quote Post

okyup
RE: File Input/output
28 Nov, 2006 - 08:35 AM
Post #9

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
It is best to use getline and then manipulate the strings. You can use atoi(string.c_str()) to convert a string into an int. It is bad programming if you use a simpler method that relies on the text file always being in the exact format and without error, because in the real world your text file will change. If there are errors in the text file you can detect them and escape with getline. wink2.gif
User is offlineProfile CardPM
+Quote Post

BitByte
RE: File Input/output
28 Nov, 2006 - 08:54 AM
Post #10

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
QUOTE
It is best to use getline and then manipulate the strings. You can use atoi(string.c_str()) to convert a string into an int. It is bad programming if you use a simpler method that relies on the text file always being in the exact format and without error, because in the real world your text file will change. If there are errors in the text file you can detect them and escape with getline.


So your telling us it's bad programming practice to use a delimiter to read a string. LOL. A program is designed to read a text file how it is meant to be read. What happens if the string contains hundreds of letters or values. Are you going to convert them all to integers? Then what is the ouput going to be like? If the text file is wrong then you get a data mismatch. That's what eofbit, badbit failbit is for. If the file is in the wrong format then you should not read the file. If i wrote a program to read a file in a specified way, and that file is wrong, then i would not change my program to be allowed to read it.
User is offlineProfile CardPM
+Quote Post

horace
RE: File Input/output
28 Nov, 2006 - 08:55 AM
Post #11

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
if you have an existing data file of the form
Paper 10.00
cat food 56
buns 67
dog food 15.00
cake 45.0

you could read it so
CODE

  while (! cost.eof() )
    {
    //  attemp to read "paper 10.0" or "dog food 20.0"
    cost >> line;          // read first string on line
    if(!(cost >> price))   // attempt read price
      {                    // if it fails read next string then price
      string line2;
      cost.clear();        // clear error condition
      cost >> line2;       // read next string
      line = line + " " + line2;  // add on end of existing string
      cost >> price;       // then read price
      }
    total = total + price;
     // getline (cost,line);
      cout << line << " cost " << price << endl;
    }

which when ran gave
Paper cost 10
cat food cost 56
buns cost 67
dog food cost 15
cake cost 45
total = 193

however, the code assume lines in data.txt are one of
text number
text text number

anthing else will fail so you would need to validate the input further

User is offlineProfile CardPM
+Quote Post

BitByte
RE: File Input/output
28 Nov, 2006 - 09:05 AM
Post #12

D.I.C Head
**

Joined: 9 Aug, 2006
Posts: 194



Thanked: 3 times
My Contributions
exactly, the program is designed to read the file in the specified format. If it's not right, don't read it. It's like a databse file, it's read and written a certain way. If someone decides to change the file in some way, then the program cannot read it. So in the real world, yes files can change, but your program will have a hell of a time trying to read it.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/8/09 08:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

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