Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
User Input | Variables
Introduction to C++
course content

Contenido del Curso

Introduction to C++

Introduction to C++

1. Basics
2. Variables
3. Conditional Statements
4. Loops
5. Intro to Arrays

User Input

We already know how to output the data with the object cout and operator <<. However, sometimes we need to get values directly from the user. To enable this operation use cin in combination with the extraction operator >>.

For example, here we want to get the value for the variable a and output it:

123456789101112131415161718
01 #include <iostream> 02 using namespace std; 03 04 int main() { 05 // Declare the variable 06 int a; 07 // Print the message for user to get the value 08 cout << "Print a number: "; 09 // Get user input 10 cin >> a; 11 // Display the input of the user 12 cout << "Your number is: " << a; 13 14 return 0; 15 }
copy

In the line #9 the user print the value which will be stored in the variable a.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 3
toggle bottom row

User Input

We already know how to output the data with the object cout and operator <<. However, sometimes we need to get values directly from the user. To enable this operation use cin in combination with the extraction operator >>.

For example, here we want to get the value for the variable a and output it:

123456789101112131415161718
01 #include <iostream> 02 using namespace std; 03 04 int main() { 05 // Declare the variable 06 int a; 07 // Print the message for user to get the value 08 cout << "Print a number: "; 09 // Get user input 10 cin >> a; 11 // Display the input of the user 12 cout << "Your number is: " << a; 13 14 return 0; 15 }
copy

In the line #9 the user print the value which will be stored in the variable a.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 3
toggle bottom row

User Input

We already know how to output the data with the object cout and operator <<. However, sometimes we need to get values directly from the user. To enable this operation use cin in combination with the extraction operator >>.

For example, here we want to get the value for the variable a and output it:

123456789101112131415161718
01 #include <iostream> 02 using namespace std; 03 04 int main() { 05 // Declare the variable 06 int a; 07 // Print the message for user to get the value 08 cout << "Print a number: "; 09 // Get user input 10 cin >> a; 11 // Display the input of the user 12 cout << "Your number is: " << a; 13 14 return 0; 15 }
copy

In the line #9 the user print the value which will be stored in the variable a.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

We already know how to output the data with the object cout and operator <<. However, sometimes we need to get values directly from the user. To enable this operation use cin in combination with the extraction operator >>.

For example, here we want to get the value for the variable a and output it:

123456789101112131415161718
01 #include <iostream> 02 using namespace std; 03 04 int main() { 05 // Declare the variable 06 int a; 07 // Print the message for user to get the value 08 cout << "Print a number: "; 09 // Get user input 10 cin >> a; 11 // Display the input of the user 12 cout << "Your number is: " << a; 13 14 return 0; 15 }
copy

In the line #9 the user print the value which will be stored in the variable a.

Tarea

Let’s write the simple code to output the age of the user.

  1. Declare the variable age type of int.
  2. Get the value from the user which will be stored in the variable age.
  3. Display the variable age.

Don’t forget about semicolon ; at the end of statements.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 3
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt