1.5 — Introduction to iostream: cout, cin, and endl

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

35 Terms

1
New cards

What does std::cout do in C++?

It prints data to the console.

2
New cards

What header file must be included to use std::cout or std::cin?

#include <iostream>

3
New cards

What does cout stand for?

Character output

4
New cards

Which operator is used with std::cout to send data to the console?

<< (insertion operator)
5
New cards

What does this line print: std::cout << "Hello" << " world!";

Hello world!

6
New cards

What is the purpose of std::endl?

It outputs a newline and flushes the output buffer.

7
New cards

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.

8
New cards

What symbol is used for newline in a string literal?

\n

9
New cards

Which operator is used with std::cin to receive input from the user?

>>(extraction operator)

10
New cards

What does std::cin >> x; do?

It reads input from the keyboard and stores it in variable x.

11
New cards

What is the output of this code?

int x{ 5 };
std::cout << "x is equal to: " << x;

x is equal to: 5

12
New cards

What happens if std::endl is used multiple times in a loop?

It causes multiple buffer flushes, which can slow down performance.

13
New cards

What kind of output is std::cout considered?

Buffered output

14
New cards

What analogy is used to describe how buffered output works?

A rollercoaster ride that boards passengers in batches

15
New cards

What happens if a program crashes before std::cout is flushed?

Any buffered output may not be displayed.

16
New cards

What is the ASCII value of the linefeed (LF) character?

10

17
New cards

What is the conventional way to write \n when not embedded in a string?

Single quoted: '\n'

18
New cards

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.

19
New cards

When entering input using std::cin, what key must be pressed to submit input?

Enter

20
New cards

What does the input buffer do in std::cin?

Stores characters typed by the user before they are extracted.

21
New cards

What kind of buffer behavior does std::cin use?

FIFO (First In, First Out)

22
New cards

What happens if std::cin fails during extraction?

The variable is set to 0, and future extractions fail until std::cin is cleared.

23
New cards

What do std::cin and std::cout always appear on?

The left-hand side of the operator (>> or <<)

24
New cards

What happens if the input is 5a for std::cin >> x;?

5 is extracted to x, and a remains in the buffer.

25
New cards

What does std::cin >> x >> y; do if the user inputs 4 5?

Extracts 4 to x, then 5 to y

26
New cards

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.

27
New cards

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.

28
New cards

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.

29
New cards

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.

30
New cards

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).

31
New cards

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.

32
New cards

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.

33
New cards

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.

34
New cards

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.

35
New cards

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;
}