Why is there no Console Window showing up whilst debugging - c++

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.

Related

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

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.

Visual Studio 2017 causing problems

I'm a complete begginer when it comes to programming. I'm trying to learn using the book "Programming: Principles and Practice Using C++" and it recommended downloading Visual Studio to code (I was previously using Codeblocks).
I downloaded the newest version (2017), got the classic applications packet for C++ and created a project.
But when I wrote my first code
#include "stdafx.h"
#include "../../std_lib_facilities.h"
int main()
{
cout << "Hello world!";
keep_window_open();
return 0;
}
Visual Studio started to freak out. When I click the local debugger button, the console app goes to the taskbar, and when I click on it, I can't do anything with it. It jsut displays "Hello World!" but no "Please enter character to exit" as it should. It doesn't react to keyboard input. I can't close it (even if I use task manager and try to close the process manually). Only solution I found is clicking "continue" button in Visual Studio, but it's not very convenient to use as the app still doesn't quite work the way I want to. Is there a way to make it work like it does in codeblocks - I just put in code, click a button and I have a fully functional console app?
One thing that could cause it - I was getting an error about using , so following advice from the internet I added
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS 1
at the beggining of the header std_lib_facilities.h
Any way someone could help me out? (Maybe I should just go back to Codeblocks and ignore Visual Studio? That doesn't seem like a good way though)
If you only want to have the behaviour of "Please enter character to exit", you could just include the cstdio header and use std::getchar() at the end. This will make sure the program does not exit until you press any key on the keyboard.
However, this method does not print out the message "Please enter a character to exit" so you would need to print it yourself using cout.
The main.cpp would end up looking something like this:
#include "stdafx.h"
#include <cstdio>
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
std::cout << "Please enter character to exit" << std::endl;
std::getchar();
return 0;
}

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.

C++ in VSCode - Getting Started

I'm having trouble getting a simple C++ script to run in VSCode (I'm new to both). I followed these instructions, and the status bar displays "C++" on the bottom right of the screen, next to the smiley face. I then ran the following script:
#include <iostream>
using namespace std;
main()
{
cout << "Hello World!\n";
return 0;
}
When I run it, the script's path flashes on the output screen and disappears. I expected it to display "Hello World" in the output.
I can run the script from the command window (I'm in Ubuntu) and the output file behaves as expected when executed.
Your program prints message to STDOUT and exits. Add some kind of wait (you can read STDIN for example) if you want to see its output.
PS: Why do you call your program "script"?

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.