You are not logged in.

#1 2010-07-13 22:14:09

halfgaar
Member
Registered: 2010-05-22
Posts: 50

KDevelop 4 C++ debugging shows no output for cout

Hi,

In KDevelop4, I have this C++ test program:

#include <iostream>
#include <cstdlib>

using namespace std;

int multiply(int a, int b);

int main(int argc, char **argv) {
    cout << "Helloasdf, world! abla" << endl;
    cout << "line 2" << endl;
    cout << "a line without endl";
    cout << "multiply: " << multiply(4, 10);
    return 0;
}

int multiply(int a, int b) {
    int antwoord = a * b;    
    return antwoord;    
}

When launching, it says this:

Helloasdf, world! abla
line 2
a line without endlmultiply: 40

However, when launching in debug mode, it says this:

Helloasdf, world! abla
line 2

When I add a endl to line 2, it does output the next line. When I set breakpoints, stepping over the lines doesn't do anything.

Is this a bug?

Offline

#2 2010-07-13 23:17:18

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: KDevelop 4 C++ debugging shows no output for cout

halfgaar wrote:

Is this a bug?

Probably not. Using endl flushes the stdout buffer, and presumably optimisation stops it being flushed automatically when your program exits. If you really don't want a newline at the end, just call cout.flush()

Last edited by PirateJonno (2010-07-13 23:20:23)


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#3 2010-07-14 07:52:49

halfgaar
Member
Registered: 2010-05-22
Posts: 50

Re: KDevelop 4 C++ debugging shows no output for cout

When I change the code to this:

#include <iostream>
#include <cstdlib>

using namespace std;

int multiply(int a, int b);

int main(int argc, char **argv) {
    cout << "aHelloasdf, world! abla" << endl;
    cout << "line 2" << endl;
    cout << "a line without endl"; // it doesn't print this line in debug mode when there is no endl
    cout.flush();
    cout << "multiply: " << multiply(4, 10);
    cout.flush();
    return 0;
}

int multiply(int a, int b) {
    int antwoord = a * b;    
    return antwoord;    
}

It still doesn't output the last lines when debugging.

Offline

#4 2010-07-14 14:33:39

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: KDevelop 4 C++ debugging shows no output for cout

Streams are automatically flushed when a program terminates, so the cout.flush() don't change anything.

I'm guessing that KDevelop expects the program to end with a newline, otherwise it will overwrite the last line.  This is a reasonable expectation -- some shells do this too.  So, just end your program with a newline, unless it has to output data in a really structured format for some reason.

Offline

#5 2010-07-14 15:35:49

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: KDevelop 4 C++ debugging shows no output for cout

Don't know how I managed to misinterpret "debug mode" as "optimisation", not that it makes me any less wrong roll
Anyway taviantor's suggestion sounds reasonable


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#6 2010-07-14 18:36:14

halfgaar
Member
Registered: 2010-05-22
Posts: 50

Re: KDevelop 4 C++ debugging shows no output for cout

I'm guessing that KDevelop expects the program to end with a newline, otherwise it will overwrite the last line.  This is a reasonable expectation -- some shells do this too.  So, just end your program with a newline, unless it has to output data in a really structured format for some reason.

It doesn't overwrite, it just stops outputting.

But OK, I guess I can live with it if it's just the way KDevelop interprets shell output commands.

Offline

Board footer

Powered by FluxBB