1

My IDE is Visual Studio 2017. I am pretty new in C++ programming so I need a help about understanding principles of creating a new C++ project in Visual Studio. So, in my first solo attempt i just chose a empty project option and afther that i chose to add new item and i write this sample code:

#include <iostream>
using namespace std;

int main()
{

    return 0;
}

Afther this step and afther steps with compiling, building and a starting without debugging i did not get any message or consol window with time of code execution or option for entering any key for ending. What is needed for getting this kind of information at the end of code?

  • 5
    Go to Tools->Options->Debugging and look for an option called "Automatically close the console when debugging stops" and ensure that this option is not activated. – Blaze Jan 29 '19 at 14:30
  • you can also add `std::cin.get();` before returning from main. but this won't work when there had been any Input with `std::cin` – Stack Danny Jan 29 '19 at 14:31
  • 3
    https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio – drescherjm Jan 29 '19 at 14:32
  • 2
    As a side note: don't use `using namespace std;` see here for more information: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Philipp Jan 29 '19 at 14:37
  • For example in Visual Studio 2015 i can chose Windows Console Application option or creating project and afther chosing that step i also get a App wizard where i can chse what i want to include in project(precompiled headers) or just empty project so in Visual Studio 2017 i do not have a chance to start that app wizard. –  Jan 29 '19 at 14:52
  • Visual Studio 2017 does not appear to give you the extra prompts. However you can easily make the changes to your project settings after the project is generated. – drescherjm Jan 29 '19 at 14:58

2 Answers2

0

You shouldn't use system("pause"); you can read here why. It's platform dependent and adds a huge overhead loading all the Windows specific intstructions.

So you should choose nicer alternatives: std::cin.get() for example. Which will work most of the time. Well, except if there had been input before (std::getline or std::cin). If you're creating a program with user input - use std::cin.ignore() twice to guarantee a "press enter to continue" effect:

#include <iostream>

int main() {
    int a;
    std::cin >> a;
    std::cin >> a;
    std::cin >> a; //etc

    std::cout << "press enter to exit - - - ";
    std::cin.ignore(10000, '\n');
    std::cin.ignore(10000, '\n');
    return 0;
}

also please don't use namespace std; read here why.


If you don't like this 3-liner (because it looks ugly) you can pack it in a void function and treat the whole thing as a black box:

void pause() {
    std::cout << "press enter to exit - - - ";
    std::cin.ignore(10000, '\n');
    std::cin.ignore(10000, '\n');
}

int main(){
    pause();
    return 0;
}
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
-1

Converting Blaze's comment to an answer

Go to Tools->Options->Debugging and look for an option called "Automatically close the console when debugging stops" and ensure that this option is not activated.


I did not get any message or consol window with time of code execution or option for entering any key for ending

Because you didn't ask for it.

What is needed for getting this kind of information at the end of code?

To perform input (see std::cin and operator<<) and output (see std::cout and operator>>). Example:

#include <iostream>

int main()
{
    std::cout << "Press enter to terminate\n";
    std::cin.get();
}
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
YSC
  • 38,212
  • 9
  • 96
  • 149