0
0
C++programming~10 mins

Input and output using cin and cout 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 causes a syntax error.
Using == is a comparison, not input.
Using >>= is a bitwise operator, not for input.
2fill in blank
medium

Complete the code to print a message to the console using cout.

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

int main() {
    cout [1] "Hello, world!" << 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 cout causes a syntax error.
Using == is a comparison, not output.
Using >>= is a bitwise operator, not for output.
3fill in blank
hard

Fix the error in the code to correctly read a floating-point number using cin.

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

int main() {
    float 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 == or => which are not input operators.
4fill in blank
hard

Fill both blanks to create a program that reads two integers and prints their sum.

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

int main() {
    int a, b;
    cin [1] a >> b;
    cout [2] "Sum: " << (a + b) << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
C==
D>>=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up << and >> operators.
Using == instead of input/output operators.
5fill in blank
hard

Fill all three blanks to read a string and an integer, then print them in a sentence.

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

int main() {
    string name;
    int age;
    cin [1] name >> age;
    cout [2] "Name: " << [3] << ", Age: " << age << endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A>>
Bname
C<<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of input/output operators.
Forgetting to print the variable name.
Mixing up << and >> operators.