2017-07-12 38 views
-3

當用戶添加他要重複循環次數時,產生錯誤的一個週期錯誤,但他不斷重複的週期沒有結束我在C++

這裏是代碼

#include <iostream> 
using namespace std; 

int main() 
{ 
    char resp, copcion, varied, tprod; 
    int cfab, ncod, num; 

    cout << "How many products do you want to register ?, enter the respective number"; 
    cin >> num; 

    while (num > 0) 
    { 
     cout << "Enter the product type If you are a Child (n) or Adult (a)"; 
     cin >> tprod; 

     if ((tprod != 'n') && (tprod != 'N') && (tprod != 'a') && (tprod != 'A')) 
     { 
      cout << "ERROR: The product type is only Children (n) or Adults (a)" << endl; 
     } 

     cout << "Enter your Variety for Salted (s) and Sweets (d)"; 
     cin >> varied; 

     if ((varied != 'd') && (varied != 'D') && (varied != 's') && (varied != 'S')) 
     { 
      cout << "ERROR: The variety of products are only Sweets (d) or Salted (s)" << endl; 
     } 
     //Code range 
     //Salted 1 to 10 
     //Sweets 11 to 20 

     cout << "Enter the product code you wish to assign"; 
     cin >> ncod; 

     if (ncod > 20) 
     { 
      cout << "ERROR: There is a range of numbers, you exceeded that amount" << endl; 
     } 

     cout << "Enter the respective amount"; 
     cin >> cfab; 

     if (cfab = 0) 
     { 
      cout << "ERROR: Amount entered is not valid"; 
     } 
    } 
    return 0; 
} 

提前感謝您解決疑惑。

+7

'cfab = 0' - >'cfab == 0'? – LogicStuff

+1

用於此目的的最佳工具是您的調試器。 – Charles

+1

@Charles Eclipse已經將它標記爲一個潛在的問題,當我將代碼複製/粘貼到自動格式化代碼時。 – mch

回答

0

該代碼讀取一個數字到num變量並做一個while(num>0) - 但它永遠不會更改num因此它總是大於0並永遠循環。

也許更改while(num>0) {while(num-- > 0) {每次遞減num,循環將運行num次而不是無限次。

+0

非常感謝您的信息 – Christopher