bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/C++/C++ Tutorial
C++•C++ Tutorial

C++ User Input

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind C++ User Input?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ << "Type a number: "; // Type a number and
3Order

Put the learning moves in the order that makes the concept easiest to apply.

cin is a predefined variable that reads data from the keyboard with the extraction operator ( >> ).
You have already learned that cout is used to output (print) values.
Creating a Simple Calculator

You have already learned that cout is used to output (print) values. Now we will use cin to get user input.

cin is a predefined variable that reads data from the keyboard with the extraction operator ( >> ).

In the following example, the user can input a number, which is stored in the variable x . Then we print the value of x :

Example

int x;
cout << "Type a number: "; // Type a number and
press enter
cin >> x; // Get user
input from the keyboard
cout << "Your number is: " << x;
// Display the input value

cout is pronounced "see-out". Used for output , and uses the insertion operator ( << )

cin is pronounced "see-in". Used for input , and uses the extraction operator ( >> )

Creating a Simple Calculator

In this example, the user must input two numbers. Then we print the sum by calculating (adding) the two numbers:

Example

int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;

There you go! You just built a basic calculator!

Previous

C++ Constants

Next

C++ Data Types