C/C++ Troubleshooting a C++ Program: Result Window Not Staying Up Long Enough

AI Thread Summary
The discussion revolves around a common issue faced by beginners in C++ programming, where the output window displaying results closes immediately after execution, preventing users from seeing the output. A user shared a simple C++ program that successfully performs a calculation but does not allow time to view the result. The solution provided involves adding the `#include <conio.h>` directive and using `getch();` before the program returns 0, which pauses the program until a key is pressed, allowing the user to see the output. The conversation also touches on the differences in behavior when running programs in various environments, particularly in Visual Studio, where using F5 runs the program and closes the window immediately, while Ctrl-F5 keeps the window open until a key is pressed. It is noted that the use of `getch();` is not considered "proper" C++ practice, as it is not commonly included in educational resources, but it serves as a practical workaround for beginners to view their output.
Heisenberg.
Messages
68
Reaction score
0
I have been creating simple programs from the hello world one to simple math and loops - all of a sudden when I went to practice more today my programs would be completed but the window showing the result would pop up and almost instantaneously close itself - in the end I can't see the result, here is a simple example where code seems fine no errors are shown but the result window is not staying up long enough:

#include <iostream>
using namespace std;

int main ()
{
int a, b;
int result;

a=5;
b=3;
result=a+b;

cout << result;

return 0;
}

I am new to c++, so I have no idea where to even look to fix this problem - it seems like the result window is not staying up long enough - any ideas? and please inform me if I am not being clear enough
 
Technology news on Phys.org
Add #include <conio.h> and use 'getch();' right before 'return 0;'. getch() waits for a character to be entered (and returns the value of it).
 
hm conio.h ? but I did apply the getch and the same issue occured
 
I added the two lines. Now it should work.

Code:
#include <iostream>
#include <conio.h> // I added this

using namespace std;

int main ()
{
int a, b;
int result;

a=5;
b=3;
result=a+b;

cout << result;getch(); // I also added this

return 0;
}
 
awesome, it worked, Thank You! my question is why is neither the website I am using nor the book I have include that bit?
 
I don't know, but I think it is because it is not "proper" C++ to do it this way. getch(); is a command which returns the value of the next character to be typed, which is why it exits when a new character is typed in. It just happens to give us what we want, i.e. time to see the result of our code.

Good luck.
 
OS? IDE?

Under Visual Studio what happens depends on whether you run your program using F5 or Ctrl-F5. I never remember which one is which - but one stops displaying the window after the execution ended and waits for anykey. And it has nothing to do with C or C++.
 
If you run your original program in a command prompt, it should work as expected. OTOH, if you run your program in an IDE such as Visual Studio, the IDE will open a separate command window, display the result, and then close.
 
Borek said:
OS? IDE?

Under Visual Studio what happens depends on whether you run your program using F5 or Ctrl-F5. I never remember which one is which - but one stops displaying the window after the execution ended and waits for anykey. And it has nothing to do with C or C++.

It is Ctrl-F5, which pauses.

And I did not say it has anything to do with C or C++, but that the reason why my suggestion is not mentioned in a book is because it is bad practice.
 

Similar threads

Replies
40
Views
3K
Replies
22
Views
3K
Replies
5
Views
3K
Replies
39
Views
4K
Replies
25
Views
2K
Replies
6
Views
1K
Back
Top