2017-07-23 52 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

尽管双引号不起作用,因为这只适用于字符串。 –