簡體   English   中英

C ++語法錯誤:程序以非零狀態退出

[英]C++ Syntax Error: Program Exited With Non-Zero Status

我正在使用repl.it編寫我的C ++。 到目前為止,我已經了解了條件,循環和函數。 現在,我正在嘗試編寫一個輸入兩個整數並找到最小公倍數和最大公分母的程序。 到目前為止,我已經編寫了大部分代碼,但是存在問題。

“以非零狀態退出”

#include <iostream>

using namespace std;

int main() 
{
int number1 = 0;
int number2 = 0;
int calc = 0;
int lcm = 0;
cout << "Give me two integers, and I will calculate the Least Common Multiple and the Greatest Common Divisor." << endl;

while (number1 <= 0) {

    cout << "Enter your first number. Cant be negative" << endl;
    cin >> number1; }

while (number2 <= 0) {
    cout << "Enter your second number. Cant be negative" << endl;
    cin >> number2; }

while(number2 != 0) { ///Greatest Common Divisor
    calc = number1 % number2;
    lcm = (number1*number2) / calc;
    number1 = number2;
    number2 = calc;
}



cout << "Least Common Multiple is " << lcm << endl;

cout << "Greatest Common divisor is " << number1 << endl;
}

所以我不確定這是語法錯誤還是由於repl.it而引起的,但是我真的很難弄清楚這一點。

謝謝

雖然循環在循環結束時檢查number2值,但是當您計算calc時,有時該值為零,然后在下一步中程序被零除異常退出。 您可以通過在計算calc變量后將此行添加到代碼中來防止出現此問題:

if (calc == 0 ) break;

另外,您的代碼無法正常工作,例如,設置number1 = 30和number2 = 18!

我使用二進制方法計算GCD,然后使用GCD計算LCM。

            #include <iostream>
            #include <math.h>   //  for pow(2,d)
            using namespace std;


            int main() 
            {
                int gcd, lcm, a, b, g, number1 = 0, number2 = 0, d=0;
                cout << "Give me two integers, and I will calculate the Greatest Common Divisor and the Least Common Multiple." << endl;

                while (number1 <= 0) {
                    cout << "Enter your first number. Cant be negative" << endl;
                    cin >> number1; 
                }
                // using binary method to calculating GCD:   https://en.wikipedia.org/wiki/Greatest_common_divisor
                while (number2 <= 0) {
                    cout << "Enter your second number. Cant be negative" << endl;
                    cin >> number2; 
                }    
                a = number1;
                b = number2;
                while (((a%2)==0) && ((b%2)==0)) {
                    a = a/2;
                    b = b/2;
                    d = d+1;
                }
                while (a != b) {
                    if ((a%2) == 0) {
                        a = a/2;
                    } else if ((b%2)==0) {
                        b = b/2;
                    } else if (a>b) {
                        a = (a-b) /2;
                    } else {
                        b = (b-a)/2;
                    }
                }
                g = a;
                cout << "\ng: " << g << "\td: " << d << "\tpower(2,d): " << pow(2,d);
                gcd = g * pow(2,d); // power(2,d) with math.h library
                lcm = (number1*number2)/gcd;   // according to LCM(a,b) = (a*b)/GCD(a,b)
                cout << "\nGreatest Common Divisor is " << gcd << " and Least Common Multiple is " << lcm << endl;
            }

首先,您需要檢查哪個數字更大,因為如果您設置number1=18number2=30那么number1%number2將為18,然后您會看到錯誤的輸出。 此外,Hossein Nazari建議的修復程序可以避免出現異常,但是,如果遵循該修復程序,則GCD將存儲在number2 例如30和18:

30%18 = 12
calc = 12
if(calc==0) break;
(lcm routine)
number1 = 18
number2 = 12

18%12 = 6
calc=6
if(calc==0) break;
(lcm routine)
number1 = 12
number2 = 6

12%6=0
calc=0
if(calc==0) break;
<done>

我寧願從您的循環中完全刪除lcm計算,在這種情況下,您將按計划將GCD放入number1 ,然后僅通過公式(number1*number2)/GCD計算LCM,即30*18/6 = 90; (number1*number2)/GCD 30*18/6 = 90;

您好,只需更改這兩個的數據類型

float calc = 0;
float lcm = 0;

@Cheers和hth,您好,我在這里執行了代碼https://www.tutorialspoint.com/compile_cpp11_online.php ,並得到了這個異常-浮點異常,在我將數據類型從int更改為float之后,我得到了這個值輸入您的第一個數字。 不能為負
10
輸入您的第二個號碼。 不能為負
20
最小公倍數為inf
最大公約數是10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM