A program may remember a count, a measurement, and a name. These values need different types. A type tells C++ what kind of value a variable stores and which operations make sense for it.
| Type | Use here | Example value |
|---|---|---|
int | Whole-number counts | 9 |
double | Numbers that may have a fractional part | 1.5 |
std::string | Text | "West room" |
#include <iostream>
#include <string>
int main() {
int boxes = 9;
double weight = 1.5;
std::string label = "West room";
std::cout << boxes << "\n";
std::cout << weight << "\n";
std::cout << label << "\n";
return 0;
}
<string> provides std::string; include it when using that type. The text value uses double quotes. Numeric values do not: "9" is text, while 9 is a number.
double is a floating-point type. It can represent fractional measurements, but many decimal fractions are only approximations in binary storage. We will use integer cents for exact small price calculations and convert for display later. A whole-number count does not need a fractional part, so int is a clearer choice for boxes or people.
An int has a limited range; it is not an unlimited mathematical integer. The small counts and cent amounts here fit within that range. For larger calculations, both the inputs and the calculated result must fit the chosen type.
The chair count in the last section used int. Here the three declarations need different types because they describe a guest count, a duration in hours, and a room name. Their types and output statements are already supplied. Replace each temporary value beside its TODO, keeping quotes around the room name but not around the numbers.
int variable guests to 14.double variable hours to 2.5.std::string variable room to "Oak Hall".Edit main.cpp, press Run to inspect the Console, then Submit to check these results.