Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C++ Manipulators

    In C++, Manipulators are predefined functions that help in formatting output. The output data is manipulated on display according to the programmer's choice. This tutorial will teach you about the various commonly used C++ manipulators.

    Here are some commonly used C++ Manipulators:

    endl Manipulator

    "endl" is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed the entire line and then point the cursor to the beginning of the next line. You can also use "\n" (escape sequence) instead of "endl" for the same purpose.

    setw Manipulator

    This Manipulator sets the minimum field width on output.

    Syntax:

    setw(x)

    Example:

    /* 
    * File: main.cpp
    * Author: Gautam
    * Created on October 16, 2011, 12:58 PM
    */
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() {
    
        float basic, ta,da,gs;
        basic=10000; ta=800; da=5000;
        gs=basic+ta+da;
        cout<<setw(10)<<"Basic"<<setw(10)<<basic<<endl
            <<setw(10)<<"TA"<<setw(10)<<ta<<endl
            <<setw(10)<<"DA"<<setw(10)<<da<<endl
            <<setw(10)<<"GS"<<setw(10)<<gs<<endl;
        return 0;
    }

    Program Output:

    cplusplus-manipulators

    setfill Manipulator

    The "setw" manipulator uses this Manipulator. If a value does not fill a field, then the character specified in the "setfill" argument of the Manipulator is used for filling the fields.

    Example:

    /* 
    * File: main.cpp
    * Author: Gautam
    * Created on October 16, 2011, 12:58 PM
    */
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() {
        cout << setw(20) << setfill('*') << "w3schools.in" << setw(20) << setfill('*') << "Test" << endl;
    }

    Program Output:

    cplusplus-setfill-manipulators



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.