How to output new line in Visual Studio actions? - c++

I add a break point in Visual Studio 2015, with an action to output a string to Output Window. There will be an auto-added line break at the end. Problem is, my previous output message(which is not output by break point) has no line break.
So I want to add new line character at the beginning of my string, to avoid it messing up with my previous message. I tried to add \n, but the \n outputs as it is, without being escaped.
How to add a new line character in break point's action?

Here are four things for you to try:
You can produce a line break using the debugger expression {"\n",s8b} which makes use of the C++ debugger format specifier s8b (unquoted 8-bit string).
Here's an example with a two-line message First{"\n",s8b}Second:
(Other than that, I am not aware of any other way to include line breaks in the message. While there are ways to enter a multi-line message (by entering line break characters' Unicode code points using the numpad), Visual Studio will just throw away everything but the first text line entered.)
Just before your current breakpoint, add an additional breakpoint with a very short action message (a dot or comma) in order to get an additional line break before your real message.
If you're on Windows (which appears likely, given Visual Studio), you can send a message to the debugger using the Windows API function OutputDebugString. This is the currently suggested solution to the SO question, "How do I print to the debug output window in a Win32 app?"
Write a message to clog: std::clog << message << std::endl;.

In Addition to the answer from stakx that matches the original question for debugging C++ applications, I would like to add a character sequence that instead works for debugging .NET applications:
{"\n",nq}
The C++ sequence would otherwise result in this error message: 's8b' is not a valid format specifier

Related

Is it possible to modify the terminal's previously printed and buffered characters?

I will start by saying: I am already aware that I can use '\b' and ansi escape codes to get the cursor position, move the cursor, and get the terminal size.
However, once the-text-I-am-interested-in has scrolled past the terminal's view, it seems impossible to modify said text.
For instance, imagine that the terminal has the following displayed on it:
first line [...]
a
b
c
...
last line
And when printing a new line, let's imagine that "first line [...]" scrolls out of screen, disappearing above "a":
a
b
c
...
last line
inserted line
Would it still be possible to modify the line ('first line [...]'), perhaps using the terminal's own buffer, in C++?
The idea would be to modify the line, such that if you do scroll back up, you would see the modified version (bonus points if you can also still print ansi codes for formatting out-of-screen).

Why does the Windows command prompt say "More? " when I run a program with an argument of d^?

I was making a C++ calculator program that removes unrecognized text from the argument array, and I just so happened to enter d^ when I was running the program. The Windows command prompt then showed "More? ", and I couldn't figure out why, since I didn't script this. I realized it was coming up since I wrote d^, but why does it do this? What does "More? " do?
Note: Compiled in Visual Studio Express for Windows Desktop, if you need to know.
More? "does" nothing. It asks you to continue your command.
^ is an "escape character" that tells the Interpreter to treat the following character different. The following character is a Line End ("Enter") - ^ tells the Interpreter not to handle it as end of line ("Enter"), so it asks you to continue.

C++ Mac OS infinite loop in regex_replace if given blank regex expression

Upon executing...
std::regex_replace("the string", std::regex(""), "doesn't matter");
...my Mac will hang indefinitely. I'm new to xcode, but I think I am using it correctly. I hit "pause" while debugging the program and found the last executed code is deep within the regex library. I need blank regex to work because the user may input blank.
This does not occur on Windows (I'm developing cross-platform).
This does not occur with std::regex_match().
As for the root cause, see the regex.cpp source code at Apple Open Source:
case regex_constants::__re_err_empty:
return "An empty regex is not allowed in the POSIX grammar.";
So, you have to check the user input and disallow passing an empty string to create a regex object.

How do I rewrite a line of text in a console project? c++

I'm working on a c++ console project and i would like to show a percentage without making a new line each time (so that the window doesn't get clogged with thousands of lines).
Is there a way of removing the last line that was printed or something to say that the next time that i output a line it should replace the current line?
You can use a \r (carriage return) to return the cursor to the beginning of the line:
This works on windows and Linux.
From: Erase the current printed console line
You could alternatively use a series of backspaces.
string str="Hello!";
cout << str;
cout << string(str.length(),'\b');
cout << "Hello again!";
From: http://www.cplusplus.com/forum/unices/25744/
Maybe mark as duplicate? I am really not sure how.
A simple example that I tested on Linux would be:
std::cout << "Some text to display..." << "\t\r" << std::flush;
Here the \t adds a tabulation to handle slightly varying string lengths and \r sends the cursor back at the start of the line (as mentioned in other answers).
std::flush is required to guarantee that the line is displayed without jumping to the next line.
This is very platform-dependent and terminal-dependent. But, you may want to look at ncurses for a start: http://linux.die.net/man/3/ncurses
For Windows: How can I overwrite the same portion of the console in a Windows native C++ console app, without using a 3rd Party library?
For Linux: https://unix.stackexchange.com/questions/43075/how-to-change-the-contents-of-a-line-on-the-terminal-as-opposed-to-writing-a-new

Compiled c++ output file displays random character at end of program?

Not sure if this is an appropriate question, but just recently I've noticed that when I run a C++ program in the terminal when it exits it has a % sign after the last output. For example a hello world program says "hello world%". What is this and how do I get rid of it? I'm on OS X, shell is zsh. Unless I am crazy it has never done this until now.
There are two possibilities that I can think of off hand:
1) You aren't printing a carriage return, so the % prompt appears at the end of the printed text instead of on the next line. (Is the % your standard prompt in the shell?)
2) You are printing past the end of a buffer and getting a random character as a result.
I'd guess #1 based on what you describe, but both could cause the behavior.