473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

IO manipulators manipulate previous cout expressions?


The code:
// Block 1
//
cout << "Integrating " << str << " on [" << low << "," << high
<< "]" << endl << " exact answer: " << exact << endl << flush;

#if 0

// Block 2
//
for (int i = 0; i < 6; ++i )
{
cout << setw(16) << setiosflags(ios::left) << label[i] << flush;
cout << setiosflags(ios::right) << setw(20) << setiosflags(ios::fixed)
<< setprecision(17) << answer[i];
cout << " error: " << fabs(exact - answer[i]) << endl;
}

#endif
The output looks like:
Integrating x on [0,10]
exact answer: 50
However, when I delete the "#if 0" and "#endif", the output becomes:
Integrating x on [0.00000000000000000,10.00000000000000000]
exact answer: 50.00000000000000000
I tried sticking in a "flush" in Block 1 to make sure the buffer is flushed
and unaffected by the manipulators within Block 2. But it doesn't seem to do
a bit of good.

Why is it that the manipulators in block 2 affect the output generated in
block 1?

Thanks,
Pete
Apr 24 '06 #1
1 2367
p@mapson.dirac.org wrote:
The code:
// Block 1
//
cout << "Integrating " << str << " on [" << low << "," << high
<< "]" << endl << " exact answer: " << exact << endl << flush;

#if 0

// Block 2
//
for (int i = 0; i < 6; ++i )
{
cout << setw(16) << setiosflags(ios::left) << label[i] << flush;
cout << setiosflags(ios::right) << setw(20) << setiosflags(ios::fixed)
<< setprecision(17) << answer[i];
cout << " error: " << fabs(exact - answer[i]) << endl;
}

#endif
The output looks like:
Integrating x on [0,10]
exact answer: 50
However, when I delete the "#if 0" and "#endif", the output becomes:
Integrating x on [0.00000000000000000,10.00000000000000000]
exact answer: 50.00000000000000000
I tried sticking in a "flush" in Block 1 to make sure the buffer is flushed
and unaffected by the manipulators within Block 2. But it doesn't seem to do
a bit of good.

Why is it that the manipulators in block 2 affect the output generated in
block 1?

Please post a minimal, compilable program which exhibits the problem.
With the short snippet that you've posted, it would seem that your loop
body is never executed... since there is no "error:" output shown.

I'm suspecting that the code snippet that you've shown is executed
multiple times, and that the first time Block 1 is executed, it works as
expected, but the 2nd and subsequent executions are showing the "odd"
behaviour.
Apr 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul Davis | last post by:
I have some simple test code, which is copied pretty much verbatim from Josuttis (although he doesn't give complete examples): #include <iostream> main() { cout << hex << 1 << endl; std::cout...
1
by: Andy | last post by:
1) Are the manipulators like std::endl and std::ends independent of character set. That is, can I use the same manipulators in both the following cases: a) <code> std::basic_stringstream<char>...
16
by: Darius.Moos AT esigma-systems DOT de | last post by:
Hi, there seems to be a problem with manipulators in g++/gcc-2.96. First some are not defined in std:: and second, when using this manipulators on streams, they give strange results. A small...
3
by: paul.wilkins | last post by:
Hi, I have a function I have written that works with g++ 2.95.3 that I would like to use with newer g++ compilers >= 3.2.0. The function is a stream manipulator similar to setw(). The code...
2
by: Dave | last post by:
Hello all, It is my understanding that there is not a portable way to write a user-defined manipulator that takes arguments. Please consider below which, for purposes of illustration,...
3
by: Lionel B | last post by:
In an overloaded output operator for a class I want to detect ostream manipulators such as flush, endl, ends, etc. I am stymied by how to do this; here's a minimal program indicating my problem: ...
6
by: Martin Eisenberg | last post by:
What do I need to make this work? #include <iostream> #include <fstream> using namespace std; struct LoggedCout { ofstream file; LoggedCout(const char* name) : file(name, ios::app) {}
9
by: David W | last post by:
A colleague wants to do this with a std::ostream: os << fahrenheit << 37.0; This would output 98.6 (i.e., Celsius --Fahrenheit). I've already suggested os << fahrenheit(37.0), but he doesn't...
3
by: shk253 | last post by:
Hi all - I'm trying to generate a payroll. I'm having trouble with the cout statements overwriting the previous inputs, e.g. enter info for employe 1: blah blah blah. Enter info for employee 2: blah...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.