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

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++ में मैनिपुलेटर्स विशेष फंक्शन या ऑब्जेक्ट होते हैं, जिनका उपयोग इनपुट/आउटपुट स्ट्रीम के साथ डेटा की फॉर्मेटिंग को नियंत्रित करने के लिए किया जाता है। इनकी सहायता से डेटा को व्यवस्थित और आसानी से पढ़े जाने योग्य रूप में प्रदर्शित किया जा सकता है।

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

C++ के कुछ महत्वपूर्ण मैनिपुलेटर्स endl, setw(), setfill() और setprecision() हैं।

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 मैनिपुलेटर आउटपुट में नई लाइन जोड़ता है और आउटपुट स्ट्रीम को फ्लश भी करता है। इसके बाद प्रदर्शित होने वाला डेटा अगली लाइन में आता है। इसकी तुलना \n से की जाती है, लेकिन endl नई लाइन देने के साथ आउटपुट स्ट्रीम को फ्लश भी करता है।

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() मैनिपुलेटर अगली आउटपुट वैल्यू के लिए न्यूनतम फील्ड विड्थ निर्धारित करता है। यह iomanip हेडर फाइल में उपलब्ध होता है। यदि वैल्यू निर्धारित विड्थ से छोटी है, तो डिफॉल्ट रूप से अतिरिक्त रिक्त स्थान जोड़े जाते हैं।

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) प्रत्येक संख्या के लिए कम से कम तीन कैरेक्टर की विड्थ निर्धारित करता है। यह विड्थ केवल अगली आउटपुट वैल्यू पर लागू होती है।

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() मैनिपुलेटर फील्ड की खाली जगह को भरने के लिए उपयोग किए जाने वाले कैरेक्टर को निर्धारित करता है। इसका उपयोग सामान्यतः setw() के साथ किया जाता है। निर्धारित किया गया फिल कैरेक्टर तब तक लागू रहता है, जब तक उसे बदला नहीं जाता।

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('*') तारांकन चिह्न (*) को फिल कैरेक्टर बनाता है, जबकि setw(3) न्यूनतम फील्ड विड्थ तीन कैरेक्टर निर्धारित करता है। डिफॉल्ट रूप से संख्याएँ दाईं ओर संरेखित होती हैं, इसलिए तारांकन चिह्न संख्या से पहले दिखाई देते हैं।

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() मैनिपुलेटर फ्लोटिंग-पॉइंट आउटपुट की प्रिसिजन नियंत्रित करता है। डिफॉल्ट रूप से यह सार्थक अंकों (significant digits) की कुल संख्या निर्धारित करता है। fixed के साथ उपयोग करने पर यह दशमलव के बाद के अंकों की संख्या निर्धारित करता है।

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) पाँच सार्थक अंक प्रदर्शित करता है।

  • setprecision(2) डिफॉल्ट रूप से दो सार्थक अंक प्रदर्शित करता है।

  • fixed << setprecision(2) दशमलव के बाद दो अंक प्रदर्शित करता है।

Summary (सारांश)

ManipulatorPurpose (उपयोग)
endlInserts a newline and flushes the output stream. नई लाइन जोड़ता है और स्ट्रीम को फ्लश करता है।
setw(n)Sets the minimum width for the next output item. अगली आउटपुट वैल्यू की न्यूनतम विड्थ निर्धारित करता है।
setfill(ch)Sets the fill character for unused field space. खाली फील्ड को भरने वाला कैरेक्टर निर्धारित करता है।
setprecision(n)Controls floating-point precision. फ्लोटिंग-पॉइंट आउटपुट की प्रिसिजन नियंत्रित करता है।

setw() and setprecision() require the iomanip header. The endl manipulator is available through iostream.

setw() और setprecision() के लिए iomanip हेडर फाइल आवश्यक है। endl मैनिपुलेटर iostream के माध्यम से उपलब्ध होता है।

Comments

Popular posts from this blog

IMP Questions of OOPS using CPP

OOPs using C++ Language Topics Covered