This question already has answers here:
How to keep the console window open in Visual C++?
(23 answers)
Closed 4 years ago.
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?
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;
}
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();
}
Related
I am trying to get the first program working from "Accelerated C++".
I was having trouble getting the program to stay open without shutting down, so I decided to put a int i = 0; and cin >> i; after main() returns. Unfortuantely, it doesn't seem to take any input, no matter where I put that cin statement.
If it helps, it is using an istream reference to accept cin input. I can't figure out how to enter code on this site.
For most of my adult life, I used system("PAUSE") without any problems to keep the program window open. It's not good for real-time systems, of course, but it's simple and powerful because you're actually running a console command and it can be used to make console scripts.
#include <iostream>
#include <cstdlib>
using namespace std;
inline void Pause () { cout << "\n"; system ("PAUSE); }
int main () { Pause (); }
This solution is not 100% portable, but it will work on PCs. A more portable solution is:
#include <conio.h>
#include <cstdio>
void Pause() {
cout << "\nPress any key to continue...";
while (_getch() < 0)
;
}
cin is good for doing simple things, but what I do is wrap the std library in C functions and use those because it can dramatically improve compile times to hide the std library headers in the implementation files.
The prefered method in C++ is std::getline() using std::string, though many teachers won't let you use that.
With cin, you also have to clear input errors and use ignore() to throw away a specific number of chars.
#include <string>
string foo;
cout << "Why do they always use foo? ";
getline (cin, foo);
cout << "You just entered" << foo;
int bar;
cout << "\nThe answer is because programmers like to go to the bar."
"\nHow many times have you seen foo bar in an example? ";
while (!(cin >> bar)) {
cout << "\nPlease enter a valid number: ";
cin.clear ();
cin.ignore (10000, '\n');
}
My many years of experience have taught me to put the \n char at the beginning of output lines as opposed to the end of them.
Hi I'm a highschool student in need of help. I am interested in C++ as I wish to become a programmer one day. I started using Visual Studio for C++ and I'm running a few simple lines of code. But everytime I press (Ctrl + F5) or Local Windows Debugger it shows my line of code but it closes out immediately after it runs, making it near impossible to read. The code is the classic "Hello World!" code. Maybe it has to do something with return 0;?
EDIT: Here's my code.
#include <iostream>
using namespace std;
int main()
{
int mark = 90;
if (mark < 50) {
cout << "HES OLD" << endl;
}
else {
cout << "Hes not old!" << endl;
}
}
When making console applications, I use the following lines at the end of my main function:
std::cout << "\nPaused. Press Enter to continue.\n";
std::cin.ignore(100000, '\n');
The idea is to display a prompt and then wait for the Enter key to be pressed.
I used visual studio 2013 two days ago.
But now... It does not working.
I tested below c code and cpp code.
//#include <stdio.h>
#include <iostream>
using namespace std;
void main(){
//printf("hi");
//cout << "test" << endl;
int a;
cin >> a;
cout << a;
}
The result of run is just blinking console.
And does not appear "Press Enter..."
result
If I try to debug then Visual studio stop and no response.
I do reset all options :(
Your console is showing the output but not waiting for you to see it.
you need to add break point or for workaround you can use
cout << a;
int ch = std::cin.get();
}
you must be able to see the value of a.
EDIT: ohh you would need fflush too, my fault too miss it.
cout << a;
fflush(stdin);
int ch = std::cin.get();
}
I tried this:
main() {
int a;
cout << "Enter a number: ";
cin >> a;
cout << a;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
But it didn't work.
You don't need to modify your source in order to do this. This tends to be annoying when you exit the program from other places with exit() or abort(). Most IDEs have an option to keep the console open. Are you using Dev-C++ by any chance? It has an option to pause the console. You can find that option in the environment settings. Unless you're using the outdated version of Dev-C++ from Bloodshed. If so, you should update to the Orwell version: http://orwelldevcpp.blogspot.com
The easiest way is to simply place:
system("PAUSE");
wherever you want the pause to be (in your case, in the line above return 0;)
However due to lots of security issues, most would consider the use of system to be bad practice. Instead, try using:
cin.get();
I've always been a fan of using:
std::cout << "Paused. Press Enter to continue.";
std::cout.flush();
std::cin.ignore(100000, '\n');
I am currently learning C++ for a programming course. I noticed that my professor's program automatically closes at the end of their program. It usually prompts the user for an input, and then upon entering an input, the program closes. How would I code that? I only know using return 0 gets me "Press Any Key to Continue"
Note: this is a .exe file
If your program doesn't wait for any input, it runs and finally exits from the program. On exiting, the console automatically closes. I assume, to run the program you're clicking on the .exe, as opposed to runnng the program from cmd.exe, or you run the program from visual studio itself without debugging.
You could just put the following line before return 0;:
std::cin.get();
It will wait for some input and then proceed.
use getch(); before return; statement
Return 0 to give "press any jey to continue" is debugger-specific behavior. Running your compiled exe outside the debugger usually wont show that.
The simple code below does a little more than you're asking for (it repeats what you typed in) but still gives the general idea.
#include <iostream>
using namespace std;
int main() {
cout << "enter something" << endl;
string stuff;
cin >> stuff;
cout << "You entered " << stuff << " you insensitive clod" << endl;
return 0;
}
it is easy, at the end of your main() function put this:
int x;
cin >> x;
this define a new variable and tries to fill it with user-input and then the program will not be terminated till the user gives it input. This is how the program reaches the Press any key to continue, finally you exit the program with a 0 argument and the console window will be destroyed automatically since it is the main window of the process.
I recommend to use:
std::cin.clear();
std::cin.sync();
std::cin.get();
cause there may be times when you need to write something and you will need to press ENTER which will make
std::cin.get();
useles. As it will remeber the first time you pressed ENTER and close the window.
Sample:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Your name: ";
std::cin >> name; \\ <--Place where you press ENTER <--------------
std::cout << "Hi, " << name << ".";
std::cin.get();
return 0;
}