Hi there, I'm fairly new to c++, I've only been doing it for about two months, or so.
Today I was working on writing a program that would calculate the Lowest Common Multiple of two inputted numbers,
but for some reason I get an error message after entering the two numbers.
Here is the code:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int main() {
cout << "This program will calculate the LCM of two numbers." << endl;
//Set variables and arrays.
int n, m, i, j;
int mult[n];
int multo[m];
//Take input.
cout << "Input the first number: ";
cin >> n;
cout << "Input the second number: ";
cin >> m;
//Set values for each element of each array
//By calculating the multiples of each number.
for (i = 0; i <= m; i++) {
mult[i] = n * i;
}
for (i = 0; j <= n; i++) {
multo[i] = m * i;
}
//Loop to compare all elements between both arrays.
//Works by comparin all the elements of mult to the first value of
//multo. Then by comparing all the elements of mult to the
//second element of multo and continues to end of multo.
for (j = 0; j <= n + 1; j++) {
for (i = 0; i <= n + 1; i++) {
if (mult[i] == multo[j]) { //compares the elements.
cout << mult[i] << " is the LCM." << endl;
}
else { //If they are not equal, continue to compare.
continue;
}
}
}
system ("pause");
return 0;
}
note: I've tried changing the variables both for the comparison loop, and for both of the calculations loops, using different combinations etc...
It seems that I'm able to get as far as the input, but then I get the error:
LCM.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
Any idea what I wrote wrong, where, and what I can do to fix it?
Question
Ryuurei
Hi there, I'm fairly new to c++, I've only been doing it for about two months, or so.
Today I was working on writing a program that would calculate the Lowest Common Multiple of two inputted numbers,
but for some reason I get an error message after entering the two numbers.
Here is the code:
#include <iostream> #include <stdlib.h> #include <math.h> using namespace std; int main() { cout << "This program will calculate the LCM of two numbers." << endl; //Set variables and arrays. int n, m, i, j; int mult[n]; int multo[m]; //Take input. cout << "Input the first number: "; cin >> n; cout << "Input the second number: "; cin >> m; //Set values for each element of each array //By calculating the multiples of each number. for (i = 0; i <= m; i++) { mult[i] = n * i; } for (i = 0; j <= n; i++) { multo[i] = m * i; } //Loop to compare all elements between both arrays. //Works by comparin all the elements of mult to the first value of //multo. Then by comparing all the elements of mult to the //second element of multo and continues to end of multo. for (j = 0; j <= n + 1; j++) { for (i = 0; i <= n + 1; i++) { if (mult[i] == multo[j]) { //compares the elements. cout << mult[i] << " is the LCM." << endl; } else { //If they are not equal, continue to compare. continue; } } } system ("pause"); return 0; }note: I've tried changing the variables both for the comparison loop, and for both of the calculations loops, using different combinations etc...
It seems that I'm able to get as far as the input, but then I get the error:
LCM.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
Any idea what I wrote wrong, where, and what I can do to fix it?
Thanks in advance.
Edited by RyuureiLink to comment
Share on other sites
13 answers to this question
Recommended Posts