Xcode not showing console output; How do you flush the console? - c++

I have a simple C++ program that uses cout and printf to log stuff and it is only showing at the end when the program is closed but if I'm stepping through the program using debug nothing is shown. Did anybody have this problem?

If you're practicing c try fflush, if c++ try cout << endl; each time you want to print.

Related

C++ std::out dont print in debug window

this is a quick question.
I am building a program in C++ and have this weird problem when it comes to print some data in the output window. I don't get any data in the output window but when I set a breakpoint I can see that data is in the variable that I am trying to print. So I am going a bit loco here.
This is the line I use to print my data:
std::cout << midiNoteNumber << std::endl;
Why does nothing appear in the debugging output window?
OutputDebugString is nothing that I could get working so I solved it by doing this:
_RPT1(0, "%d\n", midiNoteNumber);

Xcode debugger not showing C++ cout output

I am currently trying to get the hang of the debugger in Xcode while I am trying to learn C++. I'm coming across a problem where the debugger is not showing any cout output to the console. It just says (lldb)
I've set breakpoints to make sure I don't miss anything:
4 variable outputs
As you can see this piece of code has already been run:
int x = 1;
std::cout << x << " ";
However, the console is still only showing me the following:
console output
I can step over each statement and it still won't show anyting but (lldb)
To my knowlegde the debugger should be showing 1 2 4 8 sequentially as I step over/step into each statement. However, the results are only output the console after I finish debugging.
My question essentially, why am I not seeing anything being displayed?
For the record this is my first question, if I've not searched well enough or broken any rules please feel free to let me know, thanks in advance.
You need to terminate your output line, e.g.:
std::cout << x << "\n";
or
std::cout << x << std::endl;
Your output should show up after your have written a line terminator character as in either of the statements above.

Beginner's Mistake

I know this is probably a dumb question but I am a beginner and I just started learning today. I am using Dev C++ and I wrote my first code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" ;
return 0;
}
I click to compile and run. Nothing comes up. Then I click just "run" and it said it is not compiled yet.
I think there might be errors and I would have gladly fixed them myself but I don't know where I can see the errors in Dev C++.
Could this be a compiler error or did I mess up something in my code?
Thanks!
Most likely your program exits before it manages to write everything out to the console. Try adding new line to the output, like this:
cout << "Hello World" << endl;
When you write to cout, the data is not transfered to the screen right away, out of efficiency considerations. Writing to the screen is relatively slow, so the program prefers to do it in "bursts". The text is accumulated in a buffer until a special command is given to flush the buffer, or the buffer fills up. Writing out endl forces a flush, so the output will appear on the screen before your program exits.

c++ execution screen not stable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I am running a simple program, written in Dev C++ 4.9.9.2 IDE in Windows 7:
// my second program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
system("pause");
return 0;
}
This compiles successfully, but when I run it, the terminal screen comes for a second and then vanishes. How can I keep the output screen of the program visible?
You need to do cause the program to stop and wait for input. Use
system("pause");
at the end of your program before the return from main.
In addition to the options already presented (std::getchar, cin, system("pause"), if the only reason you want the window to persist is to read the output of your program (i.e. debugging), you can simply run the executable from a command prompt.
If you don't mind running the application this way, you can avoid having extra code to prompt a user for input (even if it just a single line) - and if you don't need the window to stay open under normal usage, you don't have to modify anything about your code.

How do I input data using the eclipse console? (c++)

I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.