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;
}
Answer Program 1:
Output:
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:
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:
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:
Explanation:
Here, we used three manipulators showpoint, noshowpoint, and endl.