My First C++ Program xD

Im beginning to learn C++, right now I am in the variables lesson. I am using Visual Studio C++. Anyway, this is my first C++ program. It is very simple, it just outputs the sum of 89 + 78. What do you think?

1
2
3
4
5
6
7
8
9
10
include <iostream>
using namespace std;
{
  int main()
     int a=89;
     int b=78;
     int sum=0;
     cout << sum=a+b << endl;
     return 0;
}


I hope to become a C++ guru someday, So I need all the advice and tips from you pro C++ programmers that I can get. Thanks.
Last edited on
What guide are you following? :) If you are stuck, this website has a great beginners tutorial: http://www.cplusplus.com/doc/tutorial/

As for your program, int number 2 isnt right, you cant have spaces in identifiers, your compiler will think your refering to something else, and a little tip, you can just say cout << (number + number_2) << endl;, but if you plan on using that value again then you should store it in a variable
Last edited on
closed account (j2NvC542)
Does that compile? Because of what Owain just pointed out.
closed account (zb0S216C)
On line 7, your variable declaration isn't valid, because identifiers cannot contain spaces. Remove the space between number and 2 then you'll be set. This applies to line 8, too.

Bobby94 wrote:
"So I need all the advice and tips from you pro C++ programmers that I can get."

Well, advice is a general term. Do you want advice for efficiency, readability, maintainability, ...? For your program, there's little advice we can give you.

Good thus far, though. So far, so good. Keep it up.

Wazzal
Well, the first thing I noticed that I would change (Im not pro, but I'm pretty dang good for a newbie) is that you have using namespace inside your main. I don't know if that will affect your program at all, and it probably wont, but I was taught to put it outside main, just under the header files. I would also point out that the variable int number 2 is invalid, but that seems to have already been taken care of =D.

Also, something that I'd do to the code, for readability, and just to be safe from errors later on, is to do this:
1
2
int sum = 0;
sum = number + number2;


Rather than what you did here:
int sum = number + number2;

One last thing I would do is to put all similar variables, 'int', on the same line (just to save space).
int number = 89, number2 = 78, sum = 0;

Oh, and good job for ending the line when it seems redundant. That's always good programming practice. :D

I hope this here semi-noob advice helps!
Well, the first thing I noticed that I would change (Im not pro, but I'm pretty dang good for a newbie) is that you have using namespace inside your main. I don't know if that will affect your program at all, and it probably wont, but I was taught to put it outside main, just under the header files.
It dosent matter if it is inside the main function, but it is a good idea to have it outside, so then any declerations of classes or structures etc. are affected by it.
Thanks guys, How is this code? I fixed the errors.

1
2
3
4
5
6
7
8
9
10
include <iostream>
using namespace std;
{
  int main()
     int a=89;
     int b=78;
     int sum=0;
     cout << sum=a+b << endl;
     return 0;
}
Last edited on
closed account (zb0S216C)
On line 8, swap the assignment round. The trick to the assignment operator is to read it as: "Move X into Y". In your case, move the result of a + b into sum.

Wazzak
Why initialize a variable and then change its value on the next line?
int sum = number + number2;
It''s just good programming practice. It reduces the chance of an error if he were to make this into a much more complex program. Also, one thing I learned (That has no relevance to this code at all) is that if you initialize a variable as you make it, you are able to later check whether or not it has been assigned a value.

e.g.
1
2
3
4
5
6
7
8
9
10
11

    int a, b, x=0;
    cin >> a >> b;

    x=a+b;

    if(x!=0)
        cout << "x has been assigned a value." << endl;
    else
        cout << "x has not been assigned a value." << endl;
Last edited on
I would like to point out:

1
2
3
4
5
6
7
8
9
10
include <iostream>
using namespace std;
{
  int main()
     int a=89;
     int b=78;
     int sum=0;
     cout << sum=a+b << endl;
     return 0;
}


On line 8, I would just do this:

cout<< (a + b)<< endl;

It does the exact same thing, and you can scratch sum.

Also, you'r braces are wrong.
Last edited on
@ Bobby94: you use braces incorrectly.

Hint:
1
2
3
4
int functionName()
{
    // function body
}


Also for simplicity, you could simply do cout << 89 + 78 << endl;

And a fun fact: main() is an exception from the rule that non-void functions must return a value. You can safely remove the return 0; at the end.
closed account (j2NvC542)
A mate of mine fixed a problem by adding return 0; at the end of his main() just yesterday. At least that's what he told me, haven't tested myself.
Must have been the compiler. Code::Blocks' 10.05 mingw.
Your int main() on line 4 should be before the braces. '{}'
Last edited on
You need a '#' before the "include<iostream>!
Bobby is it working yet? cuz I think we covered absolutely everything.
Yes, it's working. Thanks guys!
Topic archived. No new replies allowed.