Using Input (cin) with Variables in C++

Using Input (cin) with Variables in C++

Medium C++ Variables 34 views
c++ input variables cin basics
Explanation Complexity

Problem Statement

In this problem, you will learn how to take input from the user using variables in C++.
You will use cin to read values and store them in variables.
After taking input, you will display the values using cout.
This question helps beginners understand how input and output work together in C++.

Input Format

First line contains an integer.
Second line contains a decimal number.

Output Format

Print the entered values.

Example

21
99.5
Number: 21
Value: 99.5

Constraints

1 ≤ number ≤ 1000
0 ≤ value ≤ 10^6

Concept Explanation

In C++, variables are used to store data.
Sometimes values are not fixed and need to be entered by the user.
For this purpose, C++ provides the cin statement.

cin is used to take input from the keyboard.
The input value is stored in a variable.
The type of variable decides what kind of data can be stored.

For example, an int variable stores whole numbers,
a double variable stores decimal values,
and a string variable stores text.

To show output on the screen, we use cout.
Using cin and cout makes programs interactive.
User input makes the program more useful and real-world friendly.

Step-by-Step Explanation

Step 1: Understand the goal: take input and display it.
Step 2: Choose the main idea: input using cin.
Step 3: Declare variables with correct data types.
Step 4: Ask the user for input.
Step 5: Store input values using cin.
Step 6: Print the values using cout.
Step 7: Check the output.

Complexity Analysis

Time
O(1)
Space
O(1)

Interview Tips

• Explain difference between cin and cout
• Know how input is stored in variables
• Be ready to handle invalid input

Custom Examples

int id;
string name;
cin >> id;
cin >> name;

Concept Explanation

In C++, variables are used to store data.
Sometimes values are not fixed and need to be entered by the user.
For this purpose, C++ provides the cin statement.

cin is used to take input from the keyboard.
The input value is stored in a variable.
The type of variable decides what kind of data can be stored.

For example, an int variable stores whole numbers,
a double variable stores decimal values,
and a string variable stores text.

To show output on the screen, we use cout.
Using cin and cout makes programs interactive.
User input makes the program more useful and real-world friendly.

Step-by-Step Explanation

Step 1: Understand the goal: take input and display it.
Step 2: Choose the main idea: input using cin.
Step 3: Declare variables with correct data types.
Step 4: Ask the user for input.
Step 5: Store input values using cin.
Step 6: Print the values using cout.
Step 7: Check the output.

Input / Output Format

Input Format
First line contains an integer.
Second line contains a decimal number.
Output Format
Print the entered values.
Constraints
1 ≤ number ≤ 1000
0 ≤ value ≤ 10^6

Examples

Input:
21 99.5
Output:
Number: 21 Value: 99.5
int id;
string name;
cin >> id;
cin >> name;

Example Solution (Public)

C++
#include <iostream>
using namespace std;

int main() {
    int number;
    double value;

    cout << "Enter a number: ";
    cin >> number;

    cout << "Enter a value: ";
    cin >> value;

    cout << "Number: " << number << endl;
    cout << "Value: " << value << endl;

    return 0;
}

Official Solution Code

#include <iostream>
using namespace std;

int main() {
    int number;
    double value;

    cout << "Enter a number: ";
    cin >> number;

    cout << "Enter a value: ";
    cin >> value;

    cout << "Number: " << number << endl;
    cout << "Value: " << value << endl;

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.