2017-07-23 2 views
-2

Hallo allerseits Ich tippe eine Aufgabe, die mein Professor gab und es ist im Grunde nur eine Rechnung für einen Hotel-Typ Service. Ich habe die verknüpfte Liste fertig und alle meine Berechnungen und Ausgaben sind wie benötigt. Aber ich bekomme eine Endlosschleife, wenn die Benutzereingabe Ja oder Ja ist, um fortzufahren.Stuck in einer unendlichen Do-while-Schleife in Linked List

#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

Was ist der unendliche Teil? Geht es zurück in die Schleife und druckt cout << "Number of" usw. aus? –

+0

Sie können eine Klasse linked_list erstellen, die den Speicher verwaltet, oder verwenden Sie den STL-Container 'std :: list' – HatsuPointerKun

+1

Einer der nützlichsten aller Programmiertools ist der Debugger. Mit dem Debugger können Sie einen Breakpoint auf die 'cin >> ch;' Zeile setzen und dann das Programm Zeile für Zeile durchlaufen, um zu sehen, was das Programm gemacht hat. Hilfreiche Hinweise: IMMER immer IMMER die Eingabe für einen erfolgreichen Lesevorgang überprüfen. – user4581301

Antwort

0

ersetzen while(ch =='y' || ch == 'Y'); mit while(ch =="y" || ch == "Y" || ch=="yes" || ch=="Yes");

denke ich, das ist, was du meinst. Sie waren nicht sehr klar, aber wenn Sie "ja" oder "ja" wollen, müssen Sie die Eingabe für "ja" und "ja" überprüfen

EDIT: Ändern Sie auch "ch" in eine Zeichenfolge :)

+0

Danke, Ihre Antwort hat mir geholfen, die Lösung zu finden. Und es tut mir leid, dass es nicht ganz klar ist. –

+0

Freut mich zu hören! Wenn meine Antwort hilfreich ist, dann erwäge bitte, sie zu verbessern und sie als die richtige Antwort zu markieren. Gern geschehen – travisjayday

+0

Doppelzitat wird nicht funktionieren, da das nur für Strings ist. –