Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The extraction operator for input with
cin is >>. It reads data from the user into the variable.2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The insertion operator for output with
cout is <<. It sends data to the console.3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using << instead of >> with cin.
Using == or => which are not input operators.
✗ Incorrect
To read input with
cin, use the extraction operator >>. Using << or other operators causes errors.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up << and >> operators.
Using == instead of input/output operators.
✗ Incorrect
Use
>> to read input with cin and << to output with cout.5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of input/output operators.
Forgetting to print the variable
name.Mixing up << and >> operators.
✗ Incorrect
Use
>> to read input, << to output, and the variable name to print the string.