0
0
C++programming~20 mins

Using cin for input in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Cin Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this code reading two integers?
Consider the following C++ code that reads two integers from the user and prints their sum. What will be the output if the user inputs 3 7?
C++
#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}
AError: invalid input
B37
C3 7
D10
Attempts:
2 left
๐Ÿ’ก Hint
Remember that cin reads values separated by spaces and stores them in variables.
โ“ Predict Output
intermediate
2:00remaining
What happens if input is a word instead of a number?
What will be the output or behavior of this code if the user inputs the word hello instead of a number?
C++
#include <iostream>
using namespace std;

int main() {
    int x;
    cin >> x;
    cout << x << endl;
    return 0;
}
AInput failure, variable x remains uninitialized or zero
Bhello
C0
DProgram crashes
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens when cin tries to read an integer but gets a non-numeric string.
๐Ÿ”ง Debug
advanced
2:00remaining
Why does this code not read the full line after reading an integer?
This code reads an integer and then tries to read a full line of text. Why does the second input read an empty string?
C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    int n;
    string line;
    cin >> n;
    getline(cin, line);
    cout << "Number: " << n << endl;
    cout << "Line: " << line << endl;
    return 0;
}
AThe newline after the integer input is still in the input buffer, so getline reads it as an empty line.
BThe variable 'line' is not initialized before getline, causing it to be empty.
CThe integer input fails, so getline reads nothing.
Dgetline cannot be used after cin >> for any input.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what remains in the input buffer after reading with cin >>.
๐Ÿ“ Syntax
advanced
2:00remaining
Which option correctly reads three integers using cin?
Which of the following code snippets correctly reads three integers a, b, c from the user using cin?
Acin >> a, b, c;
Bcin << a << b << c;
Ccin >> a >> b >> c;
Dcin >> (a, b, c);
Attempts:
2 left
๐Ÿ’ก Hint
Remember the operator used with cin to read multiple values.
๐Ÿš€ Application
expert
2:00remaining
What is the value of x after this input sequence?
Given the following code, what will be the value of x after the user inputs 42 hello 100?
C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    int x;
    string s;
    cin >> x >> s >> x;
    cout << x << endl;
    return 0;
}
A42
B100
Chello
DInput failure, x is uninitialized
Attempts:
2 left
๐Ÿ’ก Hint
The input reads in order: integer, string, integer. The last integer overwrites x.