| | |
First year assigment on reading file, sorting and outputting invoice
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
By the way, I have used this code so far to read first line (ID, station, Nameand Address):
C++ Syntax (Toggle Plain Text)
void readRecord (int& id, string& name, int& item, double& cost, int& qty, bool& more) { //int id; char station; string rest; while(true) { cerr << "reading data and processing to file..."; cin >> id; if(cin.fail()) break; cin >> station; if(cin.fail()) break; cin >> ws; getline(cin, rest); if(cin.fail()) break; }
Last edited by WolfPack; May 27th, 2006 at 12:45 pm.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
You could try something akin to this:
C++ Syntax (Toggle Plain Text)
struct invoice id name and address date and time items container amount total while successful input into id if current id same as prior id continue on same invoice else complete current invoice start new invoice input into item if item equals @ input into name and address else store item in container if desired input into date and time string //At end of loop evaluate stream if EOF found file read in entirety without error else error in reading file
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
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.
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
OK,
I've written this small section of code, as per last suggestion:
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!
I've written this small section of code, as per last suggestion:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main () { const int MAX_LINE = 100; string name [MAX_LINE]; string address [MAX_LINE]; string line; int count =0; do { int position = line.find (','); string name = line.substr (0, position); string address = line.substr (position, MAX_LINE); } while (count<MAX_LINE); for (int i=0; i<count; i++) cout <<name[i]<<"/t"<<address[i]<<endl; system ("pause"); return 0; }
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.
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
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
[
]
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
[
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main () { const int MAX_LINE = 100; string date [MAX_LINE]; string time [MAX_LINE]; string inputString; int count =0; do { int position = inputString.find ('/'); date = inputString.substr(0, 4); time = inputString.substr(7,15-4); } while (count<MAX_LINE); for (int i=0; i<count; i++) cout <<date[i]<<"/t"<<time[i]<<endl; system ("pause"); return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Weird build errors that are not even in my program Please help!!
- Next Thread: C++ Builder EDBEngineError
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates text text-file tree url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






