“错误”;main.exe已停止工作“;在试图运行程序时 我是初学者C++程序员,仍然在学习基础知识。p>

“错误”;main.exe已停止工作“;在试图运行程序时 我是初学者C++程序员,仍然在学习基础知识。p>,c++,C++,我正在编写一个程序来测试输入的数字是否是一个完美的数字,但是每当我运行代码时,我都会收到一个错误,说main.exe已经停止工作 我也看过这个网站上提出的类似问题,但我不明白 有人能简单地解释一下吗 这是我的密码: #include <iostream> using namespace std; int main() { int i, number, s = 0; cin >> umber; for (i = 0; i < number

我正在编写一个程序来测试输入的数字是否是一个完美的数字,但是每当我运行代码时,我都会收到一个错误,说main.exe已经停止工作

我也看过这个网站上提出的类似问题,但我不明白

有人能简单地解释一下吗

这是我的密码:

#include <iostream>

using namespace std;

int main()
{
    int i, number, s = 0;
    cin >> umber;

    for (i = 0; i < number; i++)
        if (number % i == 0)
            s += i;

    if (number == s)
       cout << "the umber is perfect";
    else
       cout << "the number is not perfect";

    return 0;
}
#包括
使用名称空间std;
int main()
{
int i,数字,s=0;
cin>>编号;
对于(i=0;i除了在第6行输入一个未定义的变量外

cin>>umber;
您应将其更改为:

cin>>number;
如果发生浮点异常,则对从零开始的循环索引(i)执行模运算:

for (i=0;i<number;i++)
   if(number%i==0)

用于(i=0;它的模运算符
%
执行除法。现在想想当
i
为0时会发生什么,然后你尝试用0除法…@someprogramme所以你想说的是,如果用0除法永远不会发生,我就不会得到那个错误了?是的,只需将i设置为1,而不是0cin>>number;->cin>>number;运行程序我正在调试器下。它将显示问题发生的确切位置。