gpt4 book ai didi

c++ - C++ 中的简单代码在 Linux 中编译但不执行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:15:50 25 4
gpt4 key购买 nike

我正在尝试在 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 循环中你拼错了 == :

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

旁注:为了尽量减少代码中的此类错误,我建议您在表达式中保留空格,例如表达式i=product 应该写成i = product,这样才具有可读性。同样,您应该在 ;, 之后添加空格。

关于c++ - C++ 中的简单代码在 Linux 中编译但不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17927777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com