Click to See Complete Forum and Search --> : A REALLY silly question...... :(
Condor
10-23-2002, 07:16 PM
I have a very simple hello world program in C++.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
exit(0);
}
Now, if I run the above code in console (upon boot up, before starting X), I can see the hello world. But under X, I use konsole to run the code but get nothing? What should I do? Thanks......
baldguy
10-23-2002, 08:01 PM
I'm assuming you are doing this.
g++ -o hello hello.cpp
./hello
I can't recreate the problem. It works for me in gnome-terminal and konsole.
Condor
10-23-2002, 08:30 PM
Yup. I did g++ hello.cpp -o hello then ./hello
I only get the prompt in konsole, but if i quit X and do ./hello in console I get the hello world...... :(
Has it anything to do with how the output is redirected? The default output stream is the console (screen) right? Does konsole or X or whatever do anything to change that? How can I check where the output is directed to?
Condor
10-23-2002, 11:18 PM
I managed to get it in konsole now. But I have to either cout << "Hello World!\n"; or cout << "Hello World!" << endl;
This is kinda troublesome say if I don't want to have a line break it won't show up......
bwkaz
10-24-2002, 10:50 AM
Oh, right. cout is line-buffered, so if you don't specifically end a line, it won't get printed. I don't know why it works when you're not in an xterm, though. I also don't know why cout isn't getting flushed when it gets destroyed.
Wait a minute, maybe I do. Maybe it is getting flushed... When you're in konsole, if you're (for example) running a large, long-running program that prints nothing to the screen, and you type something in, then it echoes back to the screen. Then, when the first program finally exits, konsole reprints your prompt starting at column 0, on the left. Which overwrites the stuff you've typed in and what's been printed. Of course, it echoes it again after the prompt, but that's only because it's input.
When you don't print a '\n' or an endl, the same thing happens. Your output gets printed on the next line, and then when your program is done running (which takes very little time), the shell prompt overwrites what you've outputted, because the "current line" thingy is the same. You'll want that extra endl there anyway, so as to set off your program's output from someone's prompt. It irritates me to no end when a program I'm running decides it doesn't want to do this simple thing...
Condor
10-24-2002, 04:04 PM
Originally posted by bwkaz
Oh, right. cout is line-buffered, so if you don't specifically end a line, it won't get printed. I don't know why it works when you're not in an xterm, though. I also don't know why cout isn't getting flushed when it gets destroyed.
Wait a minute, maybe I do. Maybe it is getting flushed... When you're in konsole, if you're (for example) running a large, long-running program that prints nothing to the screen, and you type something in, then it echoes back to the screen. Then, when the first program finally exits, konsole reprints your prompt starting at column 0, on the left. Which overwrites the stuff you've typed in and what's been printed. Of course, it echoes it again after the prompt, but that's only because it's input.
When you don't print a '\n' or an endl, the same thing happens. Your output gets printed on the next line, and then when your program is done running (which takes very little time), the shell prompt overwrites what you've outputted, because the "current line" thingy is the same. You'll want that extra endl there anyway, so as to set off your program's output from someone's prompt. It irritates me to no end when a program I'm running decides it doesn't want to do this simple thing...
Damn! You're dead right on target. In console, the prompt appears immediately after my cout. Then in konsole nothing is seen. I tried it on Windows and it automatically starts the prompt on a newline.
I tried making my text longer than my prompt and viola! I see the prompt with the remainder of my text!!! So konsole ensures that the prompt starts from column 0.
Learnt something again. You're the man! Thanks!