value not adding up

I am doing a hw assignment in codeblock with output manipulators. The value is not adding up to the instructor's value.

It is also acting like I've input the inernal manipulator on the fifth code tested line.

this link is a screenshot of my code on codeblock and what it looks like compiled.

also for the sixth line, I am having the same error as the fifth; the value is not the same.

http://prntscr.com/8eugul

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
#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 ****
cout << first << ' ' << boolalpha << first << noboolalpha <<  endl;                                      //This is line 1
cout << second << ' ' << hex << showbase << second << ' ' << oct << second << noshowbase << endl;        //This is line 2
cout << dec << setw(16) << third << endl;                                                                //This is line 3
cout << internal << setw(13) << setprecision(4) << showpoint << showpos << fourth << noshowpos << endl;  //This is line 4
cout << scientific << setw(15) << setprecision(4) << fifth << endl;                                      //This is line 5
cout << setprecision(7) << sixth << endl;

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

    cin.get();
    return 0;
}

Last edited on
We don't know what the instructions are. It looks like the values for fifth and sixth are swapped. Maybe you should change the order you read fifth and sixth from cin?
Last edited on
Thank you Peter! That helped out with the values not being the same, but I still have a negative value on line five when the instructor's value is positive.

My first thought was to use abs() but that is not output manipulation and the answer value will be negative sometimes (since first -> sixth are not fixed numbers)

It's also is giving me the output of the internal manipulator being used when internal is not being used on the line.

http://prntscr.com/8evi6b
^^^
it is a success

http://prntscr.com/8evioi
^^^
showing the internal

http://prntscr.com/8evj4l
^^^
is positive when instructor's line is negative

PopRockCandy wrote:
It's also is giving me the output of the internal manipulator being used when internal is not being used on the line.

It doesn't matter if you do it on the same line/statement. cout is an object and remembers everything you have previously done. If you have told it to use internal it will use it until you tell it otherwise.
Last edited on
It's also is giving me the output of the internal manipulator being used when internal is not being used on the line.
YOu did you it on line 22 and never used something which cancels it. It is a persistent manipilator, unlike setw which works only for next output.
Thank you Peter! You were very helpful.
@MiiNiPaa

We are not that far into the class. I looked on the Cplusplus internal manipulator page to see what I forgot to insert, but it did not show any type of closing statement or bracket.

How do you make the internal operator exclusive to one line?
How do you make the internal operator exclusive to one line?
There is no way to do that, as there is no difference between outputting everything on same line or different lines. All manipulators are pessitent except odd setw() one. You need to pass different manipulator which will cancel std::internal. It seems that you need std::right here.
To understand why lines are not relevant you should know that << is a binary operator (just like + and *). It takes two arguments and return something.

The first argument is the stream object, in this case cout. The second argument can be a number of things (strings, numbers, manipulator functions). The return value is a reference to the first argument and this is what makes it possible to chain multiple << in the same statement.

Small example:
 
cout << a << b << endl;

This line of code could be written as (((cout << a) << b) << endl);

First thing to run is (cout << a), which outputs a and returns a reference to cout.

This leaves us with ((cout << b) << endl).

Second thing to run is (cout << b), which outputs b and returns a reference to cout.

This leaves us with (cout << endl) and it's the last thing that happens.
Topic archived. No new replies allowed.