Small Steps I — Creating with Text and Numbers
Calculating with an entered number
Read one number and print twice its value. Entering 7 should produce 14.
What you will learn
input_int reads the input as an integer and returns it. Use it differently from input, which returns a string.
Try it
Example · example.cpp
void small_main()
{
int age = input_int();
print(age + 1);
}
Type 10 in the console and press Enter. Adding 1 to the integer 10 produces 11. Enter one integer per line. If you enter text that is not a number, a prompt asks you to try again.
Expected output (excluding input prompts):
11
Exercise
Read one number and print twice its value. Entering 7 should produce 14.
Exercise starter · exercise_starter.cpp
void small_main()
{
int age = input_int();
print(age + 1);
}
Hint
Change the parts of the example that you need.
show solution
void small_main()
{
int number = input_int();
print(number * 2);
}