大家好我打字了一份教授給我的任務,它基本上只是爲酒店類型的服務開賬單。我已經完成了鏈表,我所有的計算和輸出都是按需要的。但是,當用戶輸入爲yes或yes時,我會收到一個無限循環。陷入鏈接列表中的無限制Do-while循環
#include <iostream>
using namespace std;
int main()
{
struct nodeType
{
double adults, children, costA, costC, dessert, room, tt, deposit;
nodeType *next, *link;
};
{
nodeType *first,*newNode,*last;
int num;
cout << "Casewell Catering and Convention Service Final Bill\n" << endl;
//cout << "Enter the number of this set: ";
//cin >> num;
// bool choice = true;
char ch;
first = NULL;
do
{
newNode = new nodeType;
newNode->link = NULL;
cout << "Number of Adults: ";
cin >> newNode -> adults;
cout<< "Number of Children: ";
cin >> newNode -> children;
cout<< "Cost Per Adult without dessert: $";
cin >> newNode -> costA;
cout<< "Cost Per Child without dessert: $" << (newNode -> children) *(newNode -> costA) * 0.60 << " (60% of the Adult's meal)" << endl;
cout<< "Cost per dessert: $";
cin >> newNode -> dessert;
cout<< "Room fee: $";
cin >> newNode -> room;
cout<< "Tips and tax rate: $";
cin >> newNode -> tt;
cout << "" << "\n" << endl;
//*********CALCULATIONS********************
cout << "Total cost for adult meals: $" << (newNode -> adults) * (newNode -> costA) << endl;
cout << "Total cost for child meals: $" << (newNode -> children) *(newNode -> costA) * 0.60 << endl;
cout << "Total cost for dessert: $" << ((newNode -> adults) + (newNode -> children)) * (newNode -> dessert) << endl;
cout << "Total food cost: $" <<(newNode -> adults) * (newNode -> costA) +
(newNode -> children) *(newNode -> costA) * 0.60 +
((newNode -> adults) + (newNode -> children)) * (newNode -> dessert)<< endl;
cout << "Plus tips and tax: $" << newNode -> tt * ((newNode -> adults) * (newNode -> costA) +
(newNode -> children) *(newNode -> costA) * 0.60 +
((newNode -> adults) + (newNode -> children)) * (newNode -> dessert)) << " (Does NOT Include Room Fee)" << endl;
cout << "Plus room fee: $" << (newNode -> room) << endl;
// cout << "Less Deposit: $" <<
if(first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
cout << "Would you like to continue?(yes/no)" << endl;
cin >> ch;
} while (ch =='y' || ch == 'Y'); // end of while loop
}
return 0;
}
什麼是無限的部分?它是否回到循環中並打印出cout <<「Number of」..等等? –
你可能想創建一個類linked_list來管理自己的內存,或者使用'std :: list' stl容器 – HatsuPointerKun
所有編程工具中最有用的一個就是調試器。使用調試器,你可以在'cin >> ch;'行上放置一個斷點,然後逐行執行程序以查看程序執行的操作。一邊有用:始終總是檢查輸入是否成功讀取。 – user4581301