First year assigment on reading file, sorting and outputting invoice

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

First year assigment on reading file, sorting and outputting invoice

 
0
  #1
May 27th, 2006
Hello,
I am stuck on an assignment I am working on. To be honest, the way I've structured it, it goes a bit beyond me.

Can someone kindly assist?

I have to get the following text:

3435344 @ L Brooks,12 Shaftsbury Road,Burwood NSW 2134
3435344 A 2005/02/22/08:22:41
3435344 A 2005/03/20/08:22:41
3435344 B 2005/03/23/18:22:41
3435344 B 2005/04/10/28:22:41
3435344 C 2005/03/11/08:22:41
3435344 C 2005/05/10/14:22:41
3435344 C 2005/05/19/06:22:43
3435344 D 2005/05/01/01:26:41
3873242 @ N McGoldrick,8 Colless Place,South Melbourne VIC 3205
3873242 B 2005/03/29/02:40:59
3873242 B 2005/05/16/02:40:59
3873242 C 2005/04/07/22:40:59
3873242 D 2005/03/02/02:40:59


...
pipe it to a program that sorts the stations, date time, then outputs an invoice with total amount owing. (I have been told Station A costs 2.20, B costs 2.80 etc...)

I am in first year, first semester. So I have to use easy methods.

I have made functions which basically: initialise, printHeadings (such as customer name and ID), readRecords (id, date, time, station), process Record (get station, times quantity of times entered by relevant charge), printInvoice (total for each station, then total for invoice).

My first problem:
How do I read from this file data to store into arrays, the two separate types of line (ie one has address, other has station, date and time). Do I do this within the one function or split it up into 2 functions? (One line I will use for just typing up customer details, the other I will use further to calculate amount to invoice....

Thanks for your time.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #2
May 27th, 2006
By the way, I have used this code so far to read first line (ID, station, Nameand Address):

  1. void readRecord (int& id, string& name, int& item, double& cost, int& qty, bool& more)
  2. {
  3. //int id;
  4. char station;
  5. string rest;
  6. while(true) {
  7. cerr << "reading data and processing to file...";
  8. cin >> id;
  9. if(cin.fail()) break;
  10. cin >> station;
  11. if(cin.fail()) break;
  12. cin >> ws;
  13. getline(cin, rest);
  14. if(cin.fail()) break;
  15.  
  16. }
Last edited by WolfPack; May 27th, 2006 at 12:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #3
May 27th, 2006
You could try something akin to this:
  1. struct invoice
  2. id
  3. name and address
  4. date and time
  5. items container
  6. amount total
  7.  
  8. while successful input into id
  9. if current id same as prior id
  10. continue on same invoice
  11. else
  12. complete current invoice
  13. start new invoice
  14.  
  15. input into item
  16.  
  17. if item equals @
  18. input into name and address
  19. else
  20. store item in container if desired
  21. input into date and time string
  22.  
  23. //At end of loop evaluate stream
  24. if EOF found
  25. file read in entirety without error
  26. else
  27. error in reading file
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #4
May 27th, 2006
Thanks for your help. I will begin working with this.
However, as stated previously I also have problem on how to insert those string into arrays.
The above code I wrote, only enters single values. I need to make parallel arrays, so can someone guide me with how a string is passed into an array?

if item equals @
input into name and address //this shouldnt be too hard for me but...
else
store item in container if desired
input into date and time string //...how do I store the date and time?

THANKS.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #5
May 28th, 2006
Well do you know how to use a struct or class. Classes are often preferred in c++?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #6
May 28th, 2006
We havent studied classes yet. I was trying to figure out how to use substr and get.line to store strings into parallel arrays. (I know this in theory but cant find HOW to do it). So, I would like to find character '@', anything after that is the Name, Address.
Then anything that is not '@' (ie will be a station ID such as A, B, C...), anything after that will be my date and time).
IN THEORY. Again, I dont know the code. If someone could kindly tell me how substr is organised or how I get a string and store it into an array, that's what I was invisaging. Of-course open to other recommendations.

(I am nervous I wont finish this by Friday).
Renee
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #7
May 28th, 2006
Well I guess the next question would be, do you know, or are you allowed to use, parts of the STL. Namely, vectors?

I was thinking a vector of strings may be useful for your particular problem?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #8
May 28th, 2006
Using >> to read in id and station and getline() to read in the string after station seems reasonable. You should be aware however, that >> will leave the delimiter char between inputs (in this case space char) in the input buffer, and the delimiting char will not be ignored by getline(), like it will be by another call to >>. Therefore, clearing the input buffer by calling ignore() with the appropriate parameter(s) before the call to getline() would be a good idea.

getline() will put everything after the station field on a given line of the file into a single string. You may or may not need to parse (break up) the input original input string into substrings depending on how you want to use the information in the string. The first set of substrings are delimited by commas in the name/address string and by slashes in the date/time string. Then within the name/address string you have spaces between first name and last name, and colon between hour, minute, second in the time string if you want to break the information down into even more discrete information fields. I think you could probably do one of the following to break the input strings into desired substrings based on the delimeter used, IF you need to:

1) If using STL strings, use find() to find all the delimiters in the input string and then substr() to extract each substring between any two consecutive delimiters.
2) If using C style strings use strtok() to find substrings based on the delimiters
3) Use an istringstream with getline() to find substrings based on delimiters
4) write your own function using delimiters to separate input string into substrings.

If you've never heard of 1-3 before, then you are probably expected to do 4 as part of the learning exercise.

If you don't know about structs or classes yet, then keeping all this information in parrallel arrays/vectors seems a possible solution to keeping all this information in memory until you can do something with it, like print a report. For example, you could have an array for each of the fields to be printed in the report printing out a single line of information for each ID. The report may or may not use all of the fields available in the input data file. Based on your description of the report it looks like you will need the following information: id, name/address for a given ID, number of station A per ID, number of station B per ID, number of station C per ID, etc, and total amount per ID. So I would delcare appropriate arrays/vectors for each of those fields. As you are reading the file and find a station char (A, B, C, etc) you could increment the appropriate value stored in the appropriate index of the appropriate array for that ID, irrespective of the date the transaction occurred. Once the invoice for a given ID is compeleted (or once the file is completely read if that's more appropriate) you can then calculate the total amount for each ID based on the number of each station in each array for that ID times the value of each station (provided elsewhere) and store the amount in the appropriate index of the amount array/vector.

NB: my resources indicate that substr() has several variants:

1) substr() returns a copy of the string calling substr()

2) substr(x) returns a string that starts at index x in the calling string and includes all of the remaining char in the original strings

3) substr(x, y) returns a string that starts at index x and includes, at most, y char.

Therefore if you knew that there were two delimiters (say / is the delimiter) in a given input string (say abc/defg/hij is the input string) at indexes 3 and 8, then parsing the string into three substrings based on position of the delimeters using substr() would probably be:

substring1 = inputString.substr(0, 3);
substring2 = inputString.substr(4, 7 - 3);
substring3 = inputString.substr(9);

if I didn't make a mistake.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #9
May 28th, 2006
OK,
I've written this small section of code, as per last suggestion:


  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.  
  8. const int MAX_LINE = 100;
  9. string name [MAX_LINE];
  10. string address [MAX_LINE];
  11. string line;
  12. int count =0;
  13.  
  14. do {
  15. int position = line.find (',');
  16. string name = line.substr (0, position);
  17. string address = line.substr (position, MAX_LINE);
  18. }
  19. while (count<MAX_LINE);
  20.  
  21. for (int i=0; i<count; i++)
  22. cout <<name[i]<<"/t"<<address[i]<<endl;
  23.  
  24. system ("pause");
  25. return 0;
  26. }


It looks good to me, but it wont run. I think it goes in a loop or something because my program makes the .cpp crash out.

Can you suggest further on this? I will now go and do a program for storing the date and time as well.

APPRECIATE YOUR HELP BY THE WAY!
Last edited by WolfPack; May 28th, 2006 at 8:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 30
Reputation: newgurl is an unknown quantity at this point 
Solved Threads: 0
newgurl newgurl is offline Offline
Light Poster

Re: First year assigment on reading file, sorting and outputting invoice

 
0
  #10
May 28th, 2006
OK,

This one is giving me the error:
16 "incompatible types in assignment of `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `std::string[100]' "

It look like it wants me to remove reference to MAX_LINE from string date declaration....but dont I need that to store into array?

Also, with the substring numbers, have I set them out right for this inputString:

3435344 A 2005/02/22/08:22:41


[
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.  
  8. const int MAX_LINE = 100;
  9. string date [MAX_LINE];
  10. string time [MAX_LINE];
  11. string inputString;
  12. int count =0;
  13.  
  14. do {
  15. int position = inputString.find ('/');
  16. date = inputString.substr(0, 4);
  17. time = inputString.substr(7,15-4);
  18. }
  19. while (count<MAX_LINE);
  20.  
  21. for (int i=0; i<count; i++)
  22. cout <<date[i]<<"/t"<<time[i]<<endl;
  23.  
  24. system ("pause");
  25. return 0;
  26. }
]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC