Cout fails to print anything even though it is on a line before the assertion. It behaves so when I compile and run the code through linux shell. But it prints when tried with an online compiler.
ex-
for(int i =0;i<n;i++)
{ std::cout << "should print";
assert(2==1);
}
I should see "should print" at least once right? And why the different behaviour through online compilers?
Output from std::cout is probably being buffered here. Explicitly emptying the buffer should make it print before the assertion fails. (std::endl,std::flush, std::unitbuf manipulators should do)
for(int i =0;i<n;i++)
{ std::cout << "should print" << std::endl;
assert(2==1);
}
Related
I am new to c++, but this is ridiculous!
// fstream output;
// string func();
// both have proven to be working somewhat properly, as I got something already
// written correctly in the output file and I tested func() in the cout
output << func(); // func() returns a string;
And I get written in the file:
// literally nothing
But when I do
output << "what the hell" << endl;
output << func();
I get
what the hell
{// expected output}
what the hell
{// expected output}
...
what the hell
// last output still missing
I got no idea of what might be the problem, at this point I am convinced the program is just doing it to spite me.
EDIT:
string func()
{
return "test\n";
}
I also just found out that as long as I manually put something to the output in the end, everything will be written, like this:
for(int=0; i<4; i++)
{
output << func();
}
output << endl;
get me
test
test
test
test
// func line
// endl line
As One Man Monkey Squad commented, it was just a problem of not flushing the fstream.
string func()
{
return "test"
}
output << func() << endl;
get me the desired output, without a extra line at the end, beyond the normal new line
I'm using this code to output nodes of a huffman tree to a text file with a certain formatting. All the node outputs within the if block run as expected, but the first output in the else block is missing the '0' fill character after the "L:". It should output "L:076" but instead is outputting "L: 76". The cout looks correct but the text file isn't. All future loops through the else block output like they should, it's only the first loop that is missing the fill character. Here's a picture of my output
void preOrder(node* tree, std::ofstream& of) {
if (tree->label > 0) {
of << "I:" << tree->label << " ";
}
else {
std::cout.width(3);
std::cout << std::right;
std::cout.fill('0');
std::cout << int(tree->ch) << std::endl;
of << "L:";
of << of.fill('0');
of << std::right;
of << int(tree->ch);
of << " ";
return;
}
preOrder(tree->left, of);
preOrder(tree->right, of);
}
From cppreference.com:
The second form (2) sets fillch as the new fill character and returns the fill character used before the call.
"The second form" is the non-const version, that applies here. So my guess (I never used fill myself and I cannot compile your code as it is) would be that the call is correctly applied and then you put the old fill character (blank space presumably) to the stream, because you do:
of << of.fill('0');
Also, I noticed that you dont set the width of of.
Because you're hiding something naughty from us.
#include <iostream>
int main()
{
std::cout.width(3);
std::cout << std::right;
std::cout.fill('0');
std::cout << 3 << std::endl;
return 0;
}
Outputs 003 (live example).
Please provide an MCVE and I'll edit my answer to help you.
I had a segmentation fault in my code, so I put many cout on the suspicious method to localise where.
bool WybierajacyRobot::ustalPoczatekSortowania(){
cout << "ustal poczatek sortowania: " << poczatekSortowania << endl ;
list< Pojemnik >::iterator tmp;
cout << "LOL"; // <-- this cout doesn't print when segfault
if (!poczatekSortowania){ // <- T1
cout << "first task" ;
tmp = polka.begin();
}
else{ // <-- T2
cout << " second task " ;// <-- this cout doesn't print when segfault
tmp = ostatnioUlozony;
cout << " debug cout " ; // <-- this cout doesn't print when segfault
++tmp; // <-- segfault
} ...
If the method was call and don't have segfault every cout from T1 and before was printed.
In line ++tmp is segfault because ostatnioUlozony is NULL, when method go to T2 every cout without first wasn't printed. Why?
I'm using Netbeans ang gcc, I found the "segfault line" with debug in Netbeans, but before I use then I spend some time on adding cout line and running program.
Thanks a lot,
You need to flush the output stream with either std::flush or std::endl (which will give a newline as well), otherwise you are not guaranteed to see the output:
cout << " second task " << std::flush;
Nonetheless, you have undefined behaviour if you increment a singular iterator (which the null pointer is), so this is only likely to work. As far as C++ is concerned, your program could launch a nuclear missile instead.
Another solution is to use std::cerr instead of std::cout. It is unbuffered, so no flushing is required, and it's slightly more idiomatic to use std::cerr for debugging purposes.
I'm very new to programming in C++ but I'm trying to write some code which filters a specific word from a string and then takes a specific action. For some reason the code does not see the text inside the string.
printf("%s \n", data.c_str());
cout << data;
This shows absolutely nothing - meaning I cannot use .find or write it to a file.
printf("%s \n", data);
This shows exactly what I need.
I am writing the code into data with assembly:
mov data, EDX
Why is that I can only use the the last method?
Edit:
Data is initiated as:
std::string data;
If a pointer to a string is null all subsequent calls to cout
stop working
const char* s=0;
cout << "shown on cout\n";
cout << "not shown" << s << "not shown either\n";
cout << "by bye cout, not shown\n";
The two function calls are not equivalent, as \n at printf flushes the stream. Try with:
cout << data << endl;
Be sure you used
#include <string>
in your file header. With this in place you should be able to use
std::cout << data << endl;
with no issues. If you're using a global namespace for std you may not need the std::, but I'd put it anyway to help you debug a it faster and eliminate that as a possible problem.
In Short
You will have a problem with cout, if you don't put a linebreak at it's end!
In Detail
Try adding an endl to your cout (e.g. std::cout << data << std::endl), or use following instruction to activate "immediate output" for cout (without it needing a linebreak first).
std::cout << std::unitbuf;
Complete example:
std::cout << std::unitbuf;
std::cout << data;
// ... a lot of code later ...
std::cout << "it still works";
Sidenote: this has to do with output buffering, as the name unitbuf suggests (if you want to look up what is really happening here).
This way it is also possible to rewrite the current line, which is a good example, where you would need this ;-)
Practical example
using namespace std;
cout << "I'm about to calculate some great stuff!" << endl;
cout << unitbuf;
for (int x=0; x<=100; x++)
{
cout << "\r" << x << " percent finished!";
// Calculate great stuff here
// ...
sleep(100); // or just pretend, and rest a little ;-)
}
cout << endl << "Finished calculating awesome stuff!" << endl;
Remarks:
\r (carriage return) puts the curser to the first position of the line (without linebreaking)
If you write shorter text in a previously written line, make sure you overwrite it with space chars at the end
Output somewhere in the process:
I'm about to calculate some great stuff!
45 percent finished!
.. and some time later:
I'm about to calculate some great stuff!
100 percent finished!
Finished calculating awesome stuff!
I have a very weird bug that I can't seem to figure out. I have narrowed it down to a small section of code (unless the compiler is reordering my statements, which I don't believe is true).
...
std::cout << "here"<< std::endl;
std::vector<int>::iterator n_iter;
std::vector<int>::iterator l_iter;
std::cout << "here?" << std::endl;
for(n_iter = n.begin(), std::cout << "not here" ; std::cout << "or here" && n_iter < n.end(); n_iter++)
{
std::cout << "do i get to the n loop?";
...
}
When I run this, I see the first "here", the second "here?", but I don't get the "not here" or the "or here" output. And I definitely don't get the "do i get to the n loop?".
The weird thing is that my program is working (it is almost using up an entire cpu core... ), but it doesn't finish, it just hangs.
I've tried using clang++ and g++, and I'm not using any optimizations. I have the boost library installed (and am using the boost_program_options part of it), along with armadillo. But I don't think the compiler should be reordering things...
It happens with or without the cout calls inside the for loop declaration, and it doesn't just skip the loop.
The vector "n" has a length of at least 1, and is given by a boost_program_options call.
Any ideas?
The first thing you should try is to output std::endl after each string. This flushes the buffer for the output.
The following program (which has some extra newlines that yours didn't):
#include <string>
#include <iostream>
#include <vector>
int main() {
std::vector<int> n;
n.push_back(3);
n.push_back(3);
n.push_back(3);
std::cout << "here"<< std::endl;
std::vector<int>::iterator n_iter;
std::vector<int>::iterator l_iter;
std::cout << "here?" << std::endl;
for(n_iter = n.begin(), std::cout << "not here\n" ; std::cout << "or here\n" && n_iter < n.end(); n_iter++)
{
std::cout << "do i get to the n loop?\n";
}
}
Has the following output:
[5:02pm][wlynch#orange /tmp] make foo
g++ foo.cc -o foo
[5:02pm][wlynch#orange /tmp] ./foo
here
here?
not here
or here
do i get to the n loop?
or here
do i get to the n loop?
or here
do i get to the n loop?
or here
This appears to be what you expect, so I'm not sure where you are having issues on your end, but it may be in skipped code.