Xcode debugger not showing C++ cout output - c++

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.

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);

Program doesn't stop after returning 0 from main

I wrote a program that encodes files with Huffman coding. It works fine but for some reason after returning 0 from main function it doesn't stop.
Main function looks like:
int main(int argc, char *argv[])
{
if (argc < 5)
{
std::cout << "..." << std::endl;
}
else
{
if (!std::strcmp(argv[1], "haff") && !std::strcmp(argv[2], "-c"))
HuffmanC(argv[3], argv[4]);
if (!std::strcmp(argv[1], "haff") && !std::strcmp(argv[2], "-d"))
HuffmanD(argv[3], argv[4]);
std::cout << "Operations: " << count << std::endl;
}
return 0;
}
When I run it, I get:
MacBook-Pro-Alex:code alex$ ./main haff -c test.txt test.haffOperations: 37371553
It ends with an empty line and terminal says that the program keeps running, but the last cout statement executes well and as I get it it should return 0 and finish. How can I make it finish after returning 0? Or is the problem in the rest of the code?
Or is the problem in the rest of the code?
Possibly. Perhaps you've corrupted your stack somehow, so that you're "returning" from main to someplace you didn't expect. We can't really know without an complete, verifiable example.
How can I make it finish after returning 0?
You can use the kill command on MacOS to terminate it forcefully. Using the GUI task manager may or may not work.
But perhaps a more effective course of action would be to attach a debugger to the process and see what it's actually doing.
You could read this explanation on how to do this on MacOS with XCode - but I don't use MacOS, so I wouldn't know. Also, #SergeyA graciously suggests trying using pstack to get the process' current stack. Of course, if the stack has been garbled, there's no telling what you'll actually get.
Make sure the application is compiled with debugging information included.
Finally - it's probably better to run the program with a debugger attached in the first place, and set a breakpoint at the last cout << line.

Using function parameters and arguments in Xcode 8 with C++

I've only recently started learning to code (About 3 days ago, to be exact!) so I really have no idea what I'm doing, to the point that I'm having trouble researching answers to my questions because I don't really know the terminology for anything.
Anyway, I'm learning on learncpp.com, and I'm up to 1.4a – "A first look at function parameters and arguments".
I tried to run this piece of code:
#include <iostream>
void printValues(int x, int y)
{
std::cout << x << std::endl;
std::cout << y << std::endl;
}
int main()
{
printValues(6, 7);
return 0;
}
Apparently it's supposed to spit out:
6
7
in the console window.
However, when I run it, it just says (lldb) in the console window and in the variable window it says:
[A]x = int (6)
[A]y = int (7)
The program also doesn't seem to finish running as it should – it doesn't spit out the return number at the end and when I try to change it and rerun it it asks if I want to terminate the program that's already running.
If it's any help, it highlights this line in green:
std::cout << x << std::endl;
It might also be worth noting that the tutorials on the website use Visual Studios, so I assume there's a difference between two programs that's causing me to have a problem?
Sorry if there is an obvious way to find the answer to this question, I've done some Googling and I tried watching Youtube tutorials etc. but I can't seem to find anything addressing my issue. Maybe I'm not looking in the right place.
If anybody would be able to help me or even direct me to where I might find an answer that would be much appreciated.
Thank you!
Did you set a breakpoint on that line? Usually the debugger will stop on a line if there's an error, or the developer set a breakpoint on that line. If it's highlighted in green, that's a breakpoint. You should see a blue pointer to the left of the line, like this:
If you click on it, it will turn light blue and the breakpoint will be disabled. You can then either type "continue" at the prompt, or press the continue button (it's a right-pointing triangle with a rectangle to the left of it, sort of like the "Play/Pause" button on a music player).
To remove the breakpoint, grab the blue arrow and drag it to the left and then let go of it. It should disappear in a puff of smoke.

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

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.

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.