-2

Hi everyone I am typing up an assignment my Professor gave and it is basically just making a bill for a Hotel type service. I have the linked list done and all my calculations and outputs are as needed. But i am getting an infinite loop when the user input is yes or Yes for continuing.

#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;
}
hnefatl
  • 5,860
  • 2
  • 27
  • 49
John Shin
  • 45
  • 8
  • What is the infinite part? Is it going back in the loop and printing out cout << "Number of" .. etc.? – Omid CompSCI Jul 23 '17 at 19:39
  • You may want to create a class linked_list that will manage itself the memory, or use the `std::list` stl container – HatsuPointerKun Jul 23 '17 at 19:39
  • 1
    One of the most useful of all programming tools is the debugger. With the debugger you could put a breakpoint on the `cin >> ch;` line and then step the program line by line to see what the program did. Helpful aside: Always always ALWAYS check input for a successful read. – user4581301 Jul 23 '17 at 19:49
  • Thank You, I will always keep that in mind, i never used the debugger before. I always check my code line by line and every character I type in. I'm sure that will come in use. – John Shin Jul 23 '17 at 19:51
  • Related but not a duplicate: [Infinite loop with cin when typing string while a number is expected](https://stackoverflow.com/questions/5864540/infinite-loop-with-cin-when-typing-string-while-a-number-is-expected) – user4581301 Jul 23 '17 at 19:51

1 Answers1

0

Replace while(ch =='y' || ch == 'Y'); with while(ch =="y" || ch == "Y" || ch=="yes" || ch=="Yes");

I think that's what you mean. You weren't very clear, but if you want "yes" or "Yes" to work, you have to check the input for "yes" and "Yes"

EDIT: Also, change "ch" to a string :)

travisjayday
  • 784
  • 6
  • 16