Simple "Hello World" code does not appear on Command Prompt - c++

So I have been watching a tutorial (here: https://youtu.be/yKATaptz3Dc) to complete my first line of code. My code is identical to the one in the video yet nothing appears on my console. I'm assuming there is some settings issue in my version of Code::Blocks but I have no idea where to begin. Ideas?
#include<iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}

This is an exceedingly common problem, and almost always goes to this issue:
You are executing your program incorrectly.
A console program expects to be run from a console. Yet you are probably running it from your IDE or double-clicking its icon from Explorer, which causes Windows to create a new console, run your program, and then destroy the console. Sometimes you will see the new console window flash briefly on the screen, sometimes you will not.
To run the program correctly, use Explorer to find your program's exe, click on the location bar at the top and type "cmd" and press Enter. You'll get a console with the current directory at your exe. Type the name of your exe file. Enjoy the output.
Another common response is to add code that tries to do a "Press enter to continue..." kind of thing before return 0;, but that is really not the correct way to do it.
Hope this helps.

Related

Where do you type in console input in codeblocks?

Apologies in advance for the simple question, but where on earth do you type in input into codeblocks? I've searched through google, but I can't find anything.
For example, in eclispe, input can typed in via window->show view-> console. How do I access that in codeblocks? Thank you.
Cue me looking dumb in 3...2...1...
In CodeBlock when you build and run your code, a new terminal window opens (in windows a new console window) and you can enter your inputs there. it is not like eclipse that you enter your inputs in console view.
Go to File->new->Project->Console Application.
Type in the name of the project and just click forward to create it.
The project should come premade with a Hello World example. Just run it and you will get the console to appear, that is where you will provide both standard input and output.

Outputting from a .exe to cmd

I am writing a little program in c++ which creates an .exe which i then run by calling it with parameters in cmd. I want to be able to display output from the .exe into the cmd that I ran it from. I currently have this code which opens a new cmd window to display output which is close but not what i want. any help with this would be great! thanks.
AllocConsole();
DWORD NumberOfBytesWritten = 0;
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), strLog1, lstrlen(strLog1), &NumberOfBytesWritten, 0);
Update:
I have also been able to write to a text file using dir > log.txt in the command window when calling the program, is there a way I can change this so that it directs outputs to the console window? Thanks,
My psychic debugging powers tell me that your build tools are configured to create your application in GUI rather than console mode.
If you reconfigure the build so that it generates a console mode application, you won't need to call AllocConsole or do anything special; you'll automatically be assigned to the console of the parent process.
Did you try simple operations, such as:
std::cout << "Print me" ;
or
std::cerr << "Print me too";
?
(I hope I understood correctly that you want to print to the same console where you started your app)
That question has already been asked: How to output to the console in C++/Windows. Here is an answer that seems to be useful in your case: https://stackoverflow.com/a/587792/1728537

Key logger wont record key strokes without console

I created a small basic key logger in C++. For some reason when I compile and run the program with the console displayed, it will record every key stroke I make in whatever program I am using such as a browser and store it in a text file. However when I make it so that it WON'T display a console window, it will not record anything and it's just a process in the background doing nothing. Here is the link to my code: http://pastebin.com/4wqQyLJ9
The function that is giving me trouble with hiding the console, is the Stealth() function. Any suggestions, tips or hints will be helpful.
Use this function , it works for me pretty well.
ShowWindow(GetConsoleWindow(), SW_HIDE);
Instead of hiding the window after the program starts, I solved this by not
having a window to begin with. Compile with -mwindows and a window is not
created when the program starts.
Example
I would consider a Windows Service for this kind of thing if you don't need UI. Also using GetAsyncKeyState can be more stealthy if required. This C++ source might be of use...
Windows Service Keylogger

C++ on Windows - the console window just flashes and disappears. What's going on? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Visual Studio Console App - Prevent window from closing.
I'm starting to learn C++ on Windows and I'm trying a few different development environments:
1. Netbeans with Cygwin compiler
2. MS Visual Studio 2010
For either of them, when I write a very simple Hello World program, I build it and it's fine. But when I try to run the program, the command prompt window pops up really quick and then disappears right away.
This happens whether it's in Debug or Release config. Please help with this - I can't see my program output! :(
Thanks.
EDIT1:
Thanks for the replies. This is my code:
#include <iostream>
int main()
{
std::cout << "This is a test." << std::endl;
return 0;
}
I tried Ctrl+F5 for "Start without Debugging" and that doesn't work. It still flashes the black console screen then disappears right away.
I also tried adding in std::cin.get(); and this works with Ctrl+F5, but isn't that a really... inelegant workaround solution? I'd rather have my program in the final form.
The breakpoint works, but then I have to run with debugging and the console window flashes and disappears, but then it stays in the background. Any way to get the console to stay in the foreground so I can see program output right away? Seems like that's how it should work.
Any more ideas? Why wouldn't Ctrl+F5 work?
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Write cin.get() at the end of the program.
use Ctrl+F5 to run your program or set a break point in the last line or write cin>> to any vraiable at the end....etc
I think your program just prints Hello World and then exits. That's the reason the console closes immediately. You can run the executable from Command Prompt (Start Menu > Run and type cmd.exe). Otherwise, you can put std::cin.get() in your code so that program waits for user's input and hence the console window remains open until a key is pressed.
Your application is probably working. Make the last command in your console application wait for user input: e.g int i;
string i;
cout<<"Hello";
cin<<i;
Issue a getchar() before returning or run from cmd.exe

Visual C++ Enable Console

I created an Empty Project in Visual C++, but now I need the Console to display debug output.
How can I enable the Console without recreating the project or show the output in the VS output window?
Here's some code you can insert to get a console window in a GUI'd windows app that starts in WinMain. There are other ways to accomplish this but this is the most compact snippet I've found.
//Alloc Console
//print some stuff to the console
//make sure to include #include "stdio.h"
//note, you must use the #include <iostream>/ using namespace std
//to use the iostream... #incldue "iostream.h" didn't seem to work
//in my VC 6
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf("Debugging Window:\n");
You can always call AllocConsole in code to create a console for your application, and attach it to the process. FreeConsole will remove the console, detaching the process from it, as well.
If you want all standard output stream data to go to the console, you need to also use SetStdHandle to redirect the output appropriately. Here is a page showing working code to do this full process, including allocating the console and redirecting the output.
You can write to the vs output window with OutputDebugString. http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx