Lesson 9
Basic I/O
Covers cin, cout, endl, getline, and common mistakes.
Lesson content
What is basic I/O in C++?
I/O exchanges info between program and user.
Input receives data. Output shows data.
- cin: takes input
- cout: shows output
Both live inside iostream header.
Like a restaurant. Customer orders, waiter delivers food.
User → cin → Program → cout → UserWhy does basic I/O exist?
Without I/O, programs only use fixed values.
Age = 20
// vs
Enter your age:I/O makes software interactive and useful.
Real-world use cases
- Login systems
- Student management systems
- Banking applications
- Calculator programs
- Games
- Online forms
- Inventory systems
The iostream header
#include <iostream>Header holds standard input/output stream objects.
What is cout?
cout means Character Output. Prints data to screen.
cout << value;
cout << "Hello";
// Output: Hello<< is the stream insertion operator.
What is cin?
cin means Character Input. Reads keyboard input.
cin >> variable;
cin >> age;>> is the stream extraction operator.
Multiple outputs
cout << "Age: " << age;Multiple inputs
cin >> a >> b;
// Input: 10 20Using endl
endl moves to next line. Flushes the buffer.
cout << "Hello" << endl;
cout << "World";
// Output:
// Hello
// WorldUsing \n
Another way to add a newline.
cout << "Hello\n";Unlike endl, \n skips flushing. Runs faster.
cin stops at whitespace
Important rule to remember.
// Input: John Smith
cin >> name;
// Result: JohnUse getline to read a full line.
getline(cin, name);Example 1: Display a message
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}Example 2: Take user input
#include <iostream>
using namespace std;
int main()
{
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is " << age;
return 0;
}Example 3: Add two numbers
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
int sum = num1 + num2;
cout << "Sum = " << sum;
return 0;
}Example 4: Read full name
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter your full name: ";
getline(cin, name);
cout << "Welcome, " << name;
return 0;
}Example 5: Student information
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int age;
float marks;
cout << "Enter name: ";
getline(cin, name);
cout << "Enter age: ";
cin >> age;
cout << "Enter marks: ";
cin >> marks;
cout << "\nStudent Details\n";
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Marks: " << marks << endl;
return 0;
}Common mistakes and pitfalls
- cout >> x wrong. Fix: cout << x
- cin << x wrong. Fix: cin >> x
- Missing iostream. Fix: include it
- cin for full names. Fix: use getline
- Missing std:: prefix. Fix: add std::cout
// Wrong
string name;
cin >> name;
// Input: John Smith
// Output: John// Correct
string name;
getline(cin, name);
// Output: John SmithBest practices
- Prefer \n over endl usually
- Write clear prompts before input
- Validate user input in real apps
- Use getline for names with spaces
- Avoid using namespace std in big projects
When not to use this
- Building graphical desktop apps
- Developing web applications
- Reading large files, use file streams
- High-performance logging systems
- Embedded systems, custom hardware I/O
Summary
- Basic I/O lets programs interact with users
- cout displays output
- cin receives input
- iostream header enables both
- << inserts, >> extracts data
- getline reads full lines with spaces
- \n is faster than endl
- Clear prompts improve user experience
Frequently asked questions
What does cin mean in C++?
Means Character Input. Reads keyboard data.
What does cout mean in C++?
Means Character Output. Shows text on console.
What is the difference between endl and \n?
Both add new lines. endl also flushes buffer.
Why does cin stop at spaces?
>> treats whitespace as separator. Use getline for lines.
Why do I need #include <iostream>?
Defines cin, cout, cerr, clog objects.