C++中的简单代码正在编译,但不能在Linux中执行
Simple code in C++ compiling but not executing in Linux
我正试图在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
。
相关文章:
- 是否可以将函数导入命名空间,但不能导出它?
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 声明C++具有动态大小的数组类型在 Linux 中工作正常,但不能在 Windows 中工作
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- std::chrono::d uration 可以按秒初始化,但不能按毫秒初始化?
- Python/C++:可以导入犰狳(arma::)但不能导入子程序arma::arma_rng::randn
- 为什么我可以从C++文件中读取 int 值,但不能浮点?
- C++程序在将 int 与 cin 一起使用时有效,但不能使用字符串
- SqLite c++,可以创建数据库,但不能将 anthing 插入到表中
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 16 位系统中的程序如何访问大于 65535 的整数,但不能访问地址
- 我可以在Windows(Visual C++)中读取bin文件,但不能在linux(GCC)上读取bin文件
- C++程序使用 mingw 在 Linux 上交叉编译在 MSy2 中工作,但不能直接在 Windows 中工作
- 没有返回类型的静态函数可以通过Windows上的编译,但不能传递Linux上的编译
- C++程序在Linux上完美运行,但不能在Windows上运行
- 有本地主机名,可以在windows上工作,但不能在Linux上工作
- random_shuffle() 适用于 Windows,但不能在 Linux 上编译
- VRPN C++代码在Linux上编译,但不能在Windows上编译
- C++中的简单代码正在编译,但不能在Linux中执行
- c++代码可以在mac上编译,但不能在linux上编译