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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 2K views
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
 
Physics 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.