Mastering Module 4
Output & String Handlings
Video Overview
Master Output and String Handling in C++! Learn to display text using the iostream library and cout. We cover String Literals, concatenating data with the insertion operator (<<), and formatting layouts using endl.
🎉 You're almost at the finish line! Want the full notes?
Download PDF NotesNotes
1. Visualizing Input vs Output
Confused by << and >>? Just follow the direction of the arrows!
-
Output (Push)
Arrows point towards the output. Pushing data out to the screen. -
Input (Pull)
Arrows point towards the variable. Pulling data in from the keyboard.
2. Output & Escape Sequences
The cout Object
Used with the insertion operator << to display variables and strings.
Formatting with Escape Sequences
Special characters that control the cursor layout:
- \n Newline: Cursor moves to the next line.
- \t Tab: Cursor moves to the next tab stop.
- \a Alert: Triggers a system beep.
- \" Double Quote: Displays
".
Try it Yourself
Console Output
3. Built-in (Library) Functions
Math Functions
For complex calculations beyond + or -.
pow(2, 3)→ 8.0sqrt(16)→ 4.0abs(-5)→ 5
String Manipulation
Handling text effectively.
str.length(): Size of stringstr.substr(p, n): Extract partstr.find('x'): Search index
Input/Output Manipulation
Required for setw, setprecision, and fixed.
4. Formatted Output
Precision
Sets the number of decimal places to display. Best used with fixed.
Field Width
Spaces the next output into a field of n characters. Good for tables.
Force Decimals
Forces the decimal point and trailing zeros to be shown on the screen.
Adjust Column Widths
Change the setw(n) values to see how the alignment shifts:
cout << fixed << setprecision(2);
cout << setw(10) << "Price:" << setw(8) << 19.90 << endl;
cout << setw(10) << "Tax:" << setw(8) << 1.20 << endl;
Console Output
Price: 19.90
Tax: 1.20
Pro Tip: If the setw number is smaller than the word (e.g., setw(2) for "Price"), C++ will ignore the width and print the whole word anyway!
Advanced Formatting: setfill
While setw creates empty padding, setfill fills that padding with a specific character.
Syntax:
cout << setfill('.') << setw(20) << "Chapter 1";
...........Chapter 1
Why use it? It's essential for creating readable tables of contents, receipts, or aligning financial data.
Receipt Printer Simulator
cout << left << setfill('.') << setw(25) << "Coffee";
cout << "$4.50";
5. String Handling & Math Functions
String Functions <cstring>
Manage character arrays and strings safely:
- strlen(str): Returns the number of characters.
- strncpy(dest, src, n): Copies n characters to prevent buffer overflow.
- strcmp(s1, s2): Compares strings; returns 0 if they match.
Practical Code Snippet
char szDest[50];
// Safely copy 49 chars (leaving room for null)
strncpy(szDest, szSource, 49);
szDest[49] = 0;
cout << "Length: " << strlen(szDest);
Length: 10
Specific String Class Functions
Useful Members
-
Concatenation (+)
Joins strings together.
string s = "Hello" + s2; -
getline(cin, var)
Reads a full line of text (including spaces).
VScin >> varwhich stops at the first space! -
Modifiers
str.erase(pos, len): Deletes chars.
str.insert(pos, "text"): Adds chars at a position.
The "Space" Problem
Try typing "John Smith" below and see the difference.
Waiting...
Module 4 Knowledge Check
Answer the following multiple-choice questions to test your understanding.