2017-07-12 29 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