1

I am starting with C++ (Visual Studio 2015 and Windows 8.1), with this simple code:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world" << endl;
    return 0;
}

But, the output screen shows nothing!, what shall I do?

Thanks in advance.

Nima
  • 37
  • 1
  • 1
  • 7
  • 1
    How did you start the program? If you just compile and double click it, it will print and then exit immediately as it is done, too fast to see with the naked eye. Try starting the program from the command line. – Baum mit Augen Dec 04 '15 at 13:42
  • If you are running from Visual Studio, then it will launch the application and it will output the text to the *console window that just opened*, and then the program will exit. You can set a breakpoint on `return 0;`, then you can switch to the console and see the output. You could also open a command prompt and run the program from there. – crashmstr Dec 04 '15 at 13:43
  • have you checked .exe on prompt, without pass from ide ? try to run you rprogram from console (win+x) cmd , and go in directory output of your project –  Dec 04 '15 at 13:48
  • use getch function defined in conio.h – Grv Dec 04 '15 at 13:48
  • I think it will be very helpful to learn basic MS visual Studio debugging. Like putting break point at lines where you want to analyse and then use F5 to reach till that point and reason your output till that point. Moreover I think before you finalize the code always try to work in debugging mode i.e start the program with F5 and have habit of putting breakpoints. – Ritesh Dec 04 '15 at 13:54

5 Answers5

3

In Visual Studio, start the program with Ctrl-F5 and it will run and pause automagically for you. No additional code needed.

nicomp
  • 4,344
  • 4
  • 27
  • 60
1

Your code is perfectly fine but the program currently only prints and exits right after, because this can happen very fast you might not be able to even see it,try pausing it :

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world" << endl;
    cin.get();
    return 0;
}

Also, make sure your Anti Virus isn't blocking Visual Studio.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

Your code is just fine, however, if you execute it as a cmd program, the program window will close immediately, you might not be able to even see the output. You can write extra code to solve this problem by "pausing" the program:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    cout << "Hello world" << endl;
    system("PAUSE");
    return 0;
}

if you don't like include a windows.h file every time you type, you can add a "cin.get();" in the end of the code. But to be honest, since you are just a beginner, the coolest way I think you should try, is not to use Visual Studio to learn C/C++ but to install CodeBlocks(a simple but effective IDE) to write some codes that are not so long. You know, VS is for huge and complex projects and some practical program developing.

A.Z.Young
  • 81
  • 4
0

Another solution, platform dependent. My answer is for those of you who just need test pause for debugging purposes. It's not recommended release solution!

windows

#include <iostream>

int main()
{
    std::cout << "Hello world" << endl;
    system("pause");
    return 0;
}

linux (and many alternatives)

#include <iostream>

int main()
{
    std::cout << "Hello world" << endl;
    system("read -rsp $'Press enter to continue...\n'");
    return 0;
}

Detecting paltform

I used to do this on programming homework assignments, ensuring this only happens on windows:

#include <iostream>
int main()
{
    std::cout << "Hello world" << endl;
    #ifdef _WIN32
        system("pause");
    return 0;
}

Here's a good cheatsheet for ifdef macros and operating systems: http://sourceforge.net/p/predef/wiki/OperatingSystems/

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
0

The program exits on return 0; and window closes. Before this, you must pause the program. E.g you can wait for an input.

Here is a snippet from my code to do this. It works in both windows and linux.

#include <iostream>

using std::cout;
using std::cin;

// Clear and pause methods
#ifdef _WIN32
// For windows
void waitForAnyKey() {
    system("pause");
}

#elif __linux__
// For linux
void waitForAnyKey() {
    cout << "Press any key to continue...";
    system("read -s -N 1"); // Continues when pressed a key like windows
}

#endif

int main() {
    cout << "Hello World!\n";
    waitForAnyKey();
    return 0;
}
user12043
  • 396
  • 1
  • 3
  • 12