I can't figure out what's wrong with my c++ program -
the question is: abc hardware company has hired write program it's account receivable dept. master file in ascending order customer number 20 character customer name , balance due. transaction file contains records of each transaction customer number. read in records 1 @ time 2 files , use transaction file update info master file. process transaction records before going on next master record. if transaction record contains "o" in column 1, calculate orderamount , add balance due. if record contains "p" in column 1, subtract payment balance due. keep running total of ar balance of abc company (the sum of balances each customer). after processing master record , transactions, program should prepare invoice each customer lists customer name, number, previous balance, transactions, , final balance due.
the output should like: customer name customer number previous balance $xxx.xx (all transactions per customer:) transaction# item ordered $order amoount transaction# item ordered $order amount transaction# payment $payment amount balance due $xxx.xx
i've tried changing arrays, if statements etc. nothing's printing @ when run program. please help!
here code have far:
# include <iostream> # include <fstream> # include <iomanip> # include <string> using namespace std; struct master { double custnum; string name; double balance; }; struct transactions { char transtype; int custnum; int transnum; string item; int quantity; double price; double amountpaid; }; int main () { ifstream masterfile ("master.txt"); ifstream transfile ("transaction.txt"); int prevbalance[7]; master main [7]; (int i=0; !masterfile.eof(); i++) { masterfile>>main[i].custnum>>main[i].name>>main[i].balance; } (int i=0;!masterfile.eof();i++) { cout << main[i].custnum<<" "; cout << main[i].name<<" "; cout << main[i].balance<<" "<< endl<<endl; prevbalance[i] = main[i].balance; } double companybalance = 0; double orderamt=0; transactions tran[35]; (int i=0; !transfile.eof(); i++) {{ transfile>> tran[i].transtype; cout<<tran[i].transtype<<" "; if (tran[i].transtype == 'o') { transfile>>tran[i].custnum; cout<<tran[i].custnum<<" "; transfile>> tran[i].transnum; cout<<tran[i].transnum<<" "; transfile>>tran[i].item; cout<<tran[i].item<<" "; transfile>>tran[i].quantity; cout<<tran[i].quantity<<" "; transfile>>tran[i].price; cout<<tran[i].price<<" "<<endl<<endl; orderamt= tran[i].price*tran[i].quantity; main[i].balance+= orderamt; companybalance += main[i].balance; } else if (tran[i].transtype == 'p'){ transfile>>tran[i].custnum; cout<<tran[i].custnum<<" "; transfile>> tran[i].transnum; cout<<tran[i].transnum<<" "; transfile>>tran[i].amountpaid; cout<<tran[i].amountpaid<<endl<<endl<<endl; main[i].balance-tran[i].amountpaid; companybalance += main[i].balance; }} for(int i=0; i<50; i++) { cout<<"name: "<< main[i].name <<" customer #: "<< main[i].custnum<<endl<<endl; cout<<"previous balance "<<prevbalance[i]<<endl; for(int j=0; j<7; j++){ cout<<"transaction #: "<<tran[j].transnum<<" "<<tran[j].item<<" $"<<orderamt<<endl; } cout<<"balance due: "<<main[i].balance<<endl; }
}}
here input 2 files, masterfile:
1000 tiffany 7000.99 2000 mary 6500.98 3000 jacob 6560.99 4000 gene 4560.98 5000 bella 5300.87 6000 anna 2340.90 7000 demi 4230.45
and transaction file:
o 1000 1000 pens 20 2 o 1000 2000 cpus 2 200 o 1000 3000 moniter 2 100 p 1000 4000 4000 p 1000 5000 300 o 2000 6000 cpus 3 500 o 2000 7000 mouse 3 50 o 2000 8000 wires 5 8 p 2000 9000 600 p 2000 1100 798 o 3000 1200 moniters 6 60 o 3000 1300 cpus 7 300 o 3000 1400 mouse 30 40 o 3000 1500 speakers 20 20 p 3000 1600 5000 o 4000 1001 speakers 2 50 o 4000 2002 cables 4 20 p 4000 3003 400 p 4000 4004 500 p 4000 5005 68 p 5000 6001 600 p 5000 4002 55 p 5000 2003 450 o 5000 4004 speakers 4 60 o 5000 1005 laptop 3 300 o 6000 6001 tvs 5 400 o 6000 8002 speakers 5 70 p 6000 6003 2000 p 6000 8004 1000 o 6000 8005 cables 10 15 o 7000 5001 pens 50 2 o 7000 7002 paper 400 2 p 7000 4003 150 p 7000 3004 230 p 7000 6005 450
you're using masterfile.eof()
loop condition twice.
for (int i=0; !masterfile.eof(); i++) { masterfile>>main[i].custnum>>main[i].name>>main[i].balance; } (int i=0;!masterfile.eof();i++) { cout << main[i].custnum<<" "; cout << main[i].name<<" "; cout << main[i].balance<<" "<< endl<<endl; prevbalance[i] = main[i].balance; }
think how couldn't possibly work. you've reached end of file time first loop ends. second loop going finish before starts!
of course reset file pointer beginning better way keep count of number of records have, , use count second loop.
int irecordcount = 0; (irecordcount = 0; !masterfile.eof(); irecordcount++) { masterfile>>main[irecordcount].custnum>>main[irecordcount].name>>main[irecordcount].balance; } (int i=0; < irecordcount;i++) { cout << main[i].custnum<<" "; cout << main[i].name<<" "; cout << main[i].balance<<" "<< endl<<endl; prevbalance[i] = main[i].balance; }
also note you're using fixed array (master main[7]
) store records - happens if have input file more 7 records in it? you'll exceed bounds of array. should change using std::vector
array can grow dynamically, if don't want should put additional check in when reading records make sure don't overflow array:
int irecordcount = 0; (irecordcount = 0; irecordcount < sizeof(main) / sizeof(main[0]) && !masterfile.eof(); irecordcount++) { masterfile>>main[irecordcount].custnum>>main[irecordcount].name>>main[irecordcount].balance; }
you're doing weird when reading transactions. have things like:
orderamt= tran[i].price*tran[i].quantity; main[i].balance+= orderamt;
if i
transaction number (tran[i]
) how can customer number (main[i]
)? you're allowed use variable names other i
know!
you have @ bottom of code:
for(int i=0; i<50; i++) { .... }
again, should use actual number of elements in array (irecordcount
) rather fixed number (and did 50 come since array 7 elements in size?).
Comments
Post a Comment