Idiot proofing a GCD and LCM finder

Hi, I'm new to C++ and I have this program that works, it just needs to be idiot proof. If you could help that would be greatly appreciated!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  # include <stdio.h>
# include <conio.h>


int main()
{
int n1, n2, prod, gcd, lcm,m,i ;

printf("Enter the two numbers : ") ;
scanf("%d %d", &n1, &n2) ;
prod = n1 * n2 ;
if(n1>n2 )
         m=n2;
    else
         m=n1;

    for(i=m;i>=1;i--){
         if(n1%i==0 && n2%i==0){
             gcd = i ;
             break;
         }
    }

lcm = prod / gcd ;
printf("\nThe GCD is : %d", gcd) ;
printf("\n\nThe LCM is : %d", lcm);
getch() ;
}
what constitutes an idiot when using your program?
Use C++'s iostream members cin and cout instead of C functions. Bad input will put cin into a fail state, which can be checked, cleared, and the user can be prompted again. Consider isolating this into a separate function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <limits>
#include <string>

//Gets an integer from the user that is greater than or equal to zero.
//Caller provides a prompt. Reprompt until a satisfatory input is given.
int getPositiveIntFromUser(const std::string& prompt)
{
    int retVal = -1;
    while ((std::cout << prompt) && 
           (!(std::cin >> retVal) || retVal < 0))
    {
        std::cout << "That's not a positive integer.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    return retVal;
}

//function to calculate gcd...

//function to calculate lcm...

int main()
{
    int firstNum = getPositiveIntFromUser("Enter the first number: ");
    int secondNum = getPositiveIntFromUser("Enter the second number: ");

    //do gcd, lcm stuff with firstNum and secondNum...

    return 0;
}
Last edited on
Thanks! I'm still a little confused. The program has to have two positive integers. So no variables or negatives can be used. This is what I have so far, but it keeps crashing after it checks the program.




#include <iostream>
#include <limits>
#include <string>
#include <math.h>
//Gets an integer from the user that is greater than or equal to zero.
//Caller provides a prompt. Reprompt until a satisfatory input is given.
int getPositiveIntFromUser(const std::string& prompt)
{
int retVal = -1;
while ((std::cout << prompt) &&
(!(std::cin >> retVal) || retVal < 0))
{
std::cout << "That's not a positive integer.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


{
int firstNum = getPositiveIntFromUser("Enter the first number: ");
int secondNum = getPositiveIntFromUser("Enter the second number: ");
}



//function to calculate gcd...

//function to calculate lcm...

{
int firstNum, secondNum, prod, gcd, lcm,m,i ;
printf("Enter the two numbers : ") ;
scanf("%d %d", &firstNum, &secondNum) ;
prod = firstNum * secondNum ;
if(firstNum>secondNum )
m=secondNum;
else
m=firstNum;

for(i=m;i>=1;i--){
if(firstNum%i==0 && secondNum%i==0){
gcd = i ;
break;
}
}


{

lcm = prod / gcd ;
printf("\nThe GCD is : %d", gcd) ;
printf("\n\nThe LCM is : %d", lcm);
printf("\n\nPress <Enter> to continue.");

} (I think this bracket is the problem)

return 0;
Um... where did int main() go?
So this is sort of my final of it. Thanks for all of the help guys. I still can't compile it yet though, it says I have an int declarator before int. The problem is located at (int getPositiveIntFromUser(const std::string& prompt) Sorry I am such a rookie. Lol.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <limits>
#include <string>
#include <math.h>

using namespace std;

int main ()

//Gets an integer from the user that is greater than or equal to zero.
//Caller provides a prompt. Reprompt until a satisfatory input is given.


int getPositiveIntFromUser(const std::string& prompt)
{
    int retVal = -1;
    while ((std::cout << prompt) && 
           (!(std::cin >> retVal) || retVal < 0))
    {
        std::cout << "That's not a positive integer.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
   
   
{
    int firstNum = getPositiveIntFromUser("Enter the first number: ");
    int secondNum = getPositiveIntFromUser("Enter the second number: ");
}



//function to calculate gcd...

//function to calculate lcm...

{
int firstNum, secondNum, prod, gcd, lcm,m,i ;
printf("Enter the two numbers : ") ;
scanf("%d %d", &firstNum, &secondNum) ;
prod = firstNum * secondNum ;
if(firstNum>secondNum )
         m=secondNum;
    else
         m=firstNum;

    for(i=m;i>=1;i--){
         if(firstNum%i==0 && secondNum%i==0){
             gcd = i ;
             break;
         }
    


{

lcm = prod / gcd ;
printf("\nThe GCD is : %d", gcd) ;
printf("\n\nThe LCM is : %d", lcm);
printf("\n\nPress <Enter> to continue.");

}

return 0;

}
Take out lines 10 thru 36. You're not using any of it. You're not doing any idiot-proofing, and you're pretty much back where you were when you started this thread. You don't yet understand the basics of using functions, which is a prerequisite for understanding the code I posted here a week ago.
Last edited on
I'm sorry. I'm in an independent study and I'm just not understanding this stuff. Sorry for wasting your time.
Go back and read about functions here:
http://www.cplusplus.com/doc/tutorial/functions/
There you will see that you cannot declare functions inside of other functions. When you put int main() at the very top, the compiler thinks you're trying to declare
int getPositiveIntFromUser(const std::string& prompt) inside of main, so it complains.
Here's a finished example that I'm putting here hoping it will help you understand. Notice that inside main we just kinda coordinate things, we really leave all the heavy lifting (getting user input, doing calculations) to the other functions that have been implemented. Again, I'm hoping you won't just lift this code but compare it to the code you already have to see how using functions can separate unrelated bits of logic into manageable chunks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <limits>    //for std::numeric_limits
#include <algorithm> //for std::swap pre-C++11

//Function to get an integer from the user that is greater than or equal to zero.
//Caller provides a prompt.This reprompts until a satisfatory input is given.
int getPositiveIntFromUser(const std::string& prompt)
{
    int retVal = -1;
    while ((std::cout << prompt) && 
           (!(std::cin >> retVal) || retVal < 0))
    {
        std::cout << "That's not a positive integer.\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    return retVal;
}

//function to calculate gcd
unsigned getGcd(unsigned u, unsigned v)
{
    if(u < v) std::swap(u, v);
    while ( v != 0) {
        unsigned r = u % v;
        u = v;
        v = r;
    }
    return u;
}

//function to calculate lcm
unsigned getLcm(unsigned m, unsigned n)
{
    return (m*n)/getGcd(m,n);
}

int main()
{
    int firstNum = getPositiveIntFromUser("Enter the first number: ");
    int secondNum = getPositiveIntFromUser("Enter the second number: ");

    std::cout << "The GCD of " << firstNum << " and " << secondNum << " is: " << getGcd(firstNum, secondNum) << '\n';
    std::cout << "The LCM of " << firstNum << " and " << secondNum << " is: " << getLcm(firstNum, secondNum) << '\n';

    return 0;
}
Last edited on
Thank you very much! And I'll definitely look into those tutorial sites!
Topic archived. No new replies allowed.