简体   繁体   中英

I can't figure out what's wrong with my c++ program

The question is: The ABC Hardware Company has hired you to write a program for it's Account Receivable dept. The master file is in ascending order by customer number with a 20 character customer name and balance due. The transaction file contains records of each transaction with customer number. You are to read in records one at a time from the two files and use the transaction file to update the info from the master file. Process all transaction records before going on to the next master record. If the transaction record contains an "O" in column 1, calculate the orderamount and add it to the balance due. If the record contains a "P" in column 1, subtract the payment from the balance due. Keep a running total of the AR balance of the ABC Company (the sum of balances for each customer). After processing a master record and all its transactions, the program should prepare an invoice for each customer which lists the customer name, number, previous balance, all transactions, and the final balance due.

The output should look 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 up the arrays, the if statements etc. Now nothing's printing at all when I run the program. Please help!

Here is the code I have so 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];

for (int i=0; !masterfile.eof(); i++) {
    masterfile>>main[i].custnum>>main[i].name>>main[i].balance;
    }

    for (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];
for (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 are the input two 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() as a loop condition twice .

for (int i=0; !masterfile.eof(); i++) {
    masterfile>>main[i].custnum>>main[i].name>>main[i].balance;
}

for (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 about how this couldn't possibly work. You've already reached the end of the file by the time the first loop ends. The second loop is going to finish before it even starts!

Of course you could reset the file pointer to the beginning but a much better way is to keep a count of the number of records you have, and use that count for the second loop.

int iRecordCount = 0;
for (iRecordCount = 0; !masterfile.eof(); iRecordCount++) {
    masterfile>>main[iRecordCount].custnum>>main[iRecordCount].name>>main[iRecordCount].balance;
}

for (int i=0; i < iRecordCount;i++) {
    cout << main[i].custnum<<" ";
    cout << main[i].name<<" ";
    cout << main[i].balance<<" "<<
    endl<<endl;
    prevbalance[i] = main[i].balance;
}

Also note that you're using a fixed array ( master main[7] ) to store your records - what happens if you have an input file with more than 7 records in it? You'll exceed the bounds of the array. Really you should change to using a std::vector so that the array can grow dynamically, but if you don't want to do that you should put an additional check in when reading the records to make sure you don't overflow the array:

int iRecordCount = 0;
for (iRecordCount = 0; iRecordCount < sizeof(main) / sizeof(main[0]) && !masterfile.eof();
    iRecordCount++) {
    masterfile>>main[iRecordCount].custnum>>main[iRecordCount].name>>main[iRecordCount].balance;
}

You're also doing something weird when reading your transactions. You have things like:

orderamt= tran[i].price*tran[i].quantity;
main[i].balance+= orderamt;

If i is the transaction number ( tran[i] ) how can it also be the customer number as well ( main[i] )? You're allowed to use variable names other than i you know!

You also have this at the bottom of your code:

for(int i=0; i<50; i++) {
....
}

Again, you should use the actual number of elements in the array ( iRecordCount ) rather than a fixed number (and where did 50 come from since your array is only 7 elements in size?).

In addition to the suggestions made by @JonathanPotter in his answer , I want to add that using ifstream.eof() in the for loop is error prone.

Useful reading material: Why is “while ( !feof (file) )” always wrong? .

Use of !masterfile.eof() in the conditional of a for loop is wrong for exactly the same reasons.

It's better to use:

std::vector<master> masterData;
while ( masterfile )
{
   // Try to read the data into a temporary object.
   master m;
   masterfile >> m.custnum >> m.name >> m.balance;

   // If the input was successful, add it to masterData
   if ( masterfile )
   {
      masterData.push_back(m);
   }
}

You can use similar logic to read the transaction records too.

Also, you have a line:

 main[i].balance - tran[i].amountpaid;

I am sure that is a typo and you meant to use:

 main[i].balance -= tran[i].amountpaid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM