C++ Manipulators | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Manipulators (set 1).
Submitted by Nidhi, on July 13, 2020

Program 1:

#include <iostream>
#include <istream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    istringstream strWithSpace("  Hello World");

    string strWithoutSpace;

    getline(strWithSpace >> std::ws, strWithoutSpace);

    cout << strWithoutSpace << endl;

    return 0;
}

Output:

Hello World

Explanation:

Here, we created string strWithSpace using istringstream class that contains some whitespaces before the first word, and also created an empty string. Here we used manipulator std::ws to ignore the white spaces before the first word, and then we used std::ws manipulator in the getline() function, and we got string without whitespace in the string strWithoutSpce.

Then the final output will be printed on the console screen using the below statement,

cout << strWithoutSpace << endl;

Program 2:

#include <iostream>
#include <istream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    int Len1 = 0;
    int Len2 = 0;

    istringstream strWithSpace("  Hello World");

    string strWithoutSpace;

    getline(strWithSpace >> ws, strWithoutSpace);

    Len1 = strWithoutSpace.size();
    Len2 = strWithSpace.size();

    cout << Len1 << " " << Len2 << endl;

    if (Len1 > Len2) {
        cout << "ABC" << endl;
    }
    else {
        cout << "XYZ" << endl;
    }

    return 0;
}

Output:

main.cpp: In function ‘int main()’:
main.cpp:20:25: error: ‘std::istringstream {aka class std::basic_istringstream}’ 
has no member named ‘size’; did you mean ‘tie’?
     Len2 = strWithSpace.size();
                         ^~~~

Explanation:

It will generate an error because here we called function size() from the object of istringstream class, but size() member function is not available in the istringstream class.

Program 3:

#include <iostream>
#include <istream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    int Len1 = 0;
    int Len2 = 0;

    istringstream strWithSpace("  Hello World");

    string strWithoutSpace;

    getline(strWithSpace >> ws, strWithoutSpace);

    Len1 = strWithoutSpace.size();
    Len2 = strWithSpace.str().length();

    if (Len1 > Len2) {
        cout << "ABC" << endl;
    }
    else {
        cout << "XYZ" << endl;
    }

    return 0;
}

Output:

XYZ

Explanation:

Here, we created string strWithSpace using istringstream class that contains some whitespaces before the first word, and also created an empty string. Here we used manipulator std::ws to ignore the white spaces before the first word, and then we used std::ws manipulator in the getline() function, and we got string without whitespace in the string strWithoutSpce.

We created two local variables Len1 and Len2 initialized with zero. Then we calculated the length using the below statements:

Len1 = strWithoutSpace.length();
Len2 = strWithSpace.str().length();

Here, we converted the object of istringstream class into the string and then called length() function. Then the value of Len1 and Len2 are 11 and 13 respectively. That's why the condition gets false, and "XYZ" will be printed on the console screen.

Program 4:

#include <iostream>
using namespace std;

int main()
{
    cout << showpoint << 3.14 << endl;
    cout << noshowpoint << 3.14 << endl;

    return 0;
}

Output:

3.14000
3.14

Explanation:

Here, we used three manipulators showpoint, noshowpoint, and endl.

  • showpoint: It is used to show decimal points up to 5 digits, if the specified number contains decimal point less than 5 then extra 0 printed on the console screen.
  • noshowpoint: This manipulator is used to remove trailing zeros from the output of a specified number.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.