To convert a variable from one type to another, the syntax looks like:
TYPE1 X = VALUE; // Here X is declared to be TYPE1 TYPE2 Y = (TYPE2)X; // The code '(TYPE2)X' converts X to TYPE2.
We call this typecasting X to type TYPE2. Often this is not necessary, because if I try to assign a variable to be a different type then C++ will automatically try to convert it for me. In other words, the following two conversions have exactly the same results:
double x = 3.6; int integerVersion = (int)x; // convert x to int, then assign the result to integerVersion int integerVersion2 = x; // x is the wrong type, so C++ automatically converts x to int for us
However, there will still be plenty of cases where manually converting between types is important.
Note on conversions: C++ will generally 'try to do something reasonable' when you ask it to convert between types, but the rules aren't necessarily obvious. With the types we know so far, it works like:
One more note: This isn't the only syntax you can use for typecasting. The code above uses a "C-style typecast". You will also learn about "C++-style typecasts", which look messier but are arguably nicer in some ways.
Within a given line of code, operations happen in the following order:
For example, if I run
int x = 7; int y = 2; double quotient = x / y; cout << "result is " << quotient << "\n";
then the last line will FIRST compute the division x / y; this is a division of two ints, so we get an int: 7/2 = 2. THEN the result will be stored in the double quotient. 2 is an integer, so it gets automatically converted to the decimal number 2.0. Thus the output says 2. On the other hand, if I run
int x = 7; int y = 2; double quotient = (double)x / y; cout << "result is " << quotient << "\n";
then C++ first converts the int 7 to the double 7.0. Next comes division; we're dividing a double (7.0) by an int (2), so we get a double (3.5). Last comes assignment. The division returned a double this time, so we store 3.5 in quotient. In summary, be careful with order of operations or you can get tripped up!
In discussion we created the file orderOfOperations.cpp, which shows results of some tricky examples.
We wrote howManyItems.cpp, in which we used things we've learned about arithmetic and type conversions to solve a simple programming 'puzzle'. (The program's goal is described in a comment at the start.)
Note that the program we wrote actually has a bug! See 'Extra stuff' section below. Maybe I'll post the answer in a few days.
Here are some Google searches you could try if you're having trouble with today's material. I'm deliberately trying to choose search terms that you could have thought of yourself!
The program howManyItems.cpp above is not quite correct. In discussion I showed that it gets the wrong answer if the user types 9.28 as input. (Output is 28, but the correct answer is 29.) I also promised that 9.28 is the first case that fails, assuming the user types a dollars-and-cents amount that's greater than or equal to 0. That said, I could find plenty more "problem" cases if I want to. Therefore, your advanced assignment is:
Let me know if you thought / tested / Googled about this for awhile and either made progress or want a hint!