Tech Support Guy banner
Status
Not open for further replies.
1 - 9 of 9 Posts

·
Registered
Joined
·
61 Posts
Discussion Starter · #1 ·
hi i just started in c++ and i was worndering if someone could look at my code

//my first program
#include <iostream.h>

main()
{
int num1, num2;

cout << "enter 2 integers, and i will tell you\n"
<< "the difrence between them: ";
cin >> num1 >> num2; // read 2 integers

if (num1 == num2)
cout << num1 << " is equal to " << num2 << end1;

if (num1 != num2)
cout << num1 << " is not equal to " << num2 << end1;

if (num1 < num2)
cout << num1 << " is ldess than " << num2 << end1;

if (num1 > num2)
cout << num1 << " is more than " << num2 << end1;

if (num1 <= num2)
cout << num1 << " is less than or equal to "
<< num2 << end1;

if (num1 >= num2)
cout << num1 << " is more than or equal to "
<< num2 << end1;

return 0; // indcate program ended successfuly
{
 

·
Registered
Joined
·
2,636 Posts
It should look something like this

Code:
#include <iostream>

using namespace std;

int main() {
    int num1, num2;
    cout << "enter 2 integers, and i will tell you" << "\n" 
         << "the difference between them: ";
    cin >> num1 >> num2; // read 2 integers
    if (num1 == num2)
        cout << num1 << " is equal to " << num2 << endl;
    if (num1 != num2)
        cout << num1 << " is not equal to " << num2 << endl;
    if (num1 < num2)
        cout << num1 << " is less than " << num2 << endl;
    if (num1 > num2)
        cout << num1 << " is more than " << num2 << endl;
    if (num1 <= num2)
        cout << num1 << " is less than or equal to " << num2 << endl;
    if (num1 >= num2)
        cout << num1 << " is more than or equal to " << num2 << endl;
}
return 0 is not needed at the end of main in c++. When main() successfully reaches the end of its scope, return 0 is done automatically.

main(), needs a type and that's int

In c++ it's iostream, not iostream.h.

endl, cin and cout live in the std namespace.
 

·
Registered
Joined
·
1,756 Posts
Namespaces are not required in C++, and iostream.h is a standard C++ include file, that isn't available in C. The return 0 at the end of main is optional, but it makes the code more readable if you make it clear where each function finishes. If you don't specify a return type for main() it uses int by default.
 

·
Registered
Joined
·
2,636 Posts
True, that namespaces are not required in c++, but when using STL headers, they are defined in the std namespace and you must tell the compiler that.

Including iostream.h is deprecated.

When using iostream, you need to tell the compiler where to find cout, cin, and endl etc. either by prepending std:: to each member used or doing it globally with the using namespace line. (You can get away with that by using iostream.h, but again, that's deprecated)

Yes, it is true that you *can* include the return 0 line at the end of main() if you like, but it's repetitive. I don't think for main(), that return 0 increases readability. It should be known that main() returns 0 when successfully reaching the end of its scope. Adding the return 0, would be like doing return 0; twice in C, but if you want to, it won't hurt.
 

·
Registered
Joined
·
1,756 Posts
I was programming in C++ before namespaces were even invented ;)

Yes, if you want to include <iostream> instead of <iostream.h> then you need to also include the "using namespace std" line, however it won't make any difference to the functionality of a program as simple as this. If you're saying that it's better to start off programming in C++ using namespaces to simplify things later on then I agree with you.
 

·
Registered
Joined
·
2,636 Posts
^^ Just an FYI that there's a modern way and an ancient, and the two ways might not mix well. Knowing that can save time/trouble, which wasn't an FYI to you as you appeared to already know from previous threads and posts, but may help the OP.
 
1 - 9 of 9 Posts
Status
Not open for further replies.
Top