Calculator shut down

I am new to c++. I made a calculator and i "build and run" it and it worked perfectly. then I tried to run the program that was created by the compiler and when I put in the numbers and hit enter the program just shuts down without showing the answer can someone help me?

I use codeblock

#include <iostream>
#include <limits>

using namespace std;

int num1, num2, result;

int main()
{
cout << "Welcome! please enter a number!" << endl;

cin >> num1;

cout << "please enter a number!" << endl;

cin >> num2;

result = num1 * num2;

cout << num1 << " * " << num2 << " is: " <<result << ".\n";


cout << "Press ENTER to continue...";
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

return 0;
}
Last edited on
Add the line std::cin.get(); immediately before return 0;

Or, run your program from the command line.

Last edited on
hmm. Thats strange. I ran your code in notepad++ and didn't have any issues. I wonder what could be causing that.
http://www.cplusplus.com/forum/beginner/1988/
You appear to be using the pause function that duos kept suggesting but did cin.get() work for you?

What OS are you running it on? Is it possible you've downloaded the IDE from an outdated source?
http://forums.codeblocks.org/index.php?topic=9912.0 <- this suggests that pause after run is turned off by default and can be turned on under project preferences. (Not the default setting on my Linux machine, but here it actually opens xterm, so that's not saying much)
Last edited on
Thank you!

the std::cin.get(); line worked. what is the function of that line?

I have also been serching for a way to make the program start over again after the equation is finished and not just shut down. I have tried to see if the loop functions can help me, but I have not had any luck with that.
cin.get (); <-this says to wait for the user to input a single character. Usually you would set a char type variable using it, but here we are ignoring the actual content that it returns.

Loops are what you are needing to make the program run multiple times, look into them a bit further. They run everything in their block over and over until the given condition is met. They must be inside of another function like main, they are not functions in themselves.

While loops and for loops are the main ones, though there are for-each and do-While loops later on.
@keinrich

Tip: If you used std::cin or std::getline() one or more times to get input before, then use
std::cin.ignore(/* stuff... */); std::cin.get();.

If not, then just use std::cin.get();.
Topic archived. No new replies allowed.