So I recently started to go on C++ classes and part of my homework is to make a program with "A" and "B" and to switch their number.If a=5 and b=2 I have to output b=2 a=5.That is not hard, I already made it but if I run the program outside "CodeBlocks" It immediately closes after I am done with it. I hope there are some C++ programmers on here. Here is my code: Code: #include<iostream> using namespace std; int main(){ long long a, b, c; cout<<"enter A:"<<a; cout<<"enter B:"<<b; c=a, a=b, b=c; cout<<"A="<<a<<endl; cout<<" B="<<b<<endl; return 0; }
It immediately closes after it is done because you haven't told it to do something else. You need to insert a line of code saying: stay open until I do [undefined action]
I don't program in C++, but in most of the languages, there is one command-line: pause It stops the program until you press a key (or enter most of the time).
As far as I know only the Windows Batch language has this command. For C++ he is probably searching this: Code: // Declared in iostream in the namespace std cin.get() // Read a single character
If you are using Visual Studio (Express), there's a setting to make the window stay open as well after execution I think, but what theunknownxy suggested works as well.