Why is my C++ code outputting NSString literals rather than plaintext? - c++

I know this question seems odd, as NSString doesn't exist in C++. Yet, for some reason, when I run the following code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
I see the following output:
#"Hello, World!\r\n"
The C functions printf and puts show similar behavior. I also see the same output if I store the text as a std::string and then display that.
I have no idea how to even start troubleshooting this. What is going on?

I figured it out a while ago but I forgot about my own question.
In the debug window, it just displays output like that. It only gets formatted like that in debug mode; if I compile it and run the executable from the command line, then it works as expected.

Related

C++ console output not switching to a new line when the program ends [duplicate]

This question already has answers here:
Most efficient way to output a newline
(7 answers)
Closed 6 months ago.
So I'm just starting to learn C++, and I'm just trying to get everything set up. When I run the compiled binary for my hello world code, it display's the console output, but doesn't switch to a new line afterwards. Here's an example:
Hello, World!michaela#michaela-HP-Laptop-17-by4xxx: $
I tried researching, but I can't find a good solution. I'm on Ubuntu 20.04, and using the BASH shell. I will provide the code if it's any help, though I doubt it as it literally just outputs "Hello, World!".
#include <iostream>
int main(){
std::cout << "Hello, World!";
return 0;
}
This is completely normal for the majority of programming languages, especially in a Unix environment. Even shell scripting will print without a newline if you use printf or echo -n. And thanks to the terminal emulating an old teletype-like printer-based system the cursor won't reset just because a program exitted.
If you want a newline add a '\n' to your string or output a std::endl after your text.
try this :
std::cout << "Hello, World!"<<std::endl;
or you can use this one :
std::cout << "Hello, World!\n";
The only difference between std::endl and \n is that std::endl adds std::flush (to flush the output stream), which is usually totally unnecessary and makes the program slower.

C++ and PowerShell: std::cout weird spacing with ANSI colors

I'm facing a weird issue with std::cout, ANSI color codes and PowerShell (integrated terminal in VS Code).
I have written a sample program that asks the user for string input. In the prompt I make use of ANSI color codes, so the default value (in case the user doesn't input anything) is displayed gray instead of white.
Running the executable works as intended the first time, but when running it a second time there's a weird spacing that disappears after the user presses Enter.
This issue persists until the code is compiled again.
I have noticed that when I leave out the ANSI color codes, this spacing also disappears. I'd still like to use colors though.
Please see below for sample code and images showing the output.
What causes this issue? Is there a different way of using colored std::cout output in C++?
Thanks a lot for your time.
Sample code
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Input: \x1b[37;2m(default value)\x1b[0m ";
std::getline(std::cin, input);
if(input.empty()) {
input = "default value";
}
std::cout << input << std::endl;
return 0;
}
Running the executable for the first time
Running the executable thereafter
Additional information
OS: Windows 10
Terminal: VS Code Integrated PowerShell
C++ compiler: g++ 8.1.0

Why is there no Console Window showing up whilst debugging

Alright so I have been trying to fix this for a while, but with NO SUCCESS, I've come here to search for help instead. I'd appreciate it!
So basically, when I debug with gdb it should give me a console window saying "Hello World" (cause that is what I wrote in my code) right? Well you guessed, there is no console window for me.
This is the code i've written:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
This is supposed to give me an "Hello World" output in the console window but it is not, cause the console window is not even there when I debug. I've double checked almost everything and I can't find ANY SOLUTION at all.
Thanks for listening as this is very frustrating for me.
If you are using VSCode in launch.json file with a configuration of debug execution you can use
"externalConsole": true
which allows you to see your output not in the embedded terminal.

no "hello world " output (c++)

If I run the code::blocks default console c++ "hello world" app (see below), I only see this in the console that opens :
Process returned 0 (0x0) execution time : 0.011 s
Press any key to continue.
I don't see "hello world". What could be wrong ?
If I run the ./helloworld.exe with cygwin, I do see "hello world". But I don't see it with cmd or powershell.
The app :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
There is a related discussion you might want to check out here:
http://www.tomsguide.com/forum/244674-49-basic-program
It could be a matter of the program executing too quickly, per the forum I referenced above, but that seems like an odd reason. You might try what they recommend (pause, etc.), but you may also have an issue with your compiler or how your properties are set up.
For cmd, check this out:
https://www.thecrazyprogrammer.com/2015/09/how-to-run-c-and-cpp-program-in-cmd.html
For the command line/Windows prompt, check this out:
https://msdn.microsoft.com/en-us/library/ms235639.aspx
Thanks to #George.
Solution was to remove the -mwindows compiler flag.
But to make it work, I needed to delete the .exe before building again.

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.