Beginner having issue with console app

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace 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;
}
 
Last edited on
You should read the stickied post:

Console Closing Down
http://www.cplusplus.com/forum/beginner/1988/

Yes, it is looooong, but there are ideas for making the console window stay open.
Hello rv11265,

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. 


Andy
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).
Last edited on
closed account (z05DSL3A)
rv11265 wrote:
I am using Visual Studio 2019

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:
< "path/to/the/file"
Last edited on
I am using Visual Studio 2019

The real question is "how are you executing the program?"

If you are doing it from the 2019 IDE the console window should display your output and then pause with a message until you hit a key.

1
2
3
4
5
6
#include <iostream>

int main()
{
   std::cout << "Hello World!\n";
}

Hello World!

C:\Programming\My Projects\Project1\Release\Project1.exe (process 13248) exited with code 0.
Press any key to close this window . . .

(The actual address on the 3rd line will be different on your machine and the process listed with be different with each run.

If you are running your program from File Explorer, don't. Use a Command Prompt so when the program ends the window doesn't close.

Just run your program from the VS 2019 IDE: Debug->Start Debugging (F5) or Debug->Start Without Debugging (CTRL+F5).
Last edited on
Hello rv11265,
You may need to add a header file:
1
2
3
#include<iostream>
#include<conio.h>  //the new header file
using namespace std;

Then add a sentance before the function returns like this:
1
2
3
4
5
    cout << "The difference: " << firstnumber - secondnumber  << endl;
    
    getch();   //the new sentance
    return 0;
}

You can try it:)
Last edited on
> #include<conio.h> //the new header file
The 1980's called, and they'd like their DOS back.
1
2
3
4
5
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!
Topic archived. No new replies allowed.