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

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C++ program where the result window closes immediately after execution, preventing the user from viewing the output. Participants explore potential solutions and the underlying reasons for the behavior observed in different development environments.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • The original poster describes an issue with a C++ program where the output window closes too quickly to see the results.
  • One participant suggests including #include <conio.h> and using getch(); to pause the program before it exits.
  • Another participant expresses uncertainty about the appropriateness of using conio.h and mentions that the issue persists even after applying getch();.
  • A later reply confirms that adding getch(); resolves the issue, allowing the user to see the output.
  • One participant questions why this solution is not mentioned in the resources they are using.
  • Another participant speculates that the use of getch(); is not considered "proper" C++ and explains its functionality in terms of waiting for user input.
  • Discussion about the behavior of running programs in different environments, particularly Visual Studio, where the method of execution (F5 vs. Ctrl-F5) affects whether the output window remains open.
  • Clarification that running the original program in a command prompt should yield the expected behavior, contrasting with the IDE experience.
  • One participant reiterates that the reason for the lack of mention in textbooks may relate to best practices in programming.

Areas of Agreement / Disagreement

Participants express a mix of agreement and disagreement regarding the use of getch(); and the implications of its use in C++. There is no consensus on the appropriateness of this method, and multiple views on IDE behavior and best practices are present.

Contextual Notes

Some participants note that the behavior of the output window can vary significantly depending on the IDE or method of execution used, indicating a dependence on specific environments.

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;

count << 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 occurred
 
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 ·
2
Replies
40
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 6 ·
Replies
6
Views
1K
Replies
12
Views
3K
Replies
53
Views
5K
Replies
73
Views
6K
  • · Replies 118 ·
4
Replies
118
Views
10K