2013-11-08 2 views
0

Я новичок в программировании, и мне было интересно, может ли кто-нибудь помочь мне с этим? Кажется, он находится в непрерывном цикле, и я меняю его и пробую разные методы в течение почти часа.Застрял во время цикла C++

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

Да, это должно быть, если, я только что изменил его и установил if вне блока и, похоже, работает. У меня еще много дел, чтобы он функционировал так, как хотелось бы, но ваша помощь очень ценится. благодаря – user2970001

1

Это никогда не происходит в вашем коде, но если он сделал это было бы проблемой.

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

Здесь ваш код на самом деле петли навсегда (при условии, что вы вводите «Y»). Вы также не изменяете ответ в этом блоке. Непонятно, к какому поведению вы стремитесь, или я бы сделал предложение.

while (answer == 'Y') 
    cout << "\nEnter treatment type(F,W,C,E,R,I) : "; 
Смежные вопросы