1

Quick C++ Question

Bugs_e 9/18/13 5:47 pm
186
9/18/2013 7:47 pm
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
Posted by
Bugs_e
Level 1 : New Miner
0

  Have something to say?

JoinSign in

4

Bugs_e
09/18/2013 7:47 pm
Level 1 : New Miner
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.
1
Allele
09/18/2013 7:33 pm
Level 18 : Journeyman Architect
I think the reason why system ("PAUSE"); isn't working for you is because of your compiler, CodeBlocks.

Here's some more information on what you can do here.
1
Bugs_e
09/18/2013 7:14 pm
Level 1 : New Miner
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:


#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.
1
bmanrules
09/18/2013 6:00 pm
Level 57 : Grandmaster Programmer
Do you want the program to restart? Normally after a program has executed it will close unless you add some kind of loop function
1

Welcome