C++ tutorials question about variables

I'm new on this stuff for the most part. I'm practicing using the tutorials on this site; Documentation-C++Tutorials-Variables/data types.

I'm trying the one that has you practice the variables ( a=5, b=2, a = a+1, result = a-b).

I don't understand why "a" will equal more than one variable! Shouldn't it really be written as "a=5, b=2, c=a+1 ???

I tried it both ways and it worked both ways. Is this typical or is there something wrong. I thought when I first read it that an "error" would have appeared.

Any thoughts?
Last edited on
variables can change, that's the whole point. They're like "containers" for information.

When you reassign a variable, the old value is lost.

1
2
3
4
5
int a;

a = 5;  // 'a' is 5

a = 3;  // now the 5 is lost, and 'a' becomes 3 
a = a + 1 just says to set a to the value of 1 + the current value of a. In other words, increment it by one.
Ok, I see somewhat, what you mean. But then if 'a = 5' is lost after the second 'a' is reassigned, then how does the program know that the 'a' in the second statement is '5' and not some other integer?
I'm not sure I understand your question.

Think of variables as a kind of box. They can hold a number, but they can only hold one number at a time.

1
2
3
4
5
6
7
8
a = 5;  // this puts the number 5 into the variable 'a'

a = 3;  // this puts the number 3 into the variable 'a' (replacing the number 5)

// the compiler doesn't need to know about the 5 here -- the 5 is gone forever

a = a + 1;  // this takes the number currently in 'a' (3), adds 1 to it (4), and puts
   // the result in 'a'  -- so that 'a' now holds the number 4 
Ok, I think it's just a brain fart. :)

So it doesn't just reassign "a" , but takes the original assignment, and then does what you want it to do after that. In this case, it would mean that " the original 'a' was '5', and now I want it to be
the 'original + 1'

If that makes since to you. It does to me!

Thanks for the feed back.
How about one more question!
At the end of the program, it has me write "return 0'. When I do this, the little black box that should appear on my screen with the current example ( I guess the DOS window) doesn't appear. It blinks out quickly.

But when I use ' system ("pause"); then it shows up and I can read what it says (the result of the program) until I hit another key to continue.

Is "return 0" used for the actual program in use and "system ("pause") just for practicing?
- It's not a DOS window ;P It a command prompt.

- Instead of system("pause"), you should use a safer/cleaner alternative like cin.get();

- return exits the current function. In this case, main() is the current function. When main exits, the program ends and the window closes.

- system("pause"); and cin.get(); are ways to wait for user input, so that the program remains open until the user is ready for it to close.
;p good one. The tutorial uses that as the example. I learned about 'system ("pause") from; "cppgameprogramming.com" and tried it and it left the command prompt open.

I see how the "cin.get();" is cleaner. Less info on the prompt.

Maybe the tutorial needs to be updated?

Thanks for the help.
Last edited on
Topic archived. No new replies allowed.