简体   繁体   中英

Turbo c++ program exits on hit enter

It succesfully runs without any error. after enter first digit and hit enter key then it shows Enter second digit The problem is here that After enter second digit and i hit enter it not showing result. Instead of result it exit from the program window and enter to the window where i was programming.

#include <iostream.h>
#include <conio.h>
int main()
{     clrscr();
int value1, value2, sum ;
cout << "Enter First Digit : " ;
cin >> value1 ;
cout << "Enter Second Digit : " ;
cin >> value2 ;
sum = value1+value2 ;
cout << "The Sum is : " ;
cout << sum ;
return 0;
}

I think your program is outputting correctly, and then closing.

One option is to just ask for one more bit of input, but then discard it at the end:

cout << "Please Enter to quit";
int temp;
cin >> temp;

Another way would be to run your program in a command window - on Windows, you can run "cmd", navigate to the folder that contains your program, then type in the file name to run it.

Your IDE might also allow you to enable prompt-upon-finish.

Turbo C++ has an option to view to command prompt window. Click "Window" then "Output" in the menu.

Alternatively, add the line cin.get(); at the end of your program, just before the return statement.

system("pause");

Adding this code before 'return 0' will pause your program and give you time to look at the result. If you don't like the "Press any key to continue" message it shows you can go for this:

cin.get();

However you have to include another library whenever you use it:

#include <conio.h>

Since you have used it already you don't have to include it anymore.

Warning: The second way may create you some problems in more complex programs. I recommend you to use the 'system("pause");' if you aren't sure whether the 'cin.get();' will work or not.

Add getch(); before closing the last curly bracket. You won't exit from the output screen and you will be able to see the output.

Write like this:

#include <iostream.h>
#include <conio.h>
int main()
{     clrscr();
int value1, value2, sum ;
cout << "Enter First Digit : " ;
cin >> value1 ;
cout << "Enter Second Digit : " ;
cin >> value2 ;
sum = value1+value2 ;
cout << "The Sum is : " ;
cout << sum ;
return 0;
getch();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM