![](/img/trans.png)
[英]My program is printing the wrong thing after completing, and I can't figure out why. C++
[英]I can't figure out what's wrong with my c++ program
問題是:ABC 硬件公司聘請您為其應收帳款部門編寫程序。 主文件按客戶編號升序排列,包含 20 個字符的客戶名稱和到期余額。 交易文件包含每筆交易和客戶編號的記錄。 您將一次從兩個文件中讀取一條記錄,並使用事務文件更新主文件中的信息。 在處理下一個主記錄之前處理所有交易記錄。 如果交易記錄在第1列中包含“O”,則計算訂單金額並將其添加到到期余額中。 如果記錄在第 1 列中包含“P”,則從應付余額中減去付款。 保持 ABC 公司的應收賬款余額的運行總計(每個客戶的余額總和)。 處理完主記錄及其所有交易后,程序應為每個客戶准備一張發票,其中列出客戶姓名、編號、以前的余額、所有交易和最終到期余額。
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
我試過更改數組、if 語句等。現在運行程序時根本沒有打印任何內容。 請幫忙!
這是我到目前為止的代碼:
# 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;
}
}}
這是輸入的兩個文件,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
和交易文件:
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
您使用masterfile.eof()
作為循環條件兩次。
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;
}
想想這怎么可能行不通。 到第一個循環結束時,您已經到達文件的末尾。 第二個循環將在它開始之前完成!
當然,您可以將文件指針重置為開頭,但更好的方法是記錄您擁有的記錄數,並將該計數用於第二個循環。
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;
}
另請注意,您正在使用固定數組( master main[7]
)來存儲您的記錄 - 如果您的輸入文件中有超過 7 條記錄,會發生什么? 您將超出數組的邊界。 實際上,您應該更改為使用std::vector
以便數組可以動態增長,但是如果您不想這樣做,則應該在讀取記錄時進行額外檢查以確保不會溢出數組:
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;
}
您在閱讀交易時也在做一些奇怪的事情。 你有這樣的事情:
orderamt= tran[i].price*tran[i].quantity;
main[i].balance+= orderamt;
如果i
是交易編號( tran[i]
),它又怎么可能是客戶編號( main[i]
)? 你可以使用你知道的i
以外的變量名!
你的代碼底部也有這個:
for(int i=0; i<50; i++) {
....
}
同樣,您應該使用數組中元素的實際數量 ( iRecordCount
) 而不是固定數量(因為您的數組只有 7 個元素,所以 50 是從哪里來的?)。
除了@JonathanPotter在他的回答中提出的建議之外,我想補充一點,在 for 循環中使用ifstream.eof()
很容易出錯。
有用的閱讀材料: 為什么“while (!feof (file))”總是錯誤的? .
由於完全相同的原因,在for
循環的條件中使用!masterfile.eof()
是錯誤的。
最好使用:
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);
}
}
您也可以使用類似的邏輯來讀取交易記錄。
另外,你有一行:
main[i].balance - tran[i].amountpaid;
我確定這是一個錯字,您打算使用:
main[i].balance -= tran[i].amountpaid;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.