陷入链接列表中的无限Do-while循环

时间:2017-07-23 19:35:20

标签: c++ loops linked-list 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;
}

1 个答案:

答案 0 :(得分:0)

while(ch =='y' || ch == 'Y');替换为while(ch =="y" || ch == "Y" || ch=="yes" || ch=="Yes");

我认为这就是你的意思。你不是很清楚,但如果你想要&#34;是&#34;或&#34;是&#34;要工作,你必须检查输入&#34;是&#34;和&#34;是&#34;

编辑:此外,改变&#34; ch&#34;到一个字符串:)