Lesson 2 - Input, Variables, Computation, And Output
Prerequisites
Section titled “Prerequisites”- Lesson 1. You can build and run a complete program that prints a fixed line.
- A working C++ setup, as described in Lesson 1.
What You Will Learn
Section titled “What You Will Learn”- How to declare an
intvariable with a clear name. - How to read values from standard input with
std::cin. - How to compute a value and print it with
std::cout.
Output That Depends On Input
Section titled “Output That Depends On Input”In Lesson 1 the output was fixed. Now the program reads numbers first, so the output depends on what it is given.
Three new pieces appear:
- A variable is a named place that holds one value.
int firstNumber = 0;declares an integer variable namedfirstNumberand starts it at0. std::cinis the standard input stream.std::cin >> firstNumber;reads one integer from the input and stores it infirstNumber.>>moves data from the stream on its left into the variable on its right. It points the opposite way from the<<used for output.
Whitespace between input numbers does not change how they are read. Spaces, tabs, and line breaks all act as separators, so 7 5 on one line and 7 then 5 on two lines are read the same way.
Worked Example
Section titled “Worked Example”Read two integers, store them in named variables, and print their sum.
#include <iostream>
int main() { int firstNumber = 0; int secondNumber = 0;
std::cin >> firstNumber >> secondNumber;
int sum = firstNumber + secondNumber;
std::cout << sum << "\n"; return 0;}Input:7 5Output:12Notes on the code:
std::cin >> firstNumber >> secondNumber;reads two integers in order. The first value read goes intofirstNumberand the second intosecondNumber.int sum = firstNumber + secondNumber;computes the result once and gives it a name. Naming the result keeps the printing step simple.std::cout << sum << "\n";prints the value and then a newline. Printing a variable needs no quotation marks, becausesumholds a value rather than text.
Exercise
Section titled “Exercise”Write a complete program that reads one integer and prints twice its value.
Start from the worked example. These are all of the source changes required:
- Delete the line
int secondNumber = 0;, because only one input value is needed now. - Change
std::cin >> firstNumber >> secondNumber;tostd::cin >> firstNumber;, so the program reads one integer instead of two. - Replace
int sum = firstNumber + secondNumber;withint doubled = firstNumber * 2;, which multiplies the input value by two. - Change
std::cout << sum << "\n";tostd::cout << doubled << "\n";, so the printed name matches the new variable.
Leave #include <iostream>, int main(), and return 0; unchanged.
Exercise Check
Section titled “Exercise Check”Input:7Expected output:14Check these before moving on:
- The output is
14and nothing else - The output still ends with a newline, as in the worked example
- Input
0prints0, and input100prints200
Next Steps
Section titled “Next Steps”You can now read input, name values, compute, and print. The next lesson uses only these tools on a task whose input and output format is fixed in advance.