0
0
C++programming~10 mins

Using cin for input in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read an integer from the user using cin.

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

int main() {
    int number;
    cin [1] number;
    cout << "You entered: " << number << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<<
B>>
C==
D>>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with cin.
Using assignment operator = instead of >>.
2fill in blank
medium

Complete the code to read a double value from the user using cin.

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

int main() {
    double value;
    cin [1] value;
    cout << "Value: " << value << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<<
B=
C==
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with cin.
Using assignment operator = instead of >>.
3fill in blank
hard

Fix the error in the code to correctly read a string using cin.

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

int main() {
    string name;
    cin [1] name;
    cout << "Hello, " << name << "!" << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<<
B=
C>>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with cin.
Using = instead of >>.
4fill in blank
hard

Fill both blanks to read two integers from the user and print their sum.

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

int main() {
    int a, b;
    cin [1] a [2] b;
    cout << "Sum: " << a + b << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> for input.
Using only one operator for two variables.
5fill in blank
hard

Fill all three blanks to read a character, an integer, and a double, then print them.

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

int main() {
    char ch;
    int num;
    double val;
    cin [1] ch [2] num [3] val;
    cout << "Char: " << ch << ", Int: " << num << ", Double: " << val << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<<
B>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> for input.
Missing one or more input operators.