manipulators - using ios::

Hi, I am learning manipulators, and I have a few questions about when to use ios::. I understand I need to use this for the manipulator to have access to its 'base' but in certain examples in my book it is not used, and i dont understand why it is not used in these examples. I have shows all the examples below. If anyone could explain, it would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	cout << setprecision(2) << 1000.242 << endl;   // setprecision no ios::

	cout << setw(20) << "hello there";

	return 0;
}

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	cout << setiosflags(ios::showpos) <<
		setiosflags(ios::scientific) << 123 << " " << 123.23;   //setioflags does not need ios:: but scientific does        
	return 0;
}

---------------------------------------------------------
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	char s[80];

	cin >> ws >> s;		// ws no ios::		
	cout << s;
	return 0;
}

---------------------------------------------------------
#include <iostream>
#include <iomanip>

using namespace std;

ostream &setup(ostream& stream)
{
	stream.setf(ios::left);		// left needs ios:: 
	stream << setw(10) << setfill('$');   // setw no ios::
	return stream;
}

int main()
{
	cout << 10 << " " << setup << 10;                                        
	return 0;
}


Hello DonnaPin,

There are several ways to use the manipulators. In the end I find this to be the easiest to use and it does the same as the other ways.

std::cout << std::fixed << std::showpoint << std::setprecision(2);

The "showpoint" is for printing decimal values of "1.0", but I have found that when "setprecision" has a value greater than (0) it will always print ".0" because "setprecision" says to print at least 1 decimal place.

I do not know if there is a better time to use "std::ios::something" over the above code or not. What I have done is usually simple and the above code works just fine.

It may come down to how much do you want to type and how much checking you want to do when you do not remember the correct syntax.

Andy
@DonnaPin
The reason ios:: is not being used in the examples, if that is what is troubling you, it is because the dreaded using namespace std; line is included in your examples.

This is another good reason for not using using namespace std; and instead being namespace specific once you identify which namespace is appropriate ( ios:: alone vs std::ios:: )

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::setprecision(2) << 1000.242 << std::endl;   // setprecision no ios::
    std::cout << std::setw(20) << "hello there";

    return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>

int main()
{
    std::cout
    << std::setiosflags(std::ios::showpos)
    << std::setiosflags(std::ios::scientific)
    << 123 << " " << 123.23;   //setioflags does not need ios:: but scientific does
    
    return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>

int main()
{
    char s[80];

    std::cin >> std::ws >> s;        // ws no ios::
    std::cout << s;
    
    return 0;
}


etc, etc
Topic archived. No new replies allowed.