Hard Version of Quiz

HARD VERSION OF THE QUIZ (WITH ANSWERS + PIXAR EXPLANATIONS)

These questions simulate real-world C++ bugs.


HARD QUIZ — 15 QUESTIONS


H1. What does this output? Why?

int a;
cout << "Enter number: ";
cin >> a;
cin.ignore(2, '\n');
cout << "Done";

User enters:

12345

H2. Predict behavior:

double d;
cin >> d;
cin >> d;

User types:

3.14abc

H3. Why does this fail to read two ints?

stringstream ss("10 20");
int x, y;
ss >> x;
ss.str("30 40");
ss >> y;

H4. Consider:

ofstream f("test.txt");
f << fixed << setprecision(3) << 12.34567;
f.close();

What ends up in the file?


H5. What does this produce?

cout << showpoint << setprecision(3);
cout << 5.0;

H6. Why does this not append?

ofstream f("log.txt", ios::out | ios::app | ios::trunc);

H7. What’s wrong with this CSV reader?

string id;
int price;

getline(f, id, ',');
f >> price;

H8. Why does this skip lines?

int x;
string line;

while (cin >> x) {
    getline(cin, line);
}

H9. Which manipulator affects ONLY the next item?

(A) setprecision
(B) fixed
(C) setw
(D) showpoint


H10. What does this print?

cout << setprecision(3);
cout << 12345.6789;

(no fixed)


H11. Why does this infinite loop occur?

while (cin >> x) {
    // nothing inside
}
cout << "Done";

User presses Ctrl+D (EOF).


H12. Explain all behavior:

File:

10   20
 
30   abc   40

Parser:

ifstream in("data.txt");
int a;
while (in >> a) {
    cout << a << " ";
}

H13. Why must this code flush manually?

cout << "Saving..." << '\n';

Printing to a file redirected output.


H14. What does this output? Why?

stringstream ss("12.34");
int x;
ss >> x;
cout << x;

H15. Why does getline immediately return an empty line here?

int n;
cin >> n;
string s;
getline(cin, s);


HARD QUIZ ANSWER KEY WITH PIXAR-STYLE EXPLANATIONS


H1. Output: "Done" — but the buffer still contains “345”

Cin reads:

  • “1”

  • “2”

  • “3”

Stop (int read ends at non-digit)

Backpack contains: “45\n”

ignore(2, '\n') removes only “45”
Newline remains.


H2. Cin reads 3.14 → then fails on “abc”

Second extraction hits “abc”, which panics Failbit.


H3. Must call ss.clear() before reusing

Professor Stringstream still has Failbit panicking
from previous read attempt if it failed or finished.


H4. File contains:

12.346

Because fixed + precision 3 rounds the fractional part.


H5. Prints: 5.000

showpoint forces trailing zeros.
setprecision(3) forces 3 digits after decimal (with fixed behavior implied by whole number).


H6. ios::trunc overrides ios::app

Truncation erases the notebook before writing.


H7. getline read stops at comma, but >> leaves newline

cin >> price reads past whitespace but may mix delimiters improperly.


H8. cin >> x leaves newline in buffer

getline reads only the leftover newline, NOT the next line.


H9. Correct: C — setw

Sir Format throws away the setw box after one use.


H10. Scientific-ish output: 1.23e+04 (with 3 significant digits)

Because without fixed, precision counts significant digits.


H11. cin >> x stops after EOF, loop ends normally

This is actually NOT infinite — correct behavior.
If the student saw infinite looping, the issue is failure to send EOF properly.


H12. Output: 10 20 30

Cin chokes on “abc” and stops; 40 never processed.


H13. '\n' does NOT flush — endl would

When redirecting output, you often want explicit flushing.


H14. Output: 12

Reading float into int truncates at decimal.
Professor Stringstream sees integer part only.


H15. getline returns empty because cin >> n left newline

getline sees the leftover newline and returns immediately.