GCD 程序中出现奇怪的 APPCRASH 错误?

Odd APPCRASH error in GCD program?

using namespace std;
#include <iostream>
int GCD(int a, int b){
    while(b!=0){
        int temp=b;
        b=a%b;
        a=temp;
    }
    return a;
}
int main(){
    int a=0, b=0;
    cout<<"Please enter two integers to find their GCD using the Euclidean algorithm.";
    cin>>a>>b;
    cout<<"The greatest common divisor of "<<a<<" and "<<b<<" is "<<GCD(a,b)<<".";
}

这是一个简单的 C++ 程序,可以找到两个数的最大公约数,运行很好。但是,如果我改变

int GCD(int a, int b){
    while(b!=0){

进入 while(a!=0){,我遇到一个 APPCRASH 错误,异常代码 c0000094 杀死了我的 运行。我是 运行ning C++11 ISO 标准,我的 IDE 是 Code::Blocks 16。

你只是有一个 "division by 0" 错误。

示范:

#include <iostream>

using namespace std;

int GCD(int a, int b) {
  while (a != 0) {
    int temp = b;

    if (b == 0)
    {
      cout << "Divison by 0\n";
      return 0;
    }

    b = a % b;
    a = temp;
  }
  return a;
}

int main() {
  int a = 0, b = 0;
  cout << "Please enter two integers to find their GCD using the Euclidean algorithm.";
  cin >> a >> b;
  cout << "The greatest common divisor of " << a << " and " << b << " is " << GCD(a, b) << ".";
}

输入:

5
5

免责声明:本程序仅演示有"division by 0"错误,仍不正确