In this final step, we'll create a program that demonstrates various input and output operations in C++. Understanding how to interact with users through input and output is crucial for creating interactive programs.
- In the WebIDE, create a new file named
io_operations.cpp
.
- Open
io_operations.cpp
in the editor and type the following code:
#include <iostream>
#include <iomanip> // For setprecision
#include <string> // For string operations
using namespace std;
int main() {
string name;
int age;
double height;
// Input operations
cout << "Enter your name: ";
getline(cin, name); // Reads a full line, including spaces
cout << "Enter your age: ";
cin >> age;
cout << "Enter your height in meters: ";
cin >> height;
// Output operations
cout << "\n--- Your Information ---\n";
cout << "Name: " << name << endl;
cout << "Age: " << age << " years" << endl;
// Setting precision for floating-point output
cout << fixed << setprecision(2);
cout << "Height: " << height << " meters" << endl;
// Using width and fill for formatted output
cout << "\nFormatted Age Display:\n";
cout << setw(10) << setfill('.') << left << "Age" << setw(5) << right << age << endl;
return 0;
}
Let's break down the key components of this program:
-
Additional Headers:
#include <iomanip>
: This header provides input/output manipulators like setprecision
and setw
.
#include <string>
: This header is for string operations, although it's often implicitly included with <iostream>
.
-
Input Operations:
getline(cin, name)
: This reads a full line of input, including spaces. It's useful for reading names or sentences.
cin >> age
: This reads a single value into the age
variable. The >>
operator is used for input.
-
Output Operations:
cout << "Text" << variable << endl
: This is the basic output operation. The <<
operator is used for output.
endl
: This adds a newline and flushes the output buffer.
-
Formatting Output:
cout << fixed << setprecision(2)
: This sets the output of floating-point numbers to always show 2 decimal places.
setw(10)
: This sets the width of the next output field to 10 characters.
setfill('.')
: This sets the fill character to '.' for any empty spaces in a field.
left
and right
: These set the alignment of the output within its field.
To compile and run the program:
-
In the terminal panel of your WebIDE, make sure you're in the correct directory.
-
Compile the program with:
g++ -o io_operations io_operations.cpp
-
Run the program with:
./io_operations
When you run the program, it will prompt you to enter your name, age, and height. After entering this information, the program will display it back to you in a formatted manner.
Common issues to watch out for:
- If your program skips the name input, it's likely due to a newline character left in the input buffer. To fix this, you can add
cin.ignore();
before getline(cin, name);
.
- Make sure to enter a valid number for age and height. Entering non-numeric values can cause unexpected behavior.
This program demonstrates how to handle different types of input, including strings with spaces, and how to format output for better readability. These skills are essential for creating user-friendly, interactive C++ programs.