Advanced Extended Quiz

Here is a much more advanced, extended quiz that mixes:

  • Pixar-style Streamville story elements

  • Real C++ code examples

  • Conceptual reasoning

  • Error-handling challenges

  • File I/O logic

  • Output formatting puzzles

This is designed to prepare you for C++ Fundamentals II while reinforcing everything from Chapter 5.


EXTENDED STREAMVILLE QUIZ (WITH CODE EXAMPLES)

20 advanced questions — answers can be included upon request.


SECTION 1 — Input Behavior (Cin’s Backpack)


1. What will this program output? Why?

int x;
cout << "Enter a number: ";
cin >> x;

cin.ignore(100, '\n');

cout << "Enter another: ";
cin >> x;

User types:

12 34

What gets read for the second input?
A) 34
B) Program waits for new input
C) Nothing; failbit is set
D) Cin panics


2. Why does this code skip the second prompt?

int a, b;
cin >> a >> b;
cout << "Enter c: ";
int c;
cin >> c;

User types:

10 20 30

What happens and why?


3. What will be the result of this code?

int value;
cin >> value;
if (cin.fail())
    cout << "fail\n";
else
    cout << "ok\n";

User enters:

abc

Explain:

  • Which error bit is set?

  • Why does fail occur?

  • What is still sitting in Cin’s backpack?


4. Why does this loop spin infinitely?

int num;
while (!cin.fail()) {
    cin >> num;
    cout << "Number: " << num << endl;
}

User enters:

x

Explain step-by-step what happens in Cin’s brain.



SECTION 2 — Cleaning Up After Cin (clear + ignore)


5. Fill in the missing lines to repair Cin

int age;
cin >> age;

if (cin.fail()) {
    // FIX CIN:
    _________________________;    // A: clear fogged glasses
    _________________________;    // B: sweep junk out
    cout << "Try again: ";
    cin >> age;
}

What should A and B be?


6. Why is cin.ignore(1, '\n') often NOT enough?

Explain using the Streamville analogy:
How big can Cin's backpack be?



SECTION 3 — Output Formatting (Sir Format’s Castle)


7. Predict the formatted output:

cout << setw(6) << 45 << setw(6) << 7;

Where are the spaces? Draw the exact spacing.


8. What will this print? Explain why.

cout << fixed << setprecision(2);
cout << 3.14159 << " ";
cout << setprecision(5);
cout << 3.14159 << endl;

Hint: precision is sticky, but fixed changes how it is interpreted.


9. Which manipulators are “sticky,” and which are one-time?

Categorize these:

  • setw

  • fixed

  • setprecision

  • left

  • right

  • showpoint



SECTION 4 — File Portals (File I/O)


10. What happens to “data.txt” after this code runs?

ofstream out("data.txt");
out << "Hello!";
out.close();

Assume the file already contained 200 lines.

Explain using the Streamville story.


11. Now compare with this:

ofstream out("data.txt", ios::app);
out << "Hello!";
out.close();

How is the portal behavior different?


12. Predict the output of this file-reading code:

File contents:

10
20
thirty
40

Code:

ifstream in("nums.txt");
int x;

while (in >> x) {
    cout << "Read: " << x << endl;
}
cout << "Done.\n";

Which numbers print?
Where does failbit trigger?
Does “40” ever print?


13. Why is this loop WRONG?

while (!in.eof()) {
    in >> x;
    cout << x << endl;
}

Use the story of Eofbit and Failbit both activating together.



SECTION 5 — Delimita’s Table Kingdom (Delimited Files)


14. Why does this fail on CSV input?

CSV file:

101,DuctTape,3.99

Code:

int id;
string name;
double price;

ifstream f("items.csv");
f >> id >> name >> price;

Explain why >> gets confused.
Who can read CSV? (Hint: getline)


15. Fix the code with getline + stringstream

Write C++ code that correctly reads:

102,BailingWire,12.50

And extracts id, name, price.

Use:

  • getline

  • stringstream

  • comma parsing



SECTION 6 — Professor Stringstream’s Lab


16. What does this code print?

string line = "123 apples";
stringstream ss(line);

int a;
string word;

ss >> a >> word;
cout << a << " - " << word << endl;

17. What must you do before reusing ss?

ss.str("456 bananas");
int x;
string y;
ss >> x >> y;    // fails unless ______ happens first

Why does ss remember old mistakes?


18. What does ss.clear() actually do?

Choose the Pixar explanation:

A) Erases the chalkboard
B) Calms Error Trio
C) Sweeps Cin’s backpack
D) Flushes Cout’s backpack


19. Explain str("new line").

Using the Streamville story:
What exactly does Professor Stringstream replace?



SECTION 7 — Mixed Logic (Everything Together)


20. Predict ALL output and describe Cin/Cout/File behavior

ifstream in("data.txt");
ofstream out("result.txt");

int x;
while (in >> x) {
    if (x < 0) {
        cout << "neg ";
        out << "[" << x << "]";
    } else {
        cout << "pos ";
        out << x;
    }
}
cout << endl;

File contains:

10
-3
abc
4

Explain:

  • What prints to console?

  • What goes into result.txt?

  • When does reading stop?

  • Which error bits activate?

  • Does “4” get processed?


FULL ANSWER KEY (STANDARD EXTENDED QUIZ)

(The detailed Pixar-style explanations follow in Part 2.)


SECTION 1 — INPUT BEHAVIOR

1. Answer: B — Program reads 34

Because Cin still has “34” left in his backpack after the first extraction.


2. Answer: It doesn’t skip — all values are consumed from one line

Cin reads:

  • a = 10

  • b = 20

Then c = 30 from leftover buffer.


3. Answer: failbit set; “abc” remains in buffer

Cin panics when trying to turn “abc” into an int.


4. Answer: Infinite loop because failbit stops new reads

Cin never recovers; the loop condition never becomes false.


SECTION 2 — CLEAR + IGNORE

5. Answer:

A: cin.clear();
B: cin.ignore(numeric_limits<streamsize>::max(), '\n');


6. Answer: Cin’s backpack may hold ENTIRE LINES

Using ignore(1, '\n') removes only one character, not the whole leftover mess.


SECTION 3 — FORMATTING

7. Answer:

____45____7
(6 spaces per field; 45 right aligned, then 7 right aligned.)


8. Answer: 3.14 3.14159

  • First printed with fixed precision of 2 → “3.14”

  • Then precision changed to 5 → 5 digits after decimal


9. Sticky vs non-sticky

Sticky:

  • fixed

  • setprecision

  • left

  • right

  • showpoint

NOT sticky:

  • setw (applies only once)


SECTION 4 — FILE I/O

10. Answer: Existing contents erased

ios::out wipes the notebook clean.


11. Answer: Appends to the end instead of erasing

ios::app preserves the file.


12. Answer: Only 10 and 20 print

“thirty” triggers failbit → stops loop → “40” is never read.


13. Answer: EOF loop is wrong because eofbit isn’t set UNTIL after a failed read

The loop executes one extra iteration.


SECTION 5 — DELIMITED FILES

14. Answer: >> can’t split comma-delimited data

It reads “101,DuctTape,3.99” as one giant token.


15. Answer: Correct CSV reader

string line;
getline(f, line);

stringstream ss(line);
string idStr, nameStr, priceStr;

getline(ss, idStr, ',');
getline(ss, nameStr, ',');
getline(ss, priceStr);

int id = stoi(idStr);
double price = stod(priceStr);
string name = nameStr;

SECTION 6 — STRINGSTREAM

16. Answer: prints “123 - apples”


17. Answer: You must call ss.clear() before reusing

Otherwise failbit persists.


18. Answer: B — Calms Error Trio


19. Answer: str() replaces whole chalkboard


SECTION 7 — MIXED LOGIC

20. Answer:

  • Console prints: pos neg

  • File writes: 10[-3]

  • Reading stops at “abc”

  • failbit activates

  • 4 is never processed


PIXAR-STYLE EXPLANATIONS FOR EVERY QUESTION

Below is a one-question = one-story vignette approach.


1. Why does the program read 34?

Cin dumped only the first letter (22) on the Mayor’s desk.
He shoved the rest — “34” — into his backpack.
When asked again, he says:

“No need to ask the humans! I already brought extra mail!”

So 34 pops out instantly.


2. Why doesn’t the program pause?

Cin is efficient.
He sees all three numbers on the same street:

10 20 30

So he hands over:

  • 10 → first request

  • 20 → second request

  • 30 → third request

He never needed to leave town to get new input.


3. Why fail?

Cin tries to read “abc” into an integer.
He squints. Tilt head. Panic.

Failbit runs around screaming:

“This isn’t a number!!”

Cin freezes; nothing more comes out.

The entire “abc” remains in the backpack.


4. Why infinite loop?

Cin is panicked.
Every time the Mayor asks for more mail, Cin says:

"I CAN’T!! Everything is ruined!!"

But the Mayor keeps asking:

“Are you failing?”
“Well… yes…”
“Okay, then keep going!”

Deadlock.


5. Why clear + ignore?

Doctor Clear wipes Cin’s glasses → resets failbit.
Broom Ignore dumps the garbage in Cin’s backpack.

Only then is Cin ready to try again.


6. Why ignore(1) isn’t enough?

Cin’s backpack can hold an ENTIRE paragraph of leftover input.
ignore(1) is like sweeping away one crumb.
But the table might still have a plate, napkin, and spilled soup.


7. setw formatting

Sir Format builds two boxes:

[____45][____7]

Right-aligned because that’s default.


8. fixed + setprecision

Sir Format says:

  1. “Show exactly 2 jewels after the decimal.” → 3.14

  2. Then: “Now show 5 jewels.” → 3.14159


9. Sticky manipulators

Sir Format keeps wearing his styling rules:

  • fixed, setprecision, left, right, showpoint

But he throws away setw() boxes after one use.


10. ios::out erases files

Opening with ios::out is like FileStream saying:

“This old notebook? Rip out all pages!
Time to start fresh!”


11. ios::app appends

ios::app is like:

“Keep the old notebook — just write at the bottom!”


12. File reading stops after “thirty”

Cin tries to convert “thirty” to an int.
Failbit faceplants.
Cin refuses to read further lines, including “40.”


13. eof() loop is flawed

Cin doesn’t know he has reached the end of the book
until he tries to read past it — and fails.

Only after failing does Eofbit fall asleep.


14. Why >> fails for CSV

Cin reads words separated by spaces.
When he sees:

101,DuctTape,3.99

He says:

“This is one giant word!!!”

He doesn’t know what commas are.


15. getline + stringstream

getline reads the entire scroll.
Professor Stringstream cuts it apart at every fence (comma):

  • “102”

  • “BailingWire”

  • “12.50”


16. “123 apples”

Professor Stringstream is chill:

  • First item: 123

  • Second item: apples

He’s good at parsing orderly lines.


17. Why ss.clear()?

If Professor Stringstream failed previously,
Failbit would still be screaming in his classroom.
He needs calm (clear) before starting the next line.


18. What does clear do?

Doctor Clear calms the Error Trio down.
They stop panicking, and parsing resumes.


19. What does str() do?

He erases the chalkboard entirely
and writes new text on it.


20. Mixed logic

Cin reads:

  • 10 → pos

  • -3 → neg

Next word: “abc” → panic → stops.

Cout prints:

pos neg

File gets:

10[-3]

Reading stops before 4.