1

I am a beginner in C++ and I was trying to write a program that finds the average of two numbers, but when I run the program, the window disappears without allowing me to see the result. Can someone please help me? Thanks

 #include <iostream>
 using  namespace std;
 int main()
 {
 int number1,number2,answer;
 cout << "number1? ";
 cin >> number1;
 cout << "number2? ";
 cin >> number2;

 answer = (number1 + number2)/2;

 cout << answer<<endl;
 return 0;
 }
FranXh
  • 4,481
  • 20
  • 59
  • 78

7 Answers7

6

Solution #0 (proper):
Run program from shell (cmd.exe, bash)

Solution #1 (proper):
Run program from orthodox file manager. Far Manager or midnight commander.

Solution #2 (alternative);
Redirect output to file. program.exe >file.txt from command line.

Solution #3 (improper):
Launch message box, use "sleep"/"Sleep" to delay program termination.

Solution #4 (improper):
Request user input at the end of program.

Solution #5 (improper):
Set breakpoint on "return 0", debug the program.

Solution #6 (windows+msvc):
Launch program from msvc by Ctrl+F5 using debug build. You'll get "press key to continue" prompt.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
3

Put a breakpoint at your return statement. It won't stop on an uncaught exception but that can be fixed with a try/catch block at the outermost part of main.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

Solution #6 from SigTerm (above) is not guaranteed to work. In Visual Studio you should right-click your project and choose:

Properties | Configuration Properties | Linker | System | SubSystem

and in the dropdown choose Console (/SUBSYSTEM:CONSOLE).

Michal Hosala
  • 5,570
  • 1
  • 22
  • 49
1

Include this header:

#include <stdio.h>

And add a call to getchar() before you return:

cout << answer<<endl;

getchar(); // wait for user input

return 0;
karlphillip
  • 92,053
  • 36
  • 243
  • 426
1

Before you return add system("PAUSE"); and this should fix your problem.

Vania
  • 51
  • 4
1

If you are using Visual Studio, hit CTRL+F5 to run. That inserts a "Hit RETURN to continue" for a console application.

cdarke
  • 42,728
  • 8
  • 80
  • 84
0

I always hated that...I always find the easiest to be system("pause"); right before you return, but that's not a portable solution (cin.get is though). There are many other ways as well, some of which are mentioned in the link below.

http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787

Edit: system("pause"); is terrible, should never be used, and may or may not end life on this planet as we know it, even if by a beginner in a project called 'Hello'. Using system("pause") is expensive and dangerous, and if you use it, you'll never see anyone you love again. Don't do it. Use cin.get or something else.

prelic
  • 4,450
  • 4
  • 36
  • 46
  • 1
    Blech! http://stackoverflow.com/q/1107705/10077 and http://www.gidnetwork.com/b-61.html – Fred Larson Feb 24 '12 at 18:40
  • You sarcasm is appreciated. But seriously, why use something as lame as `system("pause");` when `cin.get()` is standard, portable, and works just fine? – Fred Larson Feb 24 '12 at 18:57
  • 1
    @FredLarson - Probably because people like me keep propagating `system("pause")` when there are better solutions. The next time someone asks, I will be directing them to `cin.get()`! :) – prelic Feb 24 '12 at 19:02
  • @FredLarson: Why use `cin.get()` when you can launch program from shell? – SigTerm Feb 24 '12 at 19:24
  • @SigTerm - because not everyone is comfortable in the shell...why should I have to manually do the compiling, linking, etc. when my IDE is perfectly capable of doing all of that for me? Personally, I do use the command-line, but that's probably the counter-argument that would be used. If you're asking how to 'stop the output from disappearing so fast', chances are you're not a shell guru. – prelic Feb 24 '12 at 19:27
  • @prelic: "why should I have to manually". There are build systems that are more powerful/flexible than IDEs (not to mention version control systems and standard cli powertoys) and they all require shell. Not knowing shell will mean serious problems and lack of knowledge in the long run. If you're serious about programming, you must know how to work from CLI. There's no excuse for not being able to do it, so the earlier you start using it, the better. – SigTerm Feb 24 '12 at 19:39
  • @SigTerm - I'm not trying to argue with you. I don't use an IDE either, but that doesn't mean that you should comdemn IDEs altogether. In particular, they're great for beginners. The question is about Visual Studio, so suggesting he circumvent his problem by learning the CLI is probably frustrating for a beginner that just wants to see his program run. – prelic Feb 24 '12 at 20:08
  • @prelic: "I'm not trying to argue with you" I"m not arguing. "I don't use an IDE" I do use IDE, but as a text editor. Everything (project generation, version control, adding/removing files from project) is handled from CLI. It works for me. "The question is about Visual Studio" Not exactly. Op didn't mention platform/os, and there's no "visual studio" tag. *Most likely* OP uses msvc, but that's not certain. "probably frustrating" "probably"=="maybe", Op'll have to do it some day, so the sooner is the better. "learn CLI" is the best solution, but not the only solution, though - check my answer. – SigTerm Feb 24 '12 at 20:58