WikiGalaxy

Personalize

C++ Basics

Introduction to

The library in C++ is part of the standard library and is used for input and output operations. It includes functionalities to read data from standard input (keyboard) and write data to standard output (screen).


#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!";
    return 0;
}
        

Console Output:

Hello, World!

Using cin for Input

Reading User Input with cin

The cin object is used to accept input from the user. It reads data from the standard input stream.


#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "You are " << age << " years old.";
    return 0;
}
        

Console Output:

Enter your age: 25

You are 25 years old.

Using endl for New Line

Creating New Lines with endl

The endl manipulator is used to insert a newline character and flush the output buffer.


#include <iostream>
using namespace std;

int main() {
    cout << "Hello," << endl;
    cout << "World!";
    return 0;
}
        

Console Output:

Hello,

World!

Formatted Output with setw

Aligning Output with setw

The setw manipulator is used to set the width of the output field.


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << setw(10) << "Hello" << setw(10) << "World" << endl;
    return 0;
}
        

Console Output:

Hello World

Using getline for String Input

Reading Full Lines with getline

The getline function is used to read a line of text, including spaces, from an input stream.


#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "Enter your full name: ";
    getline(cin, name);
    cout << "Hello, " << name << "!";
    return 0;
}
        

Console Output:

Enter your full name: John Doe

Hello, John Doe!

Using cerr for Error Messages

Outputting Errors with cerr

The cerr object is used for outputting error messages. It is unbuffered and outputs immediately.


#include <iostream>
using namespace std;

int main() {
    cerr << "Error: Something went wrong!" << endl;
    return 0;
}
        

Console Output:

Error: Something went wrong!

Using clog for Logging

Logging Information with clog

The clog object is used for logging information. It is buffered, meaning it might not output immediately.


#include <iostream>
using namespace std;

int main() {
    clog << "Log: Program started successfully." << endl;
    return 0;
}
        

Console Output:

Log: Program started successfully.

Chaining Input and Output

Combining Multiple Operations

In C++, you can chain multiple input and output operations using the << and >> operators.


#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "Sum: " << a + b << endl;
    return 0;
}
        

Console Output:

Enter two numbers: 3 5

Sum: 8

Manipulating Output with setprecision

Setting Decimal Precision

The setprecision manipulator is used to control the number of digits displayed after the decimal point for floating-point numbers.


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double pi = 3.14159;
    cout << setprecision(3) << pi << endl;
    return 0;
}
        

Console Output:

3.14

Using ios::fixed for Fixed Point Notation

Displaying Fixed Point Numbers

The ios::fixed is used to display floating-point numbers in fixed-point notation.


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double num = 123.456;
    cout << fixed << setprecision(2) << num << endl;
    return 0;
}
        

Console Output:

123.46

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025