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

Kursusindhold

Introduction to C++ | Mobile-Friendly

Introduction to C++ | Mobile-Friendly

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

book
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 >>.

cin >> x; // Get the value for the variable x

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

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

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

question mark

Which line should be used to get the user input and store it in the variable x?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

Introduction to C++ | Mobile-Friendly

Introduction to C++ | Mobile-Friendly

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

book
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 >>.

cin >> x; // Get the value for the variable x

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

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

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

question mark

Which line should be used to get the user input and store it in the variable x?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
some-alt