Hello! Easy help please.

Hello! I have a text file like this:
1
2
3
4
5
6
7
2010-05-07,48640,10SW,$10.00,Washington
2010-07-08,48606,5N,$0.75,Fuller
2010-07-31,48441,15S,$2.35,Lincoln
2010-02-28,49971,12NW,$8.23,Jefferson
2010-10-01,48708,4NE,$4.44,Madison
2010-11-03,48708,25SE,$0.99,Doe
2010-05-09,48464,2W,$8.99,Smith


and my job is to separate it by getting rid of the commas and then putting it into a format like
Date:
Zip Code:
Direction:
Cost:
Customer Name:


My only problem is I do not know how to
1) remove the commas so they arent displayed on file
2) Format it so each one is in the format I said above

I am not looking for someone to do it for me, just set me in the right direction. Thanks!
First, this is a good situation to make use of classes, with appropriate members (variables belonging to the class).

Now, since each set of values is separated by line, you would want to use getline(), and store this line in a string.

Now, you want to interpret this line. Since each value is separated by a comma, and each value is in the same order, you can read in characters from the line to a buffer, continuing to read while the character is NOT a comma (ie. while( myString[i] != ',') ).

Here are a few things you should look at.

http://www.cplusplus.com/reference/fstream/ifstream/
http://www.cplusplus.com/reference/istream/istream/getline/

A little example of reading in a line to a string. Note that if you want to read consecutive lines, you can. If you call the function getline, it will read in the next line.

1
2
3
4
    string buffer;
    ifstream myFile("foo.txt", ios::in);
    getline(myFile, buffer);
    cout << "buffer: " << buffer << endl;


Last edited on
Topic archived. No new replies allowed.