C++ User Input

In C++, programs can interact with users by receiving input from the keyboard. While cout is used to display output, cin allows the program to capture input using the extraction operator (>>).

Tutorials dojo strip

Reading User Input

To prompt a user to enter a value, use cin along with a variable to store the input.

Example

C++

Explanation:

  • cout displays a message asking the user to enter a number.
  • cin >> engineCC; stores the user’s input in the variable engineCC.
  • The value entered is then displayed using cout.

Understanding cout and cin

  • cout (pronounced “see-out”) → Displays information using the insertion operator (<<).
  • cin (pronounced “see-in”) → Captures input using the extraction operator (>>).

Creating a Simple Calculator

Let’s build a program that asks the user for two numbers, adds them, and displays the result.

Example

C++

Explanation:

  • The user enters two values (speedA and speedB).
  • The program adds them and displays the result.

Tutorials dojo strip