User Input and Output in C++
# CHAPTER 6
User Input and Output
1. Introduction
Real applications interact with users. They ask for names, ages, and configurations. In C++, we handle this interaction using standard input (keyboard) and standard output (screen) objects provided by the<iostream> library.
2. Learning Objectives
-
Use
coutto output data.
-
Use
cinto read user input.
-
Use
getline()to read strings with spaces.
- Format output safely.
- Address common input buffer issues.
3. Output with cout
The std::cout object, coupled with the insertion operator <<, sends data to the console.
4. Input with cin
The std::cin object, coupled with the extraction operator >>, reads formatted input from the keyboard.
*Note: Unlike C's scanf, you do NOT need the & operator when using cin.*
5. Reading Strings (The cin vs getline() problem)
When you use cin >> to read a string, it stops reading as soon as it hits a whitespace (space, tab, or newline).
The Solution: getline()
To read a full line of text including spaces, use getline(cin, variable).
6. The Input Buffer Issue (cin.ignore())
If you use cin >> to read an integer, and then use getline() to read a string immediately after, the program will skip the getline().
Why? When you type 25 and hit ENTER, the buffer stores 25\n. cin >> reads the 25 but leaves the \n in the buffer. getline() sees the \n and thinks you pressed ENTER on an empty string!
Fix: Use cin.ignore() to clear the buffer.
7. Mini Project: Student Profile Generator
8. Memory-Level Explanation
When you usecin, the program pauses and waits for the OS to send keyboard data via the stdin stream. The extraction operator >> parses the characters, converts them to binary based on the variable's data type, and writes them directly to that variable's memory address.
9. Common Mistakes
-
Wrong arrows:
cin << age;is illegal. The arrows point towards where the data is going. Fromcin*into*age(cin >> age).
-
Skipping
cin.ignore(): Causes the program to skip string inputs if they follow an integer or float input.
10. Exercises
-
1.
Write a program that asks for a temperature in Celsius and prints it in Fahrenheit.
(F = C * 9/5 + 32).
- 2. Write a program that asks for the user's favorite quote (including spaces) and prints it with quotation marks around it.
11. MCQ Quiz with Answers
Which object is used for reading user input?
Which operator is used with cin?
Which library must be included to use cin and cout?
What is the issue with using cin >> for strings?
Which function reads a full string with spaces?
Why is cin.ignore() used?
What does endl do?
Standard input in C++ usually comes from:
Which data types can cin >> handle?
What happens if a user types a letter when cin >> intVar expects a number?
12. Interview Questions
-
Q: Explain why
cin >> strstops at a space, butgetline()does not.
-
Q: How do you check if
cinfailed (e.g., user entered a string instead of an integer) and how do you recover? (Hint:cin.fail(),cin.clear()).
13. Summary
Interactivity in C++ is handled via<iostream>. cout outputs data using the insertion operator <<. cin reads data using the extraction operator >>. For full sentences, getline() is required, and buffer management (cin.ignore()) prevents input skipping.
14. Next Chapter Recommendation
Now that our programs can accept input, we need them to make decisions. In Chapter 7: Conditional Statements, we will learn how to useif, else, and switch.