简体   繁体   中英

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

I'm using repl.it to write my 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.

#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.

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. you can prevent this problem by adding this line to your code after calculating calc variable:

if (calc == 0 ) break;

In additional your code does not work correctly, for example set number1 = 30 and number2 = 18!

I use binary method for calculating GCD and then calculate LCM by using GCD.

            #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. 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. For example 30 and 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;

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. Cant be negative
10
Enter your second number. Cant be negative
20
Least Common Multiple is inf
Greatest Common divisor is 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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