陷入while循环C ++

时间:2013-11-08 18:02:33

标签: c++ while-loop

我是编程的新手,我想知道是否有人可以帮我这个?它似乎是一个连续的循环,我一直在改变它,尝试不同的方法将近一个小时,帮助将非常感激。

    #include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <iomanip>
#include "float.h"
#include <stack>

using namespace std;

using std::stack;

int _tmain(int argc, _TCHAR* argv[])
{

    {
        char treatment_types, anymore_treatments, anymore_visits, treatment_type[15], answer/*, ftype, wtype, ctype, etype, rtype, itype*/;
        double levy = 15, fcharge, wcharge, ccharge, echarge, rcharge, icharge, cost, totalcost, totalincome;
        int quantity, noofpatients, total_qtyf, total_qtyw, total_qtyc, total_qtye, total_qtyr, total_qtyi;


        cout << "\n Enter treatment type (F,W,C,E,R,I) : ";
        cin >> treatment_types;
        cout << "\n Enter quantity of treatments : ";
        cin >> quantity;




        switch (treatment_types)
        {
        case 'F': fcharge = 27.50;
            cost = fcharge;
            total_qtyf = quantity;
            cost = quantity*cost;
            totalcost = fcharge + cost;
            /*totalcost = fcharge * total_qtyf;*/
            break;

        case 'W': wcharge = 40.00;
            cost = wcharge;
            total_qtyw = quantity;
            cost = quantity*cost;
            totalcost = wcharge + cost;
            /*      totalcost = quantity + total_qtyw;*/
            break;

        case 'C': ccharge = 210.00;
            cost = ccharge;
            total_qtyc = quantity;
            cost = quantity*cost;
            totalcost = ccharge + cost;
            /*totalcost = quantity + total_qtyc;*/
            break;

        case 'E': echarge = 25.00;
            cost = echarge;
            total_qtye = quantity;
            cost = quantity*cost;
            totalcost = echarge + cost;
            /*  totalcost = quantity + total_qtye; */
            break;

        case 'R': rcharge = 145.00;
            cost = rcharge;
            total_qtyr = quantity;
            cost = quantity*cost;
            totalcost = rcharge + cost;
            /*  totalcost = quantity + total_qtyr;*/
            break;

        case 'I': icharge = 7.50;
            cost = icharge;
            total_qtyi = quantity;
            cost = quantity*icharge;
            totalcost = cost + icharge;
            /*totalcost = quantity + total_qtyi; */
            break;
        }

        cout << "\n " << cost + 15;
    /*  do
        {*/

            cout << "\n\n""Any more treatments [Y/N] :";
            cin >> answer;

            while (answer == 'Y')
            {
                cout << "\nEnter treatment type( F,W,C,E,R,I) : ";
                cin >> treatment_type;
                cout << "\nEnter Quantity : ";
                cin >> quantity;
                cout << "\n " << cost * quantity;
                cout << "\n\n""Any more treatments [Y/N] :";
                cin >> answer;
            }
            while (answer == 'N')
            {
                cout << "Anymore visits? [Y/N]: ";
                cin >> answer;
            }
            while (answer == 'N')
            {
                cout << "Your bill is : " << totalcost << endl;
            }
            {   while (answer == 'Y')
                cout << "\nEnter treatment type( F,W,C,E,R,I) : ";
                cin >> treatment_type;

}

    }

        //while (true);
        _getche();
        return 0;

    }
//}

2 个答案:

答案 0 :(得分:4)

        while (answer == 'N')
        {
            cout << "Your bill is : " << totalcost << endl;
        }

如果输入此循环,它怎么能退出?

也许您打算让while成为if

此外:

        {   while (answer == 'Y')
            cout << "\nEnter treatment type( F,W,C,E,R,I) : ";
            cin >> treatment_type;

        }

您可能希望将while移到块外:

        while (answer == 'Y') {
            cout << "\nEnter treatment type( F,W,C,E,R,I) : ";
            cin >> treatment_type;
        }

答案 1 :(得分:1)

这在您的代码中永远不会发生,但如果确实如此,则会出现问题。

while (answer == 'N')
{
  cout << "Your bill is : " << totalcost << endl;
}

这是您的代码实际上永远循环的地方(假设您键入'Y')。您也不会在此块中更改答案。目前尚不清楚你的目标是什么,或者我会提出建议。

while (answer == 'Y')
  cout << "\nEnter treatment type( F,W,C,E,R,I) : ";