1

I have been told to make an exe file that will output 5 lines. I have been given these instructions:

Your program must not hang after running. I will execute it at command-line and will expect it to finish when the program is done.

I tried going to properties -> linker -> system - > console. It seemed to do what he wanted when I ran ctrl-f5. I then made an exe and now it disappears, and it doesn't print to the console.

I am using a simple cout program ex:

int main()
{
    cout<<"hello"<<endl;
    return 1;
}

Edit I'm using visual studio 2013, and I am running from the command line. Note I'm not asking for the window to stay open, but printing to the console itself. I am not pressing ctrl-f5, but going to cmd.exe and then to the executable. I have tried the release version as well as the debug version.

zissler
  • 107
  • 1
  • 1
  • 11
  • 2
    [this has already been answered](http://stackoverflow.com/questions/4118073/how-to-stop-console-from-closing-on-exit) Please search before posting. – erapert Oct 19 '16 at 19:48
  • 1
    ***I then made an exe and now it disappears*** If you are running your application from exporer this is expected. Windows will close an application immediately after it is finished. Run it from a command prompt instead. – drescherjm Oct 19 '16 at 19:55
  • ***but printing to the console itself.*** Since you are using `c++`, I assume you are using `std::cout`. Nothing special is needed for a native c++ console application to print to the console. Maybe you should post your ~10 line program. – drescherjm Oct 19 '16 at 19:57
  • I am using cout, I think it's more of a visual studio problem. – zissler Oct 19 '16 at 20:01
  • Using an empty project caused the problem. Using #include "stdafx.h" sent output to the console when I ran it. – zissler Oct 19 '16 at 20:04
  • I did several searches and nothing came up for this issue, only to use getchar or cin. – zissler Oct 19 '16 at 20:05
  • You mean when you press `ctrl-f5` this window stays open but does not print anything in the console window? If so try disabling your antivirus. aVast is known to prevent unknown applications from running (meaning any application you write) – drescherjm Oct 19 '16 at 20:06
  • Possible duplicate of [Preventing console window from closing on Visual Studio C/C++ Console application](http://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio) – OldProgrammer Oct 19 '16 at 21:05
  • If you tell me to not taking time to search or look for duplicates I would ask you to take time and read what I have posted. I ask for the same respect you ask of me. – zissler Oct 19 '16 at 21:09
  • I don't want the cmd window to stay open I want the text to go to cmd.exe – zissler Oct 19 '16 at 21:09
  • Did you try disabling your antivirus? – drescherjm Oct 19 '16 at 21:15
  • ***read what I have posted*** Your original explanation of your problem was very confusing. Well at least to me. You have edited it a few times and its somewhat better now. – drescherjm Oct 19 '16 at 21:16

1 Answers1

0

If I have understood your question. Try following and add useful parts to your code. Not needed lines are commented out.

// ConsoleApplication1.cpp : Defines the entry point for the console application.

// (Just 2 ways to wait, befoore command-line console vanishes.)

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "It's Foo's Bar!\n";
    system("pause"); //preferred way.
    //cin.get(); //2nd way, not as good.
    return 0;
}
  • This is not the issue. The duplicate was removed for the issue that you are solving. The issue is for some reason the cout does not print to the console even when the executable is run in a cmd.exe window (so it will not close). – drescherjm Oct 19 '16 at 21:18
  • Based on what zissler wrote an hour ago. And I tried code snip I show here. I need both #include"stdafx.h" and #include, and – Johan Lund Oct 19 '16 at 22:02
  • You don't need `#include"stdafx.h"` unless you have precompiled headers enabled. You do need `#include`. – drescherjm Oct 19 '16 at 22:05
  • ***Based on what zissler wrote an hour ago.*** The question was confusing.. – drescherjm Oct 19 '16 at 22:06
  • "using namespace std;" to make the cout work. Pause or not. I have Visual Studio 2012 Ultimate installed here. Please, try at least (make again). – Johan Lund Oct 19 '16 at 22:14