Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Standard Input and Output in C++

Introduction

Standard input and output are fundamental concepts in C++ programming, used for reading data from the user and displaying data to the user. This tutorial will cover the basics of using standard input and output in C++ with detailed explanations and examples.

Basic Output with cout

In C++, the cout object, defined in the iostream header file, is used to output data to the standard output stream, typically the console.

#include <iostream>
using namespace std;
int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

This code will output:

Hello, World!

Basic Input with cin

The cin object, also defined in the iostream header file, is used to read input from the standard input stream, typically the keyboard.

#include <iostream>
using namespace std;
int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "You are " << age << " years old." << endl;
    return 0;
}

When you run this code, it will prompt you to enter your age:

Enter your age: 25
You are 25 years old.

Combining cin and cout

You can combine cin and cout to create interactive programs. Here's an example where we read multiple inputs:

#include <iostream>
using namespace std;
int main() {
    string name;
    int age;
    cout << "Enter your name: ";
    cin >> name;
    cout << "Enter your age: ";
    cin >> age;
    cout << "Hello, " << name << ". You are " << age << " years old." << endl;
    return 0;
}

When you run this code, it will prompt for your name and age:

Enter your name: Alice
Enter your age: 30
Hello, Alice. You are 30 years old.

Formatting Output

C++ provides several ways to format output using manipulators like endl, setw, and setprecision from the iomanip header file.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
    double pi = 3.14159;
    cout << "Default: " << pi << endl;
    cout << "Fixed: " << fixed << pi << endl;
    cout << "Set precision: " << setprecision(2) << pi << endl;
    return 0;
}

This code will produce the following output:

Default: 3.14159
Fixed: 3.141590
Set precision: 3.14

Error Handling with cin

Handling invalid input is crucial for robust programs. If the user enters invalid input, cin sets an error flag which you can check using cin.fail().

#include <iostream>
using namespace std;
int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;
    if (cin.fail()) {
        cout << "Invalid input!" << endl;
    } else {
        cout << "You entered: " << number << endl;
    }
    return 0;
}

This code will output an error message if the input is not a valid integer:

Enter a number: abc
Invalid input!

Conclusion

Understanding standard input and output is essential for C++ programming. This tutorial covered the basics of using cout for output, cin for input, formatting output, and handling input errors. Practice these concepts to become proficient in handling input and output in your C++ programs.