添加while循环以在C ++中继续或退出控制台

时间:2014-02-15 20:49:50

标签: c++

嘿伙计基本上我需要在我的代码中放一个while循环,例如用户可以输入子代的票数并添加-1来停止。你能教我如何以及在哪里放置我的while循环吗?这是我第一次参加C ++课程。我非常感谢你们的帮助。这是我的代码和输出的一个例子。

示例输出

          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...

代码

#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;

}

2 个答案:

答案 0 :(得分:0)

首先尝试自己,然后查看解决方案。

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;

}  

while循环的另一种写法是for( ; ; )。重点是使用关键字break

答案 1 :(得分:0)

作为初学者,您的代码非常棒!您只需对代码进行细微更改即可。

主要更改是使用正确的while表达式,以便在用户输入(-1)作为 childTix 的值时停止循环。我不建议您使用 break 关键字来存在循环。您应该始终尝试通过将while表达式计算为 FALSE 来自然地停止循环。这使您的程序更易于阅读和跟踪。

当儿童票数大于7时,您需要添加的另一件事是常量变量 DISCOUNTEDCHILDPRICE 。您无法使用 <的值while循环中的em> CHILDPRICE 导致如果子票数小于8,则在下次运行while循环后会导致结果不一致。相反,在正文中使用if语句关于孩子的门票是否有折扣的循环。

上面没有提到其他一些变化。

以下是您的代码的修订版本。我把整个程序放在一起,这样你和其他初学者就可以从中学习,避免在未来的程序中出现这样的问题。

#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;
}