Parameterzied Stream Manipulators

Having trouble with an assignment. Was looking for some advice...

Objectives: using the iomanip library to format screen output.

Complete the provided main() program with statements to accomplish each of the following. In each case you must use the appropriate I/O stream manipulators to produce the appropriate output wherever possible. Refer to the sample output below as a guide for proper output alignment.

1. Output first first as an integer value, followed by a space, then in its written form.
2. Output second as a base ten value, followed by a space, then as a hexadecimal value, followed by a space, then as an octal value. Make sure the appropriate base indicator prefix is shown in the output.

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
#include <iostream>
#include <iomanip>
using namespace std;
int
main()
{
    bool first;
    int second;
    long third;
    float fourth;
    float fifth;
    double sixth;

    cout << "Enter bool, int, long, float, float, and double values: ";
    cin >> first >> second >> third >> fourth >> fifth >> sixth;
    cout << endl;

// ***** Solution starts here ****
//1
    cout << first << " true"
//2
    cout << second << setbase(10) << setbase(8) << setbase(16);

// ***** Solution ends here ****

    cin.get();
    return 0;
}


We're using a test driver in the compiler. But my answers are not aligning with instructors answers.
Not looking for answer. Just looking for help. Need pointed in the right direction. Any advice would be greatly appreciated.

Thanks..
Last edited on
Did you try looking at some documentation for the <iomanip> header file?
http://www.cplusplus.com/reference/iomanip/
and:
http://www.cplusplus.com/reference/library/manipulators/

This last link has information pertinent to the first problem, namely boolalpha, noboolalpha.



Last edited on
thanks jlb! after doing some reading, I feel much better. I just wasnt sure about using boolalpha or noboolalpha because we havent discussed it in class yet... 1st output is done though!
works without errors when compiled with test driver! yay!

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
#include <iostream>
#include <iomanip>
using namespace std;
int
main0()
{
    bool first;
    int second;
    long third;
    float fourth;
    float fifth;
    double sixth;

    cout << "Enter bool, int, long, float, float, and double values: ";
    cin >> first >> second >> third >> fourth >> fifth >> sixth;
    cout << endl;

// ***** Solution starts here ****
//1.
    std::cout << std::noboolalpha << first;
    cout << " ";
    std::cout << std::boolalpha << first << endl;
//2.
    std::cout.flags ( std::ios::left | std::ios::dec | std::ios::showbase );
    std::cout << second;
    cout << " ";
    std::cout.flags ( std::ios::internal | std::ios::hex | std::ios::showbase );
    std::cout << second;
    cout << " ";
    std::cout.flags ( std::ios::right | std::ios::oct | std::ios::showbase );
    std::cout << second << '\n';


// ***** Solution ends here ****

    cin.get();
    return 0;
}
Last edited on
Actually you should be careful with the following

1
2
3
    std::cout << std::noboolalpha << first;
    std::cout << " ";
    std::cout << std::boolalpha << first << endl;

The boolalpaha "flag" is sticky, meaning it will stay in force until you call noboolalpha. So the above should be:
 
    cout << first << " " <<  std::boolalpha << first << std::noboolalpha << endl;


Next the same is true with left, and dec:
1
2
3
   setf(std::ios::showbase);
   cout << second << " " << hex <<  second << dec << " ";
   cout << right << second << endl;


Also note that since you're using the using declaration on line 3 you don't need to scope each element in the std namespace.

For larger programs, that don't end when you've finished changing these manipulators be sure to "reset" them to their default values.

Topic archived. No new replies allowed.