Jump to content

C++ Lessons : 7. User Input


quix
 Share

Recommended Posts

  • Manager

C++ User Input

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

Good To Know

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!

 

1.png.56210348f845fad9f2d8f843d77e3c16.png

Click! : adelinrzo

Click! quiixx.

Click! : raizo

 

Guardian of Leaks!

 

image.png.3ca1bf60c26a3523b93df92ea8521802.png (24.01.2023)

image.png.990bdf55e8ebaad7e92cc11967598b8d.png (12.03.2023)

image.png.25e5d76f6f7de46f49423812d3d500a3.png (30.03.2023)

Link to comment
Share on other sites

  • quix locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...