Hey guys im learning C++ and im a beginner of course. I just have a simple question, or at least i think its simple for someone with experience.
Problem: When i compile and run my code it works perfectly. Its a basic calculator and asks for 2 numbers. But then I try to run my .exe file, it opens, then asks for the 2 numbers, then gives me the answer, and immediately exits!
Question: How can I stop my program from closing immediately? Or make it pause in a way?
Additional Info:
- system("pause"); does not work, and has errors if i use it.
- I use Windows 7
- I use CodeBlocks
Problem: When i compile and run my code it works perfectly. Its a basic calculator and asks for 2 numbers. But then I try to run my .exe file, it opens, then asks for the 2 numbers, then gives me the answer, and immediately exits!
Question: How can I stop my program from closing immediately? Or make it pause in a way?
Additional Info:
- system("pause"); does not work, and has errors if i use it.
- I use Windows 7
- I use CodeBlocks
4
Thanks for the information. I see using System("pause") was a bad idea from the beginning. I tried using cin.get() but the results were the same. It could be simply because im running a simple CMD type program instead of one with actual interfaces/graphics. Im still not quite sure since im new.
Thanks for the information, it was indeed useful.
Thanks for the information, it was indeed useful.
Hmm i wanted the CMD to stay open since im not that far with C++ to make graphics / interfaces or whatever. Here is an example of my code:
If you open the .EXE it wont stay open long enough to see the answer. Thats what I was wondering about but if its not possible I understand. Id just like a little explanation of why, if thats alright. Thanks for the reply.
#include <iostream>
using namespace std;
int main(){
int a;
int b;
int sum;
cout << "Enter a number: ";
cin >> a;
cout << "Enter a second number: ";
cin >> b;
sum = a + b;
cout << "The Sum is: " << sum << endl;
}
If you open the .EXE it wont stay open long enough to see the answer. Thats what I was wondering about but if its not possible I understand. Id just like a little explanation of why, if thats alright. Thanks for the reply.
Do you want the program to restart? Normally after a program has executed it will close unless you add some kind of loop function
