0

我是编程新手,我想知道是否有人可以帮助我解决这个问题?它似乎处于一个连续的循环中,我一直在更改它并尝试不同的方法近一个小时,非常感谢您的帮助。

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

    }
//}
4

2 回答 2

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;
        }
于 2013-11-08T18:05:32.420 回答
1

这永远不会在您的代码中发生,但如果发生了,那就是问题所在。

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

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

while (answer == 'Y')
  cout << "\nEnter treatment type( F,W,C,E,R,I) : ";
于 2013-11-08T18:08:23.323 回答