Q:

C++ Manipulators | Find output programs | Set 1

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Manipulators (set 1).

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;
}

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;
}

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;
}

Program 4:

#include <iostream>
using namespace std;

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

    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

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;

Answer Program 2:

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.

Answer Program 3:

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.

Answer Program 4:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Manipulators | Find output programs | Set 2... >>
<< C++ const Keyword | Find output programs | Set 2...