📖 Scenario: You are creating a simple program to display a person's name and age in a nicely formatted way. This is like writing a friendly introduction card.
🎯 Goal: Build a C++ program that stores a person's name and age, then prints a formatted sentence introducing the person.
📋 What You'll Learn
Create variables to store a person's name and age
Use a helper variable to store a greeting message
Combine the variables into a formatted sentence using std::cout
Print the final formatted sentence
💡 Why This Matters
🌍 Real World
Formatting text output is useful for creating user-friendly messages in console programs, reports, and logs.
💼 Career
Basic formatting skills are essential for software developers to display information clearly and professionally.
Progress0 / 4 steps
1
Create variables for name and age
Create a std::string variable called name and set it to "Alice". Create an int variable called age and set it to 30.
C++
Hint
Use std::string name = "Alice"; and int age = 30; inside main().
2
Add a greeting message variable
Create a std::string variable called greeting and set it to "Hello".
C++
Hint
Use std::string greeting = "Hello"; inside main().
3
Combine variables into a formatted sentence
Use std::cout to print the sentence combining greeting, name, and age in this exact format: Hello, my name is Alice and I am 30 years old.
C++
Hint
Use std::cout << greeting << ", my name is " << name << " and I am " << age << " years old." << std::endl;
4
Print the final formatted sentence
Run the program and ensure it prints exactly: Hello, my name is Alice and I am 30 years old.
C++
Hint
Run the program and check the output matches exactly.
Practice
(1/5)
1. What does the setw manipulator do in C++ output formatting?
easy
A. Sets the number of decimal places for floating numbers
B. Sets the width of the next output field
C. Changes the text color in the console
D. Aligns text to the center automatically
Solution
Step 1: Understand the purpose of setw
The setw manipulator sets the width of the next output field in characters.
Step 2: Compare with other manipulators
setprecision controls decimal places, not width. Color and alignment are not handled by setw.
Final Answer:
Sets the width of the next output field -> Option B
setw(5) sets the width to 5 characters, padding with spaces on the left by default.
Step 2: Apply setw to numbers
Number 42 is printed right-aligned in 5 spaces: 3 spaces + '42'. Number 1234 is 4 digits, so 1 space + '1234'.
Final Answer:
42
1234 -> Option A
Quick Check:
setw pads left with spaces [OK]
Hint: setw pads on left by default for numbers [OK]
Common Mistakes:
Assuming setw pads on the right
Ignoring spaces added by setw
Thinking setw truncates numbers
4. Identify the error in this code snippet:
#include <iostream>
using namespace std;
int main() {
cout << setw(10) << 3.14159 << endl;
return 0;
}
medium
A. No error, code runs fine
B. Using cout without std:: prefix
C. Incorrect use of endl
D. Missing <iomanip> header for setw
Solution
Step 1: Check header includes
The code uses setw but does not include <iomanip>, which is required.
Step 2: Verify other parts
cout is used with using namespace std;, so no prefix needed. endl usage is correct.
Final Answer:
Missing <iomanip> header for setw -> Option D
Quick Check:
setw needs iomanip header [OK]
Hint: Always include <iomanip> when using setw [OK]
Common Mistakes:
Forgetting to include <iomanip>
Thinking cout needs std:: with using namespace std;
Assuming endl usage causes error
5. How would you format the output of a floating-point number to always show exactly 3 decimal places and a total width of 8 characters, right-aligned?
hard
A. cout << fixed << setprecision(3) << setw(8) << number;
B. cout << setprecision(3) << setw(8) << number;
C. cout << setw(8) << fixed << number << setprecision(3);
D. cout << fixed << setw(3) << setprecision(8) << number;
Solution
Step 1: Set fixed decimal places
Use fixed and setprecision(3) to show 3 decimals exactly.
Step 2: Set total width and alignment
Use setw(8) to set total width to 8 characters, right-aligned by default.