Back to Course

Introduction to C++

0% Complete
0/0 Steps
  1. C++: Basics
    5Topics
    |
    6 Quizzes
  2. C++: Compilation and Execution
    5Topics
    |
    6 Quizzes
  3. C++: Variables
    9Topics
    |
    10 Quizzes
  4. C++: Conditions Operators
    5Topics
    |
    6 Quizzes
  5. C++: Logical Operators
    3Topics
    |
    4 Quizzes
  6. C++: Loops and Errors
    8Topics
    |
    9 Quizzes
  7. C++: Vectors
    4Topics
    |
    3 Quizzes
Lesson 3, Topic 7
In Progress

C++: Inputs from Users

Yasin Cakal 6 Nov 2021
Lesson Progress
0% Complete

As we learned earlier, we also use variables to take inputs from the program users. Now, how to do that?

We take inputs from the users using a standard input stream written as cin. This is pronounced as see-in. The operands that follow cin are >> which are different from the ones used for cout<<.

Let’s try using this in a program:

#include <iostream>

int main() 
{
	int year;
	std::cout<<"When were you born?\n";
	std::cin>>year; 
	std::cout<<"I was born in "<<year;
	return 0;
}

Output:

When were you born?
1990
I was born in 1990

Let’s understand what this code does.

  • The terminal first displays the sentence When were you born? to give the user information of what kind of input is required from the user.
  • The user then inputs a value which is shown in the terminal as the user types it. In this case, the user types 1990.
  • Then terminal then prints the line I was born in 1990 where 1990 is the input from the user.

Responses

Leave a Reply

%d bloggers like this: