Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

add - C++ beginner, execution window disappears quickly

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;
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...