简体   繁体   中英

Adding a while loop to continue or exit the console in C++

Hey guys basically I need to put a while loop in my code for example a user can enter the number of tickets for the children and adding -1 to stop. Can you teach me how and where to put my while loop? Its my first time taking a C++ class. I would greatly appreciate your help guys. here is my code and an example of the output.

Example output

          Chesapeake Amusement Park

Enter children ticets or -1 to stop... 10
Enter adult tickets................... 11

          Chesapeake Amusement Park
        ----------------------------

        Tickets      Price       Total
Child         10         10.00        10.00
Adult         11         20.50        220.50

          21

Security Fee                        15.00

 Total Bill                    $  335.00

 Cash received.....340

 Change                                4.50

Enter children tickets or -1 to stop...

Code

#include <iostream>
#include <iomanip>
using namespace std;

const double ADULTPRICE = 20.50;
const double SECURITYFEE = 15.00;

int main ()
{
    double adultTotal, childTotal, totalBill, change, cash;
    double CHILDPRICE = 12.00;
    int childTix, adultTix, tixTotal;

    cout << "\n           Chesapeake Amusement Park" << endl << endl;

    cout << "    Enter children tickets or -1 to stop... ";
    cin >> childTix;

    if (childTix >= 8)
        CHILDPRICE = 10.00;

    cout << "    Enter adult tickets.................... ";
    cin >> adultTix;

    childTotal = CHILDPRICE * childTix;
    adultTotal = ADULTPRICE * adultTix;
    totalBill = childTotal + adultTotal;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "\n\n                 Chesapeake Amusement Park";
    cout << "\n                 -------------------------";
    cout << "\n\n               Tickets      Price      Total\n";

    cout << "     Children   " << setw(3)  << childTix
    << setw(14) << CHILDPRICE
    << setw(11) << childTotal;

    cout << "\n     Adults     " << setw(3)  << adultTix
    << setw(14) << ADULTPRICE
    << setw(11) << adultTotal;

    tixTotal = childTix + adultTix;

    cout << "\n ";
    cout << "\n        "          << setw(11) << tixTotal;
    cout << "\n ";

    if ((tixTotal >= 20) || (childTix >= 14))
        cout << "\n       Security Fee           " << setw(14) << SECURITYFEE;

    cout << "\n";
    cout << "\n         Total Bill             $" << setw(11) << totalBill;

    cout << "\n ";
    cout << "\n ";
    cout << "\n         Cash received...... ";
    cin >> cash;

    change = cash - totalBill;

    cout << "\n ";
    cout << "\n ";
    cout << "\n     Change                  " << setw(11) << change;

    return 0;

}

First try on your own , then see the solution .

while(1)
{

    cout << "    Enter adult tickets.................... ";
    cin >> adultTix;

    if(adultTix == -1)
        break;


    childTotal = CHILDPRICE * childTix;
    adultTotal = ADULTPRICE * adultTix;
    totalBill = childTotal + adultTotal;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "\n\n                 Chesapeake Amusement Park";
    cout << "\n                 -------------------------";
    cout << "\n\n               Tickets      Price      Total\n";

    cout << "     Children   " << setw(3)  << childTix  
                               << setw(14) << CHILDPRICE 
                               << setw(11) << childTotal;

    cout << "\n     Adults     " << setw(3)  << adultTix  
                                 << setw(14) << ADULTPRICE 
                                 << setw(11) << adultTotal;

    tixTotal = childTix + adultTix;

    cout << "\n ";
    cout << "\n        "          << setw(11) << tixTotal;
    cout << "\n ";

    if ((tixTotal >= 20) || (childTix >= 14))
        cout << "\n       Security Fee           " << setw(14) << SECURITYFEE;

    cout << "\n";
    cout << "\n         Total Bill             $" << setw(11) << totalBill;

    cout << "\n ";
    cout << "\n ";
    cout << "\n         Cash received...... ";
    cin >> cash;

    change = cash - totalBill;

    cout << "\n ";
    cout << "\n ";
    cout << "\n     Change                  " << setw(11) << change;

}  

Another way of writing while loop is for( ; ; ) . The main point was to use keyword break .

Your code is great as a beginner! You just need to do minor changes to your code.

The primary change is to use a correct while expression to make the loop stops when the user inputs (-1) as a value for childTix . I do not recommend that you use the break keyword to exist the loop. You should always try to stop the loop naturally by letting the while expression be evaluated to FALSE . This makes your program easier to read and trace.

Another thing you need to add is the constant variable DISCOUNTEDCHILDPRICE when the number of children tickets is greater than 7. You cannot play with the value of CHILDPRICE inside the while loop cause this will lead to inconsistent results after you run the while loop the next time if the number of child tickets is less than 8. Instead, use if statement inside the body of the loop to figure out if there is a discount or not on children' tickets.

There is also a few other changes not mentioned above.

Here is the revised version of your code. I put the whole program so you and other beginners can learn from it and avoid having such problems in your future programs.

#include <iostream>
#include <iomanip>
using namespace std;

const double ADULTPRICE = 20.50;
const double SECURITYFEE = 15.00;
const double CHILDPRICE = 12.00;
const double DISCOUNTEDCHILDPRICE = 10;

int main()
{
    double adultTotal, childTotal, totalBill, change, cash;
    int childTix, adultTix, tixTotal;

    cout << "\n           Chesapeake Amusement Park" << endl << endl;
    cout << "    Enter children tickets or -1 to stop... ";
    cin >> childTix;

    while (childTix != -1)
    {
        cout << "    Enter adult tickets.................... ";
        cin >> adultTix;

        if (childTix < 8)
            childTotal = CHILDPRICE * childTix;
        else
            childTotal = DISCOUNTEDCHILDPRICE * childTix;

        adultTotal = ADULTPRICE * adultTix;
        totalBill = childTotal + adultTotal;

        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);

        cout << "\n\n                 Chesapeake Amusement Park";
        cout << "\n                 -------------------------";
        cout << "\n\n               Tickets      Price      Total\n";

        cout << "     Children   " << setw(3) << childTix
            << setw(14) << CHILDPRICE
            << setw(11) << childTotal;

        cout << "\n     Adults     " << setw(3) << adultTix
            << setw(14) << ADULTPRICE
            << setw(11) << adultTotal;

        tixTotal = childTix + adultTix;

        cout << "\n\n        " << setw(11) << tixTotal;
        cout << "\n ";

        if ((tixTotal >= 20) || (childTix >= 14))
            cout << "\n       Security Fee           " << setw(14) << SECURITYFEE;

        cout << "\n\n         Total Bill             $" << setw(11) << totalBill;

        cout << "\n\n";
        cout << "\n         Cash received...... ";
        cin >> cash;

        change = cash - totalBill;

        cout << "\n\n";
        cout << "\n     Change                  " << setw(11) << change;

        cout << "\n           Chesapeake Amusement Park" << endl << endl;
        cout << "    Enter children tickets or -1 to stop... ";
        cin >> childTix;
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM