Manipulators in C++ – endl, setw(), setfill(), setprecision(), fixed and setiosflags() (C++ में मैनिपुलेटर्स)

In C++, manipulators are special functions or objects used with input/output stream operators to control the formatting of input and output. They help programmers display data in a well-organized and readable form.

C++ में Manipulators विशेष Functions या Objects होते हैं, जिनका उपयोग Input/Output Stream के साथ Data की Formatting को नियंत्रित करने के लिए किया जाता है। इनकी सहायता से Data को व्यवस्थित और आसानी से पढ़े जाने योग्य रूप में प्रदर्शित किया जा सकता है।

Some important C++ manipulators are endl, setw(), setfill(), setprecision(), fixed and setiosflags().

C++ के कुछ महत्वपूर्ण Manipulators endl, setw(), setfill(), setprecision(), fixed और setiosflags() हैं।

1. endl Manipulator

The endl manipulator inserts a newline into the output stream and flushes the stream. It moves subsequent output to the next line. It is often compared with \n, but unlike \n, endl also flushes the output stream.

endl Manipulator Output में नई Line जोड़ता है और Output Stream को Flush भी करता है। इसके बाद प्रदर्शित होने वाला Data अगली Line में आता है। इसकी तुलना \n से की जाती है, लेकिन \n के विपरीत endl Output Stream को Flush भी करता है।

Example: Simple Interest Program

#include <iostream>
using namespace std;
int main()
{
float p, r, t, si;
cout << "Enter principal, rate and time:" << endl;
cin >> p >> r >> t;
si = (p * r * t) / 100;
cout << "Simple Interest = " << si << endl;
return 0;
}

Sample Output:

Enter principal, rate and time:
1000 5 2
Simple Interest = 100

Formula (सूत्र):

[
SI=\frac{P\times R\times T}{100}
]

Here, p represents principal, r represents the annual rate of interest, and t represents time in years.

यहाँ p मूलधन, r वार्षिक ब्याज दर और t समय को वर्षों में दर्शाता है।

2. setw(int w) Manipulator

The setw() manipulator sets the minimum field width for the next formatted output item. It is available through the iomanip header file. If the value occupies fewer characters than the specified width, additional spaces are used by default.

setw() Manipulator अगली Output Value के लिए Minimum Field Width निर्धारित करता है। यह iomanip Header File में उपलब्ध होता है। यदि Value निर्धारित Width से छोटी है, तो Default रूप से अतिरिक्त रिक्त स्थान जोड़े जाते हैं।

Syntax:

setw(width);

Example: Counting from 1 to 100

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
cout << "Counting from 1 to 100" << endl;
for (i = 1; i <= 100; i++)
{
cout << setw(3) << i << " ";
if (i % 10 == 0)
cout << endl;
}
return 0;
}

Sample Output (first two lines):

Counting from 1 to 100
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20

Explanation: setw(3) reserves a minimum width of three characters for each number. The width setting applies only to the next formatted output item.

व्याख्या: setw(3) प्रत्येक संख्या के लिए कम से कम तीन Characters की Width निर्धारित करता है। यह Width केवल अगली Output Value पर लागू होती है।

3. setfill(char ch) Manipulator

The setfill() manipulator specifies the character used to fill the unused portion of a field when a field width has been set. It is commonly used with setw(). The fill character remains active until it is changed.

setfill() Manipulator Field की खाली जगह को भरने के लिए उपयोग किए जाने वाले Character को निर्धारित करता है। इसका उपयोग सामान्यतः setw() के साथ किया जाता है। निर्धारित किया गया Fill Character तब तक लागू रहता है, जब तक उसे बदला नहीं जाता।

Syntax:

setfill(character);

Example: Counting from 1 to 20 Using Asterisks

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i;
cout << "Counting from 1 to 20" << endl;
for (i = 1; i <= 20; i++)
{
cout << setfill('*') << setw(3) << i << " ";
if (i % 10 == 0)
cout << endl;
}
return 0;
}

Sample Output:

Counting from 1 to 20
**1 **2 **3 **4 **5 **6 **7 **8 **9 *10
*11 *12 *13 *14 *15 *16 *17 *18 *19 *20

Explanation: setfill('*') specifies the asterisk (*) as the fill character, while setw(3) sets the minimum field width to three characters. Since numbers are right-aligned by default, the asterisks appear before the numbers.

व्याख्या: setfill('*') तारांकन चिह्न (*) को Fill Character बनाता है, जबकि setw(3) Minimum Field Width तीन Characters निर्धारित करता है। Default रूप से Numbers दाईं ओर Align होते हैं, इसलिए तारांकन चिह्न संख्या से पहले दिखाई देते हैं।

4. setprecision(int n) Manipulator

The setprecision() manipulator controls the precision of floating-point output. By default, it specifies the total number of significant digits. When used with fixed, it specifies the number of digits after the decimal point.

setprecision() Manipulator Floating-Point Output की Precision को नियंत्रित करता है। Default रूप से यह Significant Digits की कुल संख्या निर्धारित करता है। fixed के साथ उपयोग करने पर यह Decimal Point के बाद के Digits की संख्या निर्धारित करता है।

Syntax:

setprecision(n);

Example: Displaying the Value of Pi

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double pi = 3.14159;
cout << setprecision(5) << pi << endl;
cout << setprecision(2) << pi << endl;
cout << fixed;
cout << setprecision(2) << pi << endl;
return 0;
}

Sample Output:

3.1416
3.1
3.14

Explanation:

  • setprecision(5) displays five significant digits by default.

  • setprecision(2) displays two significant digits by default.

  • fixed << setprecision(2) displays two digits after the decimal point.

व्याख्या:

  • setprecision(5) Default रूप से पाँच Significant Digits प्रदर्शित करता है।

  • setprecision(2) Default रूप से दो Significant Digits प्रदर्शित करता है।

  • fixed << setprecision(2) Decimal Point के बाद दो Digits प्रदर्शित करता है।

5. fixed Manipulator

The fixed manipulator displays floating-point numbers in fixed-point notation. When it is used with setprecision(n), the value of n represents the number of digits displayed after the decimal point.

fixed Manipulator Floating-Point Numbers को Fixed-Point Notation में प्रदर्शित करता है। जब इसका उपयोग setprecision(n) के साथ किया जाता है, तब n Decimal Point के बाद प्रदर्शित होने वाले Digits की संख्या निर्धारित करता है।

Syntax:

fixed;

Example: Using fixed Manipulator

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a = 12.34567;
double b = 45.6;
cout << fixed;
cout << setprecision(2);
cout << "Value of a = " << a << endl;
cout << "Value of b = " << b << endl;
return 0;
}

Output:

Value of a = 12.35
Value of b = 45.60

Explanation: The fixed manipulator displays floating-point numbers with a fixed number of digits after the decimal point. Here, setprecision(2) displays exactly two digits after the decimal point.

व्याख्या: fixed Manipulator Floating-Point Numbers को Decimal Point के बाद निश्चित संख्या में Digits के साथ प्रदर्शित करता है। यहाँ setprecision(2) Decimal Point के बाद ठीक दो Digits प्रदर्शित करता है।

Important Point: The fixed formatting setting remains active until it is changed using another formatting manipulator, such as defaultfloat.

महत्वपूर्ण बिंदु: fixed Formatting तब तक लागू रहती है, जब तक इसे defaultfloat जैसे किसी अन्य Formatting Manipulator की सहायता से बदला नहीं जाता।

6. setiosflags(long flags) Manipulator

The setiosflags() manipulator sets the specified formatting flags of an input/output stream. These flags control how data is displayed, such as showing a positive sign, displaying a decimal point, or aligning output.

It is available through the iomanip header file.

setiosflags() Manipulator Input/Output Stream के निर्धारित Formatting Flags को Set करता है। ये Flags यह नियंत्रित करते हैं कि Data किस प्रकार प्रदर्शित होगा, जैसे Positive Sign दिखाना, Decimal Point प्रदर्शित करना या Output को Align करना।

यह iomanip Header File के माध्यम से उपलब्ध होता है।

Syntax:

setiosflags(flags);

Some commonly used formatting flags are:

कुछ सामान्य रूप से उपयोग किए जाने वाले Formatting Flags निम्नलिखित हैं:

FlagPurpose (उपयोग)
ios::showposDisplays a plus sign before positive numbers. Positive Numbers के पहले Plus Sign दिखाता है।
ios::showpointDisplays a decimal point and trailing zeros where appropriate. आवश्यकता के अनुसार Decimal Point और अतिरिक्त Zeros दिखाता है।
ios::leftAligns output to the left. Output को बाईं ओर Align करता है।
ios::rightAligns output to the right. Output को दाईं ओर Align करता है।
ios::fixedDisplays floating-point values in fixed-point notation. Floating-Point Values को Fixed-Point Notation में दिखाता है।

Example: Using setiosflags()

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a = 25.5;
double b = 10.0;
cout << fixed << setprecision(2);
cout << setiosflags(ios::showpos | ios::showpoint);
cout << "Value of a = " << a << endl;
cout << "Value of b = " << b << endl;
return 0;
}

Output:

Value of a = +25.50
Value of b = +10.00

Explanation:

  • ios::showpos displays a plus (+) sign before positive numbers.

  • ios::showpoint ensures that the decimal point is displayed.

  • fixed displays the values in fixed-point notation.

  • setprecision(2) displays two digits after the decimal point.

व्याख्या:

  • ios::showpos Positive Numbers के पहले Plus (+) Sign प्रदर्शित करता है।

  • ios::showpoint Decimal Point को प्रदर्शित करना सुनिश्चित करता है।

  • fixed Values को Fixed-Point Notation में प्रदर्शित करता है।

  • setprecision(2) Decimal Point के बाद दो Digits प्रदर्शित करता है।

Note: Formatting flags set using setiosflags() remain active until they are changed or cleared. The resetiosflags() manipulator can be used to clear specified formatting flags.

नोट: setiosflags() द्वारा Set किए गए Formatting Flags तब तक लागू रहते हैं, जब तक उन्हें बदला या Clear नहीं किया जाता। निर्धारित Formatting Flags को Clear करने के लिए resetiosflags() Manipulator का उपयोग किया जा सकता है।

Difference Between Important C++ Manipulators (महत्वपूर्ण C++ मैनिपुलेटर्स में अंतर)

ManipulatorPurpose (उपयोग)
endlInserts a newline and flushes the output stream. नई Line जोड़ता है और Stream को Flush करता है।
setw(n)Sets the minimum width for the next output item. अगली Output Value की Minimum Width निर्धारित करता है।
setfill(ch)Sets the fill character for unused field space. खाली Field को भरने वाला Character निर्धारित करता है।
setprecision(n)Controls floating-point precision. Floating-Point Output की Precision नियंत्रित करता है।
fixedDisplays floating-point numbers in fixed-point notation. Floating-Point Numbers को Fixed-Point Notation में दिखाता है।
setiosflags(flags)Sets specified formatting flags. निर्धारित Formatting Flags को Set करता है।

Important Points (महत्वपूर्ण बिंदु)

  1. Manipulators are used to control input/output formatting in C++.

    C++ में Input/Output Formatting को नियंत्रित करने के लिए Manipulators का उपयोग किया जाता है।

  2. endl inserts a newline and flushes the output stream.

    endl नई Line जोड़ता है और Output Stream को Flush करता है।

  3. setw() applies only to the next formatted output item.

    setw() केवल अगली Formatted Output Value पर लागू होता है।

  4. setfill() remains active until the fill character is changed.

    setfill() तब तक लागू रहता है, जब तक Fill Character को बदला नहीं जाता।

  5. setprecision() controls significant digits by default.

    setprecision() Default रूप से Significant Digits को नियंत्रित करता है।

  6. fixed with setprecision(n) specifies the number of digits after the decimal point.

    fixed के साथ setprecision(n) Decimal Point के बाद Digits की संख्या निर्धारित करता है।

  7. setiosflags() is used to set stream formatting flags.

    setiosflags() का उपयोग Stream Formatting Flags को Set करने के लिए किया जाता है।

Manipulators are useful features of C++ that help control the appearance of input and output data. They make program output more organized, readable, and properly formatted. Manipulators such as endl, setw(), setfill(), setprecision(), fixed, and setiosflags() are commonly used in C++ programs to display numbers, text, and formatted results.

Manipulators C++ की उपयोगी सुविधाएँ हैं, जो Input और Output Data के स्वरूप को नियंत्रित करने में सहायता करती हैं। ये Program के Output को अधिक व्यवस्थित, पढ़ने योग्य और उचित रूप से Formatted बनाते हैं। endl, setw(), setfill(), setprecision(), fixed और setiosflags() जैसे Manipulators का उपयोग C++ Programs में Numbers, Text और Formatted Results प्रदर्शित करने के लिए किया जाता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Syllabus – From Basics to Advanced C++ and Data Structures and Algorithms

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)