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.
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?
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
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'
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?
;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.