-1

I have 1 simple code in c++. That code calculate sum of 2 entered numbers. I convert that code to .exe for running windows. My problem is it will work but it doesn't show sum. After entering second number program closes immediately. I have no idea why this happen.

Here is my code:

#include <iostream> 
using namespace std; 
int main() 
{ 
    int a, b; 
    cout << "Please enter first number \n"; 
    cin >> a; 
    cout << "Please enter second number \n"; 
    cin >> b; 
    int z = a + b; 
    cout << "Sum of these numbers are: " << z; 
}

I find solution via command prompt. But I want to completely run my .exe file without command prompt

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Dostonbek
  • 37
  • 4
  • 1
    I think your progam is ok. Open a command prompt/terminal and run your program from there (instead of running it from your IDE). Side note : don't use `using namespace std;` – Pepijn Kramer Dec 26 '22 at 08:36
  • The program does what you tell it to. It prints out the sum, and immediately exits because that's what the code says to do. Command prompt is one way to get around this, since the window which runs the program will stay open (your program itself closes). Presumably you're using an IDE though, in which case if you share which IDE you are using, someone may know how to keep it open. You can usually just search for your IDE name and "prevent close when program exits" or similar to find out how to control that. – ChrisMM Dec 26 '22 at 08:40
  • *"I want to completely run my .exe file without command prompt"* -- kind of strange desire, given that your program is a command-line program. Do you mean you want to run it from your IDE (place where you edit the code)? – JaMiT Dec 26 '22 at 08:40
  • You should somehow wait for user input at the end of your program, e.g. using `getc()`. – πάντα ῥεῖ Dec 26 '22 at 08:41
  • I am using Microsoft Visual Studio. I converted it from cpp to .exe. I can't run completely from my PC. I am sorry I just learned it I don't know what is IDE. – Dostonbek Dec 26 '22 at 08:46
  • @Dostonbek *"I don't know what is IDE"* -- A simple description of an IDE (short for "[integrated development environment](https://en.wikipedia.org/wiki/Integrated_development_environment)") is *"the place where you edit the code"*. A little more precisely, an IDE is a program that provides both a way to edit your code and a way to "convert it to .exe" (more commonly called "compile it"). Visual Studio is an example of an IDE. – JaMiT Dec 26 '22 at 08:53
  • A side note: better to avoid `using namespace std` - see here https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. – wohlstad Dec 26 '22 at 09:30

1 Answers1

0

Closing the console after program finishes is a normal behaviour when you don't start it from the terminal. You could delay the closing of console by using std::cin.get(). The function waits until recieving input from terminal, so when you press any key, the program will end and console will close

ihsan
  • 365
  • 3
  • 11
  • 1
    My favorite hack; though I have found that you need to call `std::cin.seekg(0)` before calling `std::cin.get()` to get the desired effect after using `operator>>`. I usually use `std::cin.ignore()` to the same effect. – Camwin Dec 26 '22 at 09:10
  • Camwin thank you very much you are the one can help me – Dostonbek Dec 26 '22 at 09:22