| | |
First year assigment on reading file, sorting and outputting invoice
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Anything written between [code] [/code] will be considered as code.
For example
[code]
#include <iostream>
[/code]
will appear like
For example
[code]
#include <iostream>
[/code]
will appear like
C++ Syntax (Toggle Plain Text)
#include <iostream>
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
Can someone suggest why this for loop is not working as it should?
it returns a value of 0 everytime.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main () { const int MAX_SIZE=5; char station [MAX_SIZE]; int count = 0; do{ cout<<"Enter station:"<<endl; cin>>station[count]; double totalA = 0; if (station [MAX_SIZE] = 'A') { for (int i=0; i<count; i++) totalA= totalA + 2.20; cout<<totalA; } } while (count<MAX_SIZE); system ("pause"); return 0; }
it returns a value of 0 everytime.
You have to increment count:
Change this:
to this:
and this:
to this:
C++ Syntax (Toggle Plain Text)
count = count + 1;
Change this:
C++ Syntax (Toggle Plain Text)
char station [MAX_SIZE];
to this:
string station [MAX_SIZE];and this:
C++ Syntax (Toggle Plain Text)
if (station [MAX_SIZE] = 'A')
to this:
if (station [MAX_SIZE] == "A")
*Voted best profile in the world*
•
•
Join Date: Jul 2005
Posts: 1,758
Reputation:
Solved Threads: 283
if (station [MAX_SIZE] = 'A')
In the above line, station[MAX_SIZE] doesn't exist or is out of bounds/illegal, is bad. The largest valid index is MAX_SIZE minus 1. You probably don't even want MAX_SIZE minus 1 because it won't be initialized until the fifth time through the do/while loop. You probably want count instead of MAX_SIZE.
Keeping station an array of char isn't wrong. Making sation an array of strings isn't necessary, but will also work.
Do yourself and others trying to help by using good indenting and spaces habits. It's much easier to read.
In the above line, station[MAX_SIZE] doesn't exist or is out of bounds/illegal, is bad. The largest valid index is MAX_SIZE minus 1. You probably don't even want MAX_SIZE minus 1 because it won't be initialized until the fifth time through the do/while loop. You probably want count instead of MAX_SIZE.
Keeping station an array of char isn't wrong. Making sation an array of strings isn't necessary, but will also work.
Do yourself and others trying to help by using good indenting and spaces habits. It's much easier to read.
•
•
Join Date: Apr 2006
Posts: 30
Reputation:
Solved Threads: 0
Geez,
thanks everyone.
I have been busy and come up with this. about 5 lines give me errors. So I have not been able to test the thing yet on the data. But I am excited about getting closer!
I have also worked on my indentations. Please check it out.
thanks everyone.
I have been busy and come up with this. about 5 lines give me errors. So I have not been able to test the thing yet on the data. But I am excited about getting closer!
I have also worked on my indentations. Please check it out.
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); time [count]= inputString.substr(11,16-11); count++; } while (count<MAX_SIZE); for (int i=0; i<count; i++) cout <<date[i]<<"\t"<<time[i]<<endl; } void calculateTotalStation (int& MAX_SIZE, char& station, int& count, double& totalA, double& totalB, double& totalC, double& totalD) { station [MAX_SIZE]; count = 0; totalA = 0; totalB = 0; totalC = 0; totalD = 0; do{ if (station [count] == 'A') { totalA= totalA + 2.20; cout<<totalA; } if (station [count] == 'B') { totalB= totalB + 2.80; cout<<totalB; } if (station [count] == 'C') { totalC= totalC + 2.30; cout<<totalC; } if (station [count] == 'D') { totalD= totalD + 3.80; cout<<totalD; } } while (count<MAX_SIZE); } void resetControlTotals(double& controlTotal) { controlTotal = 0; } void printTotalInvoice (int& MAX_SIZE, char& station, double& totalA, double& totalB, double& totalC, double& totalD, double& totalInvoice, double& controlTotal) { station [MAX_SIZE]; totalA = 0; totalB = 0; totalC = 0; totalD = 0; totalInvoice = 0; 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); } 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) printTotalInvoice (MAX_SIZE, station, totalA, totalB, totalC, totalD, totalInvoice); 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); calculateTotalStation (MAX_SIZE, station, count, totalA, totalB, totalC, totalD); resetControlTotals(controlTotal); printTotalInvoice (MAX_SIZE, station, totalA, totalB, totalC, totalD, totalInvoice); 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
Views: 5650 | Replies: 25
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






