1/34
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What does std::cout do in C++?
It prints data to the console.
What header file must be included to use std::cout or std::cin?
#include <iostream>
What does cout stand for?
Character output
Which operator is used with std::cout to send data to the console?
What does this line print: std::cout << "Hello" << " world!";
Hello world!
What is the purpose of std::endl?
It outputs a newline and flushes the output buffer.
Why is it better to use \n over std::endl in most cases?
Because \n outputs a newline without flushing the buffer, making it more efficient.
What symbol is used for newline in a string literal?
\n
Which operator is used with std::cin to receive input from the user?
>>
(extraction operator)
What does std::cin >> x;
do?
It reads input from the keyboard and stores it in variable x.
What is the output of this code?
int x{ 5 };
std::cout << "x is equal to: " << x;
x is equal to: 5
What happens if std::endl is used multiple times in a loop?
It causes multiple buffer flushes, which can slow down performance.
What kind of output is std::cout considered?
Buffered output
What analogy is used to describe how buffered output works?
A rollercoaster ride that boards passengers in batches
What happens if a program crashes before std::cout is flushed?
Any buffered output may not be displayed.
What is the ASCII value of the linefeed (LF) character?
10
What is the conventional way to write \n when not embedded in a string?
Single quoted: '\n'
What does this program do?
std::cout << "Enter a number: ";
int x{};
std::cin >> x;
std::cout << "You entered " << x << '\n';
Prompts the user for a number and prints it back.
When entering input using std::cin, what key must be pressed to submit input?
Enter
What does the input buffer do in std::cin?
Stores characters typed by the user before they are extracted.
What kind of buffer behavior does std::cin use?
FIFO (First In, First Out)
What happens if std::cin fails during extraction?
The variable is set to 0, and future extractions fail until std::cin is cleared.
What do std::cin and std::cout always appear on?
The left-hand side of the operator (>> or <<)
What happens if the input is 5a for std::cin >> x;?
5 is extracted to x, and a remains in the buffer.
What does std::cin >> x >> y;
do if the user inputs 4 5
?
Extracts 4 to x
, then 5 to y
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A letter, such as h
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: 0 is always printed.
What’s happening: An integer can’t hold a letter, so extraction completely fails. x
is assigned the value 0.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A number with a fractional part (e.g. 3.2
). Try numbers with fractional parts less than 0.5 and greater than 0.5 (e.g. 3.2
and 3.7
).
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: The fractional part is dropped (not rounded).
What’s happening: Given the number 3.2
, the 3
gets extracted, but .
is an invalid character, so extraction stops here. The .2
remains for a future extraction attempt.
If you are wondering why this isn’t a disallowed narrowing conversion, narrowing conversions are only disallowed during list-initialization (which happens on line 6). The extraction happens on line 7.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A small negative integer, such as -3
.
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: The entered number is output.
What’s happening: A minus sign at the beginning of a number is acceptable, so it is extracted. The remaining numbers are extracted as well.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A word, such as Hello
.
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: 0 is always printed.
What’s happening: An integer can’t hold a letter, so extraction completely fails. x
is assigned the value 0.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A really big number (at least 3 billion).
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: You are most likely to get the number 2147483647
.
What’s happening: x
can only hold numbers up to a certain size. If you enter a value larger than the largest number x
can hold, it will be set to the largest number that x
can hold (which is probably 2147483647
, but might be different on your system).
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A small number followed by some letters, such as 123abc
.
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: The numeric values are printed (e.g. 123).
What’s happening: 123
is extracted, the remaining characters (e.g. abc
) are left for a later extraction.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: A few letters followed by a small number, such as abc123
.
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: 0 is always printed.
What’s happening: An integer can’t hold a letter, so extraction completely fails. x
is assigned the value 0.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: +5
(three spaces, followed by a plus symbol, and a 5).
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: 5 is printed.
What’s happening: The leading whitespace is skipped. Plus is a valid symbol at the start of a number (just as a minus sign would be), so it is extracted. The 5 is also extracted.
The program expects you to enter an integer value, as the variable x
that the user input will be put into is an integer variable.
Run this program multiple times and describe the output that results when you enter the following types of input: 5b6
#include <iostream> // for std::cout and std::cin
int main()
{
std::cout << "Enter a number: "; // ask user for a number
int x{}; // define variable x to hold user input
std::cin >> x; // get number from keyboard and store it in variable x
std::cout << "You entered " << x << '\n';
return 0;
}
Result: 5 is printed.
What’s happening: 5
is extracted. b
is invalid, so extraction stops here. The b6
remains for a future extraction attempt.
Ask the user to enter three values. The program should then print these values. Add an appropriate comment above function main()
.
The program should match the following output (when run with input values 4
, 5
, and 6
):
#include <iostream>
// Asks the user to enter three values and then print those values as a sentence.
int main()
{
std::cout << "Enter three numbers: ";
int x{};
int y{};
int z{};
std::cin >> x >> y >> z;
std::cout << "You entered " << x << ", " << y << ", and " << z << ".\n";
return 0;
}