| | |
First year assigment on reading file, sorting and outputting invoice
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
[B]Lerner[B],
If I make changes to make station[count], then will it be ok to have code like this for that section:
If I make changes to make station[count], then will it be ok to have code like this for that section:
C++ Syntax (Toggle Plain Text)
void calculateTotalStation (int& MAX_SIZE, string& station, int& count, double& totalA, double& totalB, double& totalC, double& totalD) { count = 0; station [count]; totalA = 0; totalB = 0; totalC = 0; totalD = 0; do{ if (station [count] == 'A') { totalA= totalA + 2.20; cout<<totalA; }
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
I would have a series of arrays of int called stationA, stationB, stationC all initialized to zero. As I was reading the data from file, each time a stationA was found for any given ID i would increase the int at index i for stationA. Else if a stationB was found for any given ID I would increment the value at index of stationB with same index as given ID, etc. Then when I wanted to calculate the total of the invoice it would be a calculation such as:
amount to put at index i for array totalAmount is (number at index i in stationA * value of stationA) plus (number at index i in stationB * value of stationB) plus etc
amount to put at index i for array totalAmount is (number at index i in stationA * value of stationA) plus (number at index i in stationB * value of stationB) plus etc
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
In reviewing your post with the "entire" program, it looks as though you have abanded the idea of storing all the data in parallel arrays and printing out the results all at once after the entire file has been read in favor of printing out an invoice for a given ID when a new ID has been found, which is fine, but you won't need arrays for stationA, stationB, etc. under the latter scenario.
I note that there is no mechanism to read from file in the program posted so that mechanism will need to be added at some point if you are going to be getting the data from a file.
It also looks like the while loop in main() is terminated early and the body of the if() statements terminate prematurely for lack of controlling brackets, but it's difficult to say for sure without comments.
In the following, it looks like you are trying to save all the stations for a given ID in an array and then calculate the station totals from there. That's viable. However, to pass an array of strings you would use string * station, not string & station and MAX_SIZE was declared global, so it doesn't need to be passed anywhere. (Globals should be avoided when possible, but this is one situation where I find using them at least tolerable).
Here's how I would use an array of string called station to do the calculations:
I note that there is no mechanism to read from file in the program posted so that mechanism will need to be added at some point if you are going to be getting the data from a file.
It also looks like the while loop in main() is terminated early and the body of the if() statements terminate prematurely for lack of controlling brackets, but it's difficult to say for sure without comments.
In the following, it looks like you are trying to save all the stations for a given ID in an array and then calculate the station totals from there. That's viable. However, to pass an array of strings you would use string * station, not string & station and MAX_SIZE was declared global, so it doesn't need to be passed anywhere. (Globals should be avoided when possible, but this is one situation where I find using them at least tolerable).
C++ Syntax (Toggle Plain Text)
void calculateTotalStation (int& MAX_SIZE, string& station, int& count, double& totalA, double& totalB, double& totalC, double& totalD) { //count appears to be the number of stations visit by this ID //the array of stations and the count are passed in, so the //following two lines aren't needed. count = 0; //this would mean no stations will be visited station [count]; //to my knowledge this is an illegal statement //this will set all station totals to 0 totalA = 0; totalB = 0; totalC = 0; totalD = 0; do { if (station [count] == 'A') //station[count] is a string so use "A" { totalA= totalA + 2.20; cout<<totalA; }
Here's how I would use an array of string called station to do the calculations:
C++ Syntax (Toggle Plain Text)
void calculateTotalStation (string * station, int count, double& totalA, double& totalB, double& totalC, double& totalD) { totalA = 0; totalB = 0; totalC = 0; totalD = 0; int i = 0; do { if (station [i] == "A") { totalA= totalA + 2.20; cout<<totalA; } else if(station[i] == "B") . . . }while(i < count) }
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
I really want to print out total for id xx is $....
Total for id YY is $....
I have amended code.
Is this looking closer to it?
I have made comments where I get errors. My declarations here and there are weird now...
Thanks for your help....
Total for id YY is $....
I have amended code.
Is this looking closer to it?
I have made comments where I get errors. My declarations here and there are weird now...
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string.h> using namespace std; void printGenericHeading(int&, int&); void printCustomerHeading(int&, int&); void readDateTime (int& , int& , string& , string& , string& ,int& , int& ); void calculateTotalStation (int& , char& , int& , double& , double& , double& , double& ); void resetControlTotals(double&); void printTotalInvoice (int& , char& , double& , double& , double& , double& , double& ); const int MAX_DETAIL_LINES =100; const int MAX_LINE = 100; const int MAX_SIZE = 20; double TotalInvoice = 0; int pageCount = 0; int lineCount = 0; bool more= true; void printGenericHeading(int& pageCount, int& lineCount) { pageCount = pageCount +1; cout<<"INVOICE FOR TOLL EXPENSES"<<endl; cout<<"_________________________"<<endl; cout<<"TAG\tCUSTOMER\tSTATION" <<"\t\t\t\t\n"; cout<<"ID\tNAME\t\tID\n"; lineCount = 4; } void printCustomerHeading(int& pageCount, int& lineCount) { pageCount = pageCount +1; cout<<"\t\t\t\t\n"; cout<<"t\t\t\t\tDATE\t\tTIME\tAMOUNT\n\n"; lineCount = 3; } void readDateTime (int& MAX_LINE, int& MAX_SIZE, string * date, string * time, string& inputString,int& count, int& position) { date [MAX_SIZE]; time [MAX_SIZE]; inputString; count =0; do { cerr<<"processing..."<<endl; getline(cin, inputString); if(cin.fail()) break; int position = inputString.find ('/'); date [count] = inputString.substr(0, 10); // this line gave error so I change string & to string * date) time [count]= inputString.substr(11,16-11); //this line gave error so I change string & to string * time) count++; } while (count<MAX_SIZE); for (int i=0; i<count; i++) cout <<date[i]<<"\t"<<time[i]<<endl; } void calculateTotalStation (string * station, int count, double& totalA, double& totalB, double& totalC, double& totalD, double& totalInvoice, double& controlTotal) { totalA = 0; totalB = 0; totalC = 0; totalD = 0; int i = 0; do { if (station [i] == "A") { totalA= totalA + 2.20; cout<<totalA; } else if(station[i] == "B") { totalB= totalB + 2.80; cout<<totalB; } else if(station[i] == "C") { totalC= totalC + 2.30; cout<<totalC; } else if(station[i] == "D") { totalD= totalD + 3.80; cout<<totalD; } totalInvoice = totalA + totalB + totalC + totalD; cout<<"TOTAL:\t\t\t\t\t\t\$"<<totalInvoice<<endl; cout<<"\t\t\t\t\t\t_______"<<endl; cout<<"\t\t\t\t\t\t_______"<<endl; cout<<"We thankyou for your prompt payment."<<endl; resetControlTotals(controlTotal); }while(i < count); } void resetControlTotals(double& controlTotal) { controlTotal = 0; } int main () { int id; int prevId; string name; string prevName; double invoiceTotal; double controlTotal; int pageCount; int lineCount; prevId =id; prevName=name; while(more) { if (id!=prevId) calculateTotalStation (MAX_SIZE, station, count, totalA, totalB, totalC, totalD); //this line gives error (STATION UNDECLARED) prevId= id; prevName = name; } if (lineCount >MAX_DETAIL_LINES) printGenericHeading(pageCount, lineCount); printCustomerHeading(pageCount, lineCount); readDateTime (MAX_LINE, MAX_SIZE, date, time, inputString, count, position); // this line gives error of date undeclared calculateTotalStation (MAX_SIZE, station, count, totalA, totalB, totalC, totalD);// this line gives error of station undeclared resetControlTotals(controlTotal); printTotalInvoice (MAX_SIZE, station, totalA, totalB, totalC, totalD, totalInvoice);// this line gives error of station undeclared system("pause"); return 0; }
Thanks for your help....
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
You asked if I had abandoned the idea of using parallel arrays.
I havent. I just am walking in the dark here and only getting to understand the whole concept of parallel arrays.
I hope this final version is pretty close to how it should be, since I have to hand it in tonight (Australia time). but I also have to 'clean up' my hierarchy chart, PSEUDOCODE, and do a deskcheck chart, once I am happy the program is as close as I can get it).
Thanks for your time.
So am I almost there?
I havent. I just am walking in the dark here and only getting to understand the whole concept of parallel arrays.
I hope this final version is pretty close to how it should be, since I have to hand it in tonight (Australia time). but I also have to 'clean up' my hierarchy chart, PSEUDOCODE, and do a deskcheck chart, once I am happy the program is as close as I can get it).
Thanks for your time.
So am I almost there?
![]() |
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
Views: 5649 | Replies: 25
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






