Clion's Debugger is running program, but the regular console is not. - c++

When I compile and run in Clion 2018.1.3, it builds fine but nothing happens. No errors either. The console opens up, shows the path location of the project, lets me type, but it doesn't show any of my printf statements. When I run it in the debugger, everything shows up and it works fine. It wasn't like this before. I tried back tracking and no luck. Inserting a printf statement on the first line of main doesn't print on the console either.
I tried re-writing a huge chunk of the code again on a different project. No luck. Can I get some ideas on why there is this miscommunication? This is written in c++ but was translated from c using cstdio cstdlib libraries.

CLion captures the output of your program. That means output to standard output (use printf or similar) isn't line-buffered as is the default. Instead it is fully buffered. If you want output to happen you need to explicitly flush the standard output.
The reason it works with debugging might be (I'm speculating here) that CLion itself recognizes the newline and write the output then. Or that it modifies standard output to be unbuffered.

Related

How can MSVC with /TC option compile corecrt libs(like stdio), written in C++?

In order to find answer to my own question How to use ESC sequences to set foreground colors?
I tried to find out how printf() itself works, and then maybe create my own function, which only accepts RGB and then automatically applies this color as an escape sequence to VT100 emulator of windows console. But eventually as I started digging into the code I discovered, that the most of IO, as well as stdin and stdout are written in C++ as well as string validator for printf format string.
As I was taught, in order to execute the program, compiler does all-the-way-back inclusion up to point, where all the text from all the included libs is pasted in your code. But my compiler is set to C only, so it can't compile C++ code. How could this happen? And is there other way to write stdout with escape codes already known to me(\033[%d;%d;%d;%d;%dm), though pushing the limitations of the console output?

Clearing Screen in C++/XCode

I am trying to write up a C++ program for a class using XCode. One of the things I wish to do, is to simply clear the screen. I've looked into how to do this, however the catch is that the code needs to run on both a Windows and Macintosh computer. I've looked at some other similar questions, but none of the answers help me. I know there is no "screen" but I want the system to clear the output window. I know that the command system("clear"); does what I want it to, but when XCode tests the program, instead of clearing the screen it prints TERM Variable not set
I've tried opening up the terminal and typing clear and it does in fact respond the way I want it to, so why doesn't the 'terminal' inside of XCode do the same? I just want to get the output window in XCode to respond to clear the same way that the terminal already does.
Here is something I have already tried;
I went to the terminal and ran echo $TERM, to which the terminal responded xterm-256color. I then went over to XCode and opened the "Scheme" settings, and found an Environment Variables setting under "Arguments". I added a variable (to the blank list) called TERM and gave it value xterm-256color. Upon running the program again, the output displays ¿[H¿[2J in the output window, positioned where the TERM Variable not set used to be printed.
Last thing, as a reminder, I cannot change the source code from the way it is now, or it could cause errors when the program is run on a Windows machine.
It does not work because you are "lying": The terminal in Xcode is not a xterm-256color, but it is dumb terminal. More precise, the display represents a NSTextStorage that collects stdout and/or (depending on target switch) stderr.
A dumb terminal is not able to clean the display. If you want to change this, you can write a plug-in similar to Xcode-Colors what adds the ability to understand ansi color codes.
However, if your requirement that the code simply run at Windows and OSX, you may stick with your solution system("clear"), since it works prefectly in the "normal" OSX terminal.

Why would a program run when launch from windows but not the command prompt?

I wrote a small C++ program in VS2k8. When I launch it from windows (double click the exe file) it runs fine. When I go to the command prompt and try to run it it will hang and eventually crash. I've created test programs with simple outputs that work fine both ways.
Is there something I'm missing? I'm relatively new to programming. I'm trying to launch this program using the VBA shell command but it produces the same outcome as the command prompt.
The funny thing is it was working fine at first until I went in to change the value of a constant variable and rebuilt it (I didn't think that had anything to do with it but I changed it back with no success). No settings where changed.
Edit: I've name it time.exe and than copies.exe (when I tried copying and pasting the code into a new project). The actual code is about 250 lines, not sure what part of it would be causing the issue. It opens a .csv file, loads the information into vectors, and then compares the vectors to each other (adding something to the end of it if it meets certain conditions). It than outputs the file to another .csv file.
Might suggest that the current directory on start up is different and this is causing your issue as you make some assumptions about the current path or drive?

C++ interpreter / console / snippet compiler

I am looking for a program where I can enter a C++ code snippet
in one window, press a button, and get output in another window.
Compilation should somehow be hidden behind the button. On a
per-snippet basis would be fine, full interactive probably asking
too much. It should run under Linux/Unix. Main use case would be
learning/testing/short debugging, etc.
Related stuff I found:
-- the Reinteract project for python (which i'm told sage has features similar to)
-- the same thread for C# here: C# Console?
-- the CINT interpreter from the CERN ROOT project
(which may be close, but maybe there are more comfortable apps around)
-- some programs called Quickly Compile or Code Snippet, which are M$.
http://codepad.org/ works nicely for this purpose. By default, it will run what you paste when you hit submit and display the result (or any errors you might have).
Dinkumware has a page for this AND you can choose the compiler
http://dinkumware.com/exam/default.aspx
Do something like this ?
test a C# snippet code by just copying it to the clipboard and then type csc.exe:
http://reboltutorial.com/blog/redirect-shell-to-rebol-console/
Cling (interactive C++ interpreter, built on the top of LLVM and Clang libraries): https://root.cern.ch/drupal/content/cling
Just configure your code editor to compile and run your code snippets.
Most code editors have the capability of 'sending' the current buffer/file to an external tool. I configure one editor key binding to compile the current buffer, and another key binding to execute whatever was last compiled (actually to run whatever has the same base filename as the current buffer with an '.exe' extension). My experience is with Windows, but the same or similar can be done on Unix/Linux.
Then it becomes one keystroke to compile and another to run what I jut compiled. This could also easily be just a single keystroke to compile & run, but I have several key bindings set to compile using various different compilers. That way I can easily test snippets using the latest MSVC, MSVC 6, MinGW GCC, Comeau and Digital Mars compilers to check for differences.
I would do it like this:
Capture the 'snippit' as text
Create a.cpp with the following:
int main() {
snippitCode();
return 0;
}
void snippitCode() {
// INSERT SNIPPIT HERE
}
Use 'exec' to launch a compiler and pipe the output to an output file.
Use 'exec' to run the application and pipe the output to an output file.
In the 'output' window, you can run 'tail -f' on the output file to continuously update when new output arrives.

Show or capture complete program output using cmd.exe

I'm practising writing recursive functions using Visual Studio 2015 on Windows 7.
I use cout to track the progress of my code, but it shows too many results and even though I stop the program I can not see the initial result... I can only see the output from the middle.
How can I see the complete program output?
The problem is that cmd.exe (the Windows commad prompt) has a fixed-size buffer for displaying output. If your program writes a lot of output, only the last N lines will be displayed, where N is the buffer size.
You can avoid this problem in several ways:
Write to a file, instead of to std::cout. All of your output will be captured in the file, which you can read in the text editor of your choice.
Redirect the standard output to a file. Run your program as my_prog.exe > output.log and the output will be redirected to output.log.
Pipe your output to the more command, to show it one screen at a time: my_prog.exe | more
Increase the cmd.exe buffer size. If you right-click on the title bar of the command window, you can select the Properties menu option. Within the Layout tab, you'll see a section called Screen buffer size. Change the Height to a larger value, and you'll be able to capture that many lines of output. Note that this is somewhat unreliable, since you often don't know in advance of executing your program how many lines it will output. One of the other approaches, using files, is often a better solution.
Note that this isn't really a problem with your C++ program. It's entirely reasonable to be able to produce a large quantity of output on the standard output stream. The best solutions are the ones that redirect or pipe the output to a file. These operations are available on most sensible platforms (and Windows, too) and do exactly what you need without having to change your program to write to a file.
I'm not sure to understand your problem, maybe you should write the output in a file instead of the standard output ? Then you will see all your results
Run your application from the commandline and redirect the output to a file:
yourapp.exe > yourapp.log