2017-07-23 85 views
-2

大家好我打字了一份教授給我的任務,它基本上只是爲酒店類型的服務開賬單。我已經完成了鏈表,我所有的計算和輸出都是按需要的。但是,當用戶輸入爲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; 
} 
+0

什麼是無限的部分?它是否回到循環中並打印出cout <<「Number of」..等等? –

+0

你可能想創建一個類linked_list來管理自己的內存,或者使用'std :: list' stl容器 – HatsuPointerKun

+1

所有編程工具中最有用的一個就是調試器。使用調試器,你可以在'cin >> ch;'行上放置一個斷點,然後逐行執行程序以查看程序執行的操作。一邊有用:始終總是檢查輸入是否成功讀取。 – user4581301

回答

0

更換while(ch =='y' || ch == 'Y');while(ch =="y" || ch == "Y" || ch=="yes" || ch=="Yes");

我想這就是你的意思。你不是很清楚,但如果你想要「是」或「是」工作,你必須檢查輸入是「是」和「是」

編輯:另外,將「ch」改爲字符串:)

+0

謝謝你,你的回答幫助我找到解決方案。對不起,我很抱歉。 –

+0

很高興聽到!如果我的答案有幫助,請考慮將它提升,並將其標記爲正確答案。不客氣 – travisjayday

+0

儘管雙引號不起作用,因爲這隻適用於字符串。 –