2017-07-26 9 views
1
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)<<"."; 
} 

これは、2つの数値の最大公約数を見つけ出し、うまく動作する単純なC++プログラムです。私はwhile(a!=0){GCDプログラムのOdd APPCRASHエラー?

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

を変更する場合は、私は私の実行を殺すAPPCRASHエラー、例外コードc0000094を体験します。私はC++ 11のISO標準を実行しています。私のIDEはCode :: Blocks 16です。

+0

* APPCRASHエラーが発生しました*入力は?b = 0の場合もありますか? –

+0

@MichaelWalz 'b = 0'ならば? –

+0

@ GauravSehgal私は100,150,150,150,150,100を使用しました。これはエントリーレベルのC++コースの単純な割り当てでしたので、例外を無視しました。 :P) –

答えて

0

「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 

免責事項:このプログラムは唯一のエラー "0による除算" があることを示して、それはまだ正しくありません。