C++, lesson 3
std::cin ("see-in") is the standard input stream, the partner of std::cout. The >>
operator reads a value from it and stores it in a variable, converting the text to the
variable's type:
>> skips spaces, tabs and line breaks before each value, so these lines read 3 4 typed on
one line or on two:
Reading into a std::string with >> reads one word (up to the next whitespace). You'll
learn to read whole lines with std::getline in the strings lesson.
2 exercises to try what you just read.
If the input doesn't match the type (letters where a number was expected), the stream goes
into a failed state, the variable gets 0, and every later read does nothing. You can test
for that with if (std::cin >> a), which the loops and errors lessons use.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * | add, subtract, multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3 |
/ | divide | 7.0 / 2 | 3.5 |
% | remainder (integers only) | 7 % 2 | 1 |
*, / and % bind more tightly than + and -. Use parentheses when in doubt.
When both operands are integers, / throws away the fractional part (it truncates toward
zero), and % gives the remainder. Together they split a quantity into units:
To get a fractional result, make one operand a double. The clean C++ way is
static_cast:
A handy integer trick: "round up" division of positive numbers is (a + b - 1) / b. Seven
people at tables of four need (7 + 4 - 1) / 4 == 2 tables.
Dividing an integer by zero is undefined behaviour (usually a crash). Check before dividing.
By default std::cout prints up to six significant digits and drops trailing zeros: 2.50
prints as 2.5. For money and measurements you usually want a fixed number of decimals. Two
manipulators do it: std::fixed switches to fixed-point notation, and
std::setprecision(n) (from <iomanip>) then sets the number of digits after the point:
Both settings are "sticky": they stay in effect for every later number sent to std::cout.
Read three integers: the number of guests, slices each guest eats, and slices per pizza (all at least 1). Print how many whole pizzas to order so everyone gets their slices, and how many slices will be left over.
7 guests × 3 slices = 21 slices; 3 pizzas give 24, leaving 3.
Read the distance driven in kilometres and the fuel used in litres (both may have decimals, and the distance is greater than 0). Print the consumption in litres per 100 km and the distance per litre, each with two digits after the point.
int age{};
std::cin >> age;
std::cout << "Next year you will be " << age + 1 << '\n';int a{}, b{};
std::cin >> a >> b;int total_minutes{135};
int hours{total_minutes / 60}; // 2
int minutes{total_minutes % 60}; // 15int slices{7}, people{2};
double each{static_cast<double>(slices) / people}; // 3.5int points{10};
points += 5; // 15
points *= 2; // 30
++points; // 31
--points; // 30#include <iomanip>
double price{2.5};
std::cout << std::fixed << std::setprecision(2) << price << '\n'; // 2.50Input:
7 3 8
Output:
Pizzas: 3
Leftover slices: 3Input:
420 31.5
Output:
7.50 L/100km
13.33 km/L