简体   繁体   English

C ++语法错误:程序以非零状态退出

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

I'm using repl.it to write my C++. 我正在使用repl.it编写我的C ++。 So far I have learned about conditionals, loops, and functions. 到目前为止,我已经了解了条件,循环和函数。 Right now I am trying to write a program that inputs two integers and finds the Least Common Multiple and the Greatest Common Denominator. 现在,我正在尝试编写一个输入两个整数并找到最小公倍数和最大公分母的程序。 So far I have written most of my code, however there is a problem. 到目前为止,我已经编写了大部分代码,但是存在问题。

"exited with non-zero status" “以非零状态退出”

#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;
}

So I'm not sure if it's a syntax error or it's because of repl.it, but I'm really struggling to figure this out. 所以我不确定这是语法错误还是由于repl.it而引起的,但是我真的很难弄清楚这一点。

Thanks 谢谢

While loop checks number2 value in end of the loop, but when you calculate calc, sometime this value is zero, then in next step program exited with divide by zero exception. 虽然循环在循环结束时检查number2值,但是当您计算calc时,有时该值为零,然后在下一步中程序被零除异常退出。 you can prevent this problem by adding this line to your code after calculating calc variable: 您可以通过在计算calc变量后将此行添加到代码中来防止出现此问题:

if (calc == 0 ) break;

In additional your code does not work correctly, for example set number1 = 30 and number2 = 18! 另外,您的代码无法正常工作,例如,设置number1 = 30和number2 = 18!

I use binary method for calculating GCD and then calculate LCM by using GCD. 我使用二进制方法计算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;
            }

First of all, you need to check which number is bigger, because if you set number1=18 and number2=30 then number1%number2 will be 18, and then you see where it goes - to the wrong output. 首先,您需要检查哪个数字更大,因为如果您设置number1=18number2=30那么number1%number2将为18,然后您会看到错误的输出。 Also, the fix suggested by Hossein Nazari is good to avoid the exception, but you GCD is stored in number2 if you follow that fix. 此外,Hossein Nazari建议的修复程序可以避免出现异常,但是,如果遵循该修复程序,则GCD将存储在number2 For example 30 and 18: 例如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>

I would rather remove the lcm computation from your loop entirely, in this case, you'd get your GCD in number1 as planned, then just calculate LCM by formula (number1*number2)/GCD which would be 30*18/6 = 90; 我宁愿从您的循环中完全删除lcm计算,在这种情况下,您将按计划将GCD放入number1 ,然后仅通过公式(number1*number2)/GCD计算LCM,即30*18/6 = 90; (number1*number2)/GCD 30*18/6 = 90;

Hi just change data type of this two 您好,只需更改这两个的数据类型

float calc = 0;
float lcm = 0;

Hi @Cheers and hth, I have executed code here https://www.tutorialspoint.com/compile_cpp11_online.php , and got this exception - Floating point exception, After i have changed data type from int to float i got this values Enter your first number. @Cheers和hth,您好,我在这里执行了代码https://www.tutorialspoint.com/compile_cpp11_online.php ,并得到了这个异常-浮点异常,在我将数据类型从int更改为float之后,我得到了这个值输入您的第一个数字。 Cant be negative 不能为负
10 10
Enter your second number. 输入您的第二个号码。 Cant be negative 不能为负
20 20
Least Common Multiple is inf 最小公倍数为inf
Greatest Common divisor is 10 最大公约数是10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM