[help]C++ beginner noob # 2

Posts 1–15 of 72 · Page 1 of 5
[help]C++ beginner noob # 2
hello guys
i just finished learning about variables and i incorporated it into my 2nd program, but i just want to know if i made any errors with the declarations or anything like that.


// C++ program Number 2
# include <iostream>
using namespace std;
int main ()
{
//declaring variables
int mpgh1;
int mpgh2;

// Process:
mpgh1 = 3;
mpgh2 = 1;
mpgh1 = mpgh1 = 3;
result = mpgh1 - mpgh2;

//functions to print out the results
cout << result;
//terminte (ends the program)
return 0;
}
What is the point of this

Code:
mpgh1 = mpgh1 = 3;
You dont even need that line of code, if you already declared it with an integer at the beginning.
it was an accident i realized my mistake after i posted the thread
Oh ok, well besides that its all good.

Bit of advice:

If your making a small program like this dont use:

Code:
using namespace std;
just actually spell it out, so you can get into the habit of doing it. Only use it when your going to use cout or cin alot.
I use namespace std all the time even for one liners, because I'm a lazy bastard and I know how it works anyway.

EDIT: plus it get really old when you start using flags and end up with something like this: std::ios::in ... eww.
Perfecto. Cept the extra mpgh1=
result is undefined
Quote Originally Posted by whit View Post
result is undefined
What do you mean result is undefined. Isnt it closing correctly because its returning 0?
Quote Originally Posted by aanthonyz View Post
What do you mean result is undefined. Isnt it closing correctly because its returning 0?
He didn't declare result :P he basically just assigned a value to some random letters :3
Quote Originally Posted by mookamoka3 View Post
He didn't declare result :P he basically just assigned a value to some random letters :3
Put simply, he must declare 'result' with type like int, float, unsigned int etc.. Of course there's no need now to use float or uint.
Quote Originally Posted by kamilos93 View Post
Put simply, he must declare 'result' with type like int, float, unsigned int etc.. Of course there's no need now to use float or uint.
What he said

but yeah, just int result; written pretty much anywhere before you tried to assign it a value would've been just dandy.
Quote Originally Posted by mookamoka3 View Post
What he said

but yeah, just int result; written pretty much anywhere before you tried to assign it a value would've been just dandy.
Of course he can make like You've said:
Code:
int result;
result = mpgh1 - mpgh2;
or:
Code:
int result = mpgh1 - mpgh2;
Oh, I completely missed that wow...
Quote Originally Posted by aanthonyz View Post
Oh, I completely missed that wow...
Same. It took me legit 10 minutes after i saw whit say it.

Sneaky ass little error.
Posts 1–15 of 72 · Page 1 of 5

Post a Reply

Tags for this Thread

None

Talk with us