Declaring your variables
This is no longer the case. You will get errors (in the linker at the bottom of Visual C++) like, "Unidentified identifier" or "undeclaredvariable" --something like that. The good thing is, that it will give you an idea of where the error is. Try not to pay too much attention to the exact line, because that may not always be the case. Anyways, If you wanted to do the calcuation above in Visual C++, it would look like this:
Don't worry about your variable ans being = to zero at first. This is just the initial value that needs to be set.
Int stands for integer. And their are other types of integers (short, long, and int).
There are also Floating-point numbers. These are numbers that contain a decimal. And there are other types of float numbers (float, double and long double). To declare a floating point number, it would look like this:
quite similar to int, just remember it has a decimal, so when you declare it use a decimal.
char is another type. This is short for character. A char is one character enclosed in single quotation marks. Some programmers pronounce it as "care", others say "char" like in charcoal- at any rate, you would declare a char like this:
String is a data type you will use. This is zero or more characters enclosed in double quotation marks. When you use a string, this is what the code should look like:
Finally their are boolean values. If you do not know, boolean is either True or False. Instead of using the full word boolean like in Visual Basic, you will use bool. Take a look below:
You want to make it a habit to declare your variables correctly, and not to mix them back and forth, this will cause tedious errors in your programs and can produce weird results because they are stored in a special memory location depending on the type. It can get quite technical. I'll explain a bit.
The computer stores numeric data in binary code, and character data in ASCII codes. 0 (zero) in AScII = 48; 0 (zero) in binary = 00110000. C++ will store it according to the data type -- so say you have the number 9. Well, if you assign that the type int: its memory location will read as the binary number 1001 (one 1 + one 8). If the data type is char: its memory location will will be stored as a character using ASCII code 57, which is represented in internal memory as 00111001 (one 1 + one 8 + one 16 + one 32). Catching on?
I will show you in a code sample, and you can later on execute the code to see the results for yourself.
the results will read for var1: 65, and for var2: A. Eventhough they equal the same thing, the results are different based on the datatype, because 01000001 is interpreted for a character as the ASCII code 65, which is equivalent to the letter, A. However, when you declare the type as int, 01000001 is interpreted as the binary code for decimal number 65.