Program comments and explanation..-.-

Hello all! So.. My teacher gave the class a program about GCD and LCM and he wants us to write a comment on EVERY DAMN LINE(explanation what the line does). So I saw how the program works, but due to the teacher never explaining anything as he is supposed, I understand mostly NOTHING. So if anyone here can explain the lines to me or give me some useful info - I'll be extremely grateful. It's C++ 2008


#include "stdafx.h"
#include "iostream"
using namespace std;
int GCD (int a,int b )
{
while (a!=b)
{
if(a > b)
a-=b;
else
b-=a;
}
return a;
}
int LCM (int a, int b)
{
return a * b / GCD (a, b);
}
int main ()
{
int a , b;
L1: cout<<" Enter 2 positive numbers: "<<endl;
cin>>a; cin>>b;
if (a<=0 || b<=0) goto L1;
cout<<" GCD "<<GCD (a,b)<<" and LCM "<<LCM (a,b)<<endl;
system ("pause");
return 0;
}


As I said every help will be greatly appreciated! Thanks!
By "C++ 2008" I am sure you mean that your IDE is Microsoft Visual C++ 2008, which means that it is C++03.

What lines are you confused about?
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 "stdafx.h"
#include "iostream"
using namespace std;
int GCD (int a,int b )
{
    while (a!=b)
    {
        if(a > b)
            a-=b;
        else
            b-=a;
    }
    return a;
}
int LCM (int a, int b)
{
    return a * b / GCD (a, b);
}
int main ()
{
    int a , b;
    L1: cout<<" Enter 2 positive numbers: "<<endl;
    cin>>a; cin>>b;
    if (a<=0 || b<=0) goto L1;
    cout<<" GCD "<<GCD (a,b)<<" and LCM "<<LCM (a,b)<<endl;
    system ("pause");
    return 0;
}
Line 1: stdafx.h is an auto-generated header file that VC++ makes for your project if you accidentally forget to choose to make a Blank project instead of the default. It has to be the very first statement because of how precompiled headers work, and you would have to change some project settings if you wanted to remove it from your project. It is not required in normal C++, it's just a Microsoft thing.

As for the second line, I can only guess your teacher typed this in a hurry and goofed up. Normally it should be with <> instead of "", but I think it might work anyway in some compilers.

As for the third line, I'm going to assume your teacher was VERY pressed for time and didn't want to bother writing a proper C++ program.

Fourth line, again they must have been rushed to have messed up the spacing that bad. Same for 7th line.

8-11 jeez they must have tried to make this program in under a minute or something.

15, 17, 19, & 21 more hurried spacing

22 You've got to be kidding me.

23 more hurrying

24 OH GOD THE HORROR

25 more hurried spacing

26 OH THE HORROR

27 not really required, but nice to do nonetheless.
Last edited on
LB wrote:
As for the second line, I can only guess your teacher typed this in a hurry and goofed up. Normally it should be with <> instead of "", but I think it might work anyway in some compilers.

I'm pretty sure it's a mistake.

If the assumption that it's the VS2008 IDE with the standard compiler, any includes surround by quotations are looked for in the project directory. Any with the chevrons are in the predefined include paths.
In VC++2008, "" looks in project directory first and then tries the predefined include paths. <> looks in predefined include paths only. So the code would work in VC++2008
LB wrote:
In VC++2008, "" looks in project directory first and then tries the predefined include paths.

Oh, really? I never knew that. I'd always assumed that it would look at the project directory and that would be the end of it.

Then again, I've never written #include "iostream" .
Just checked, apparently it's a C++ thing, not VC++ thing:
http://ideone.com/CcFvHb
Still though, <> should be used.
LB wrote:
Still though, <> should be used.

Agreed.

Hello, unconvention!

:-)
Topic archived. No new replies allowed.