What You’ll Learn
By the end of this post, you will:
- Use
cinandcoutfor console input/output - Format output using manipulators
- Read from and write to text files
- Understand how to use simple graphics libraries (intro only)
Console Input and Output
C++ uses cin and cout from the <iostream> library for standard input and output.
Example:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "You entered: " << age << endl;
return 0;
}
How it works:
coutdisplays messages or data to the terminalcinreads input from the user>>is the extraction operator (used withcin)<<is the insertion operator (used withcout)
Inputting Multiple Values
You can chain cin to read multiple inputs:
int a, b;
cin >> a >> b;
If the user types 5 10, a becomes 5 and b becomes 10.
Formatting Output
C++ provides the <iomanip> library to format output.
Common manipulators:
| Manipulator | Use |
|---|---|
setw(n) | Sets width of output |
setprecision(n) | Controls decimal places |
fixed | Uses fixed-point notation |
left, right | Aligns text |
Example:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159265;
cout << fixed << setprecision(2) << pi << endl; // Output: 3.14
return 0;
}
Reading and Writing Files
To use file I/O in C++, include the <fstream> library.
Step 1: Include the header
#include <fstream>
Step 2: Declare file streams
- Use
ofstreamfor writing to files - Use
ifstreamfor reading from files
Writing to a File
#include <fstream>
using namespace std;
int main() {
ofstream outFile("output.txt");
if (outFile.is_open()) {
outFile << "Hello, file!" << endl;
outFile << "This is written using C++." << endl;
outFile.close();
}
return 0;
}
This will create a file named output.txt and write two lines to it.
Reading from a File
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ifstream inFile("output.txt");
string line;
if (inFile.is_open()) {
while (getline(inFile, line)) {
cout << line << endl;
}
inFile.close();
}
return 0;
}
This program reads output.txt line by line and prints it to the console.
File Check and Error Handling
Always check whether a file is open before using it.
ifstream file("data.txt");
if (!file) {
cout << "Error opening file." << endl;
}
Simple Graphics in C++ (Intro)
C++ itself doesn’t have built-in graphics, but you can use external libraries like:
- SFML
- SDL
- graphics.h (older, Turbo C++ style)
Here’s a simple program using graphics.h (only works in some environments like Code::Blocks with setup):
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
circle(200, 200, 100); // draw circle at x=200, y=200, radius=100
getch();
closegraph();
return 0;
}
This displays a simple window with a circle. Graphics programming requires proper setup, which we’ll cover later in a dedicated post.
Quiz: Input & Output
Mini Exercises
- Ask the user to enter their name and age, and print a message like:
Hello Sarah, you are 21 years old. - Create a program that writes 5 numbers to a file, one per line.
- Write a program to read numbers from a file and print their sum.
Common Mistakes to Avoid
- Forgetting to include headers like
<fstream>or<iomanip> - Not checking if a file was successfully opened
- Missing
cin.ignore()when mixingcinandgetline() - Trying to use graphics without setting up the required environment
Summary
In this post, you learned:
- How to use
cinandcoutfor user interaction - How to format output with manipulators
- How to perform file input/output in C++
- Basic concepts of using graphics in C++
What’s Next?
In the next post, we’ll explore Functions in C++ — including parameters, return values, and how to structure your code using reusable logic blocks.
