declaring variables with ()

i am trying to self learn c++ from the 'documentation' part of this site, and am already stuck =( can anyone tell me why my result is 4 when it's supposed to be 6? please! sorry if this is a very beginner question

#include <iostream>
using namespace std;

int main ()
{
int a=5;
int b(2);
int result;

a = a + 3;
result = a - b;
cout << result;
system("pause");
return 0;
}
The result is 6, and that is what your program is showing. (after line 11, before it has garbage)
You are delusional.


Side note: ¿didn't you see that wonderful template so your code can be pretty printed?
[code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()
{
	int a=5;
	int b(2); 
	int result;

	a = a + 3;
	result = a - b;
	cout << result << endl; //a line break, for the output to look better
	//system("pause"); //read http://www.cplusplus.com/articles/iw6AC542/
	return 0;
}
[/code]
Last edited on
Well... you don't need to ask sorry.... this is the beginner section, after all ^^
(I am not a professional programmer too :P)

The only thing I suggest you is to include your code in code tags (button "<>" in "Format" that opens for you the code tags [ code] [/ code])

however.... to be honest I am not able to give you an answer. I readed your code a lot of times and I didn't see anything that explains this strange behaviour.
try to make this replace

1
2
//replaces cout << result;
cout << "a = " << a << " ; b = " << b << " ; result = " << result;


In this way you can "watch" all values. Perhaps "b" will be not 2 as expected. In that case the only explaination I can think is: bad (or old) compiler: try a newer one.

(PS I self learned all I know about c++ too.... it requires time and probably, like me, you will not become a pro, but you would be able to make a lot of things if you apply hardly ^^)
I compiled your code and it worked exactly as expected. I've been told not to blame the compiler, but if nobody is getting your error, then it might be something wrong with your compiler. I use Visual Studio, but there are tons of other options out there if you want.
We all agree that there is no error in code and no any single reason why the program could return 4 instead of 6.
It is useless to test the code ourselves becouse it is evident that there aren't errors in code (also reported in a tutorial here in cplusplus.com)

But he posted the question becouse he claims that the problem of "wrong value" happened to him. We have no element to claim he is saying a wrong thing.

So the only explaination I could find is "perhaps it is the compiler". However this is why I purposed to modify the code and make it more "verbose", writing all the 3 final values (a, b, result) in the console and not only the value of result.

In this way it would be possible to understand where the problem is stored, exactly becouse usually compilers does not contains error (they are deeply tested and professionally developed) but, in this specific case, I cannot figure any other reasonable explaination of the problem reported by decades.

Note: I don't think he made errors becouse he copied correctly the code here so we can assume he wrote correctly his own code also in his own cpp file
Last edited on
well thank you all for the response, i really was confused because i wrote it after memorizing the 'documentation' and then got the wrong answer so i tried copying and pasting it from the site in a new file and it came with the right response so maybe it was just a glitch with my compiler
@ne555, ill try that template next time :)
and thanks noburn, self learning c++ seems a bit harder than java/html
Topic archived. No new replies allowed.