C++中的简单代码正在编译,但不能在Linux中执行

Simple code in C++ compiling but not executing in Linux

本文关键字:但不能 Linux 执行 编译 简单 代码 C++      更新时间:2023-10-16

我正试图在linux机器上用C++编译和执行一个简单的代码。但是程序会被卡在代码的中间。我找不到原因。

这是代码

#include <iostream>
using namespace std;
int n;
int product =1;
int counter =0;
int p;
int main()
{
    //return 1;
    cout << "How many numbers?" << endl;
    cin >> n ;
    cout << "Input the numbers " << endl;
    for(int i=0;i<n;i++)
        {
                cin >> p;
                product = product*p;
                int p = 1;
        }
        cout << "Now our number to be factorised is " << product << endl;
        cin >> p;
        for(int i=1;i=product;i++)
        {
         if(product%i==0)
         counter++;
        }
         cout << "the number of factors is " << counter << endl;
          return 0;
}

代码被困在"现在我们要分解的数字是"乘积。它计算产品,但不会进行任何进一步的

因为无限循环,在第二个循环中您拼错了==:

for(int i=1;i=product;i++)
             ^
               should be ==

旁注:为了最大限度地减少代码中的此类错误,我建议您在表达式中保留空间,例如表达式i=product应写成i = product,以便可读。类似地,您应该在;,之后添加空格。

看起来这一行有一个拼写错误:

for(int i=1;i=product;i++)
             ^

您使用的是赋值(=),而不是逻辑等于(==)。这实际上是一个无限循环,因为这个表达式的结果是true

相关文章: