I am trying to write a simple console app that does calculations
I have done some other console apps that work fine
This app prompt for the numbers then displays the results very quickly.
This app does not stop after displaying the results it just flashes the answer and ends
I am using Visual Studio 2019
#include <iostream>
usingnamespace std;
int main()
{
int firstnumber;
int secondnumber;
cout << "Give me a number: ";
cin >> firstnumber;
cout << "Give me a second number: ";
cin >> secondnumber;
cout << "The sum of the two numbers: " << firstnumber + secondnumber << endl;
cout << "The difference: " << firstnumber - secondnumber << endl;
cin.ignore();
return 0;
}
You either need to set your unknown IDE not to close the console window when the program ends or you could add this before the "return".
1 2 3 4 5 6 7
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
I did read the stickied post, but I thought one of the solutions was to add
cin.ignore();
just before the return 0;
and I did that and it still did not pause
just recycle a cin, eg put
cin >> firstnumber;
right before return 0. you can type something here to close it after taking a look.
a better way is to run the program from a console:
start, run, cmd
cd "path and stuff\more path\visual etc\"
run it from there (by typing the programs name)
the issue is it opens a temporary console, runs, and closes the console. If you run it from your own console, you can not only see the output, you can capture the output to a file and send input into it from a file (saves testing by retyping the same junk over and over).
Go to tools->options, find Debugging->General and look for 'Automatically close the console when debugging stops' and make sure it is unchecked, the console should then stay. *
Edit
* ...or check it there may have been a UI bug in some version where it had the opposite effect.
Edit 2
jonnin wrote:
If you run it from your own console, you can not only see the output, you can capture the output to a file and send input into it from a file (saves testing by retyping the same junk over and over).
It's simple enough to create a file with your test data and set the debugging category in project properties to pipe the file to the input stream. [In the "Command Arguments" property type:
cout << "The difference: " << firstnumber - secondnumber << endl;
getch(); //the new sentance
return 0;
}
Don't try to fix the issue with the wrong solution. The problem is the console window is closing when the program exits. The fix is a change to the VS2019 IDE option as others have mentioned previously - not some un-needed additional code in every program you write!