-1

I'm trying to code in C++ and this is what it is

#include <iostream>
using namespace std;

int main()
{   cout<< "Welcome to C++ programming";
    return 0;
}

there is no output showing up. It just says Program ended with exit code: 0 THis is the first time I'm coding in C++ and I'm using xcode.

  • 2
    [Duplicate?](http://stackoverflow.com/questions/36218138/xcode-debugger-not-showing-c-cout-output) – François Andrieux Apr 05 '17 at 19:28
  • 1
    Does adding a newline help? `<< std::endl;` – tadman Apr 05 '17 at 19:29
  • Are you running it from a shell or from a GUI? If from a GUI then it probably runs, shows the output in a console window that then immediately closes when the program terminates. Try running it from a console/shell and you should see the output. – Jesper Juhl Apr 05 '17 at 19:32
  • Possible duplicate of [Xcode debugger not showing C++ cout output](http://stackoverflow.com/questions/36218138/xcode-debugger-not-showing-c-cout-output) – RazaUsman_k Apr 05 '17 at 19:59

3 Answers3

1

Your program exits right after typing "Welcome to C++ programming".

cout << "Welcome to C++ programming";

So you should add

system("pause")

before

return 0;

And your final code will look like this

#include <iostream>
using namespace std;

int main()
{
    cout << "Welcome to C++ programming";
    system("pause");
    return 0;
}
Serid
  • 354
  • 6
  • 13
0

First, your application is exiting with 'exit code 0' which is being applied because of your 'return 0' statement at the end of the application.

I copied your code and ran it in Visual Studio 2017 on a windows machine and your code worked fine with one exception.

It opens the console...runs the code...and closes it immediately because this is what your code tells it to do.

There are multiple ways around this.

  • One: add a (require user input) option to continue, this can be through the use of system("pause") or a variety of cin.get(), etc. The use of 'system' is not encouraged as it leads to non portable code. This method is not encouraged as the issue you are experiencing would not occur when your application would be run from a real command line, ie closing upon completion of the code segments.

  • Two: your application, and others like it hopefully have a 'leave command line open after execution' option or a similar setting.

    • For xcode 4 follow these steps to accomplish this: open the 'Behaviors' tab in 'Preferences' and under 'Build Starts' (make sure that's checked", enable the "Show" debugger with "Console View" section. example of preferences window

    • for xcode 3: xcode preferences, under building select the 'Build Results Window', Open duing builds. set to Always

bcm27
  • 145
  • 8
0

Add a new line after the output statement:

#include <iostream>
using namespace std;

int main()
{   cout<< "Welcome to C++ programming" << endl;
    return 0;
}

If the console still doesn't appear, press shift-command-C to reactivate the console.

EDIT:

I should also note that your problem will be different in different IDEs. In Visual Studio, it will work, but the console will disappear, that's why the other answers here mentioned putting system("pause") or other pausing functions at the end. In CLion, it works perfect as is. In Xcode, the output needs to end with a newline.

Jish
  • 92
  • 5