How do i make a calculator that lets you run the program again when finished?

How can i make it so when it is finished being used, it gives you the option to be used again?

codes:

#include <cstdlib>
#include <iostream>
#include <windows.h>
int ans;
int x;
int y;

using namespace std;

int main(int argc, char *argv[])
{
cout <<"Welcome to Cheesies calculator." << endl;
cout <<"For addition press 1." << endl;
cout <<"For subtraction press 2." << endl;
cout <<"For multiplcation press 3." << endl;
cout <<"For division press 4." << endl;
cin >> ans;

if (ans==1)
{
cout <<"You chose to do addition!" << endl;
cout <<"Please enter the first number." << endl;
cin >> x;
cout <<"Now, enter the second number." << endl;
cin >> y;
cout <<"calculating......"<< endl;
Sleep (500);
cout <<"Finished calculating! Answer:"<< x+y << endl;
}
if (ans==2)
{
cout <<"You chose to do subtraction!" << endl;
cout <<"Please enter the first number." << endl;
cin >> x;
cout <<"Now, enter the second number." << endl;
cin >> y;
cout <<"calculating......"<< endl;
Sleep (500);
cout <<"Finished calculating! Answer:"<< x-y << endl;
}
if (ans==3)
{
cout <<"You chose to do multiplcation!" << endl;
cout <<"Please enter the first number." << endl;
cin >> x;
cout <<"Now, enter the second number." << endl;
cin >> y;
cout <<"calculating......"<< endl;
Sleep (500);
cout <<"Finished calculating! Answer:"<< x*y << endl;
}
if (ans==4)
{
cout <<"You chose to do division!" << endl;
cout <<"Please enter the first number." << endl;
cin >> x;
cout <<"Now, enter the second number." << endl;
cin >> y;
cout <<"calculating......"<< endl;
Sleep (500);
cout <<"Finished calculating! Answer:"<< x/y << endl;
}




system("PAUSE");
return EXIT_SUCCESS;
}

TY in advance!
Last edited on
Please use code tags when posting source code.

Generally, if you'd like a program to be runnable multiple times, you can use a while loop:

1
2
3
4
5
6
7
8
9
bool done = false;
while (!done)
{
  int a;
  cout << "Type 0 to quit: ";
  cin >> a;

  done = (a == 0);
}
Last edited on
1. use [ code][ /code] tags to highlight the code

2. avoid system(). Why? Read this: http://www.cplusplus.com/forum/articles/11153/

3. Instead system("PAUSE") you can use e.g.
1
2
cin.sync();
cin.get();

but that's only one of many options

4. avoid using global variable definitions

5. more efficient than if-else-if ladder is switch(operation)
for more info check this post: http://www.cplusplus.com/forum/beginner/13281/

6. now to the point:

you may put everything into loop, so the code will look more/less like this one:
http://www.dreamincode.net/code/snippet884.htm

EDIT: yes, follow Duncan's advice

or

you can make a function with simple menu e.g. 1-add, 2 - substr, 3 - mul, 4 - div, 5 - exit
and put everything into switch block

Finally, i like this: sleep(500) Why you put that into your code :-)? To simulate ENIAC :)?
Last edited on
Thanks!
and sorry about not using code tags xD forgot, it wont happen again
O_o

and john to answer why i put sleep(500)
I like the suspence of it calculating :D its so unfun when its just BAM and answer
its more fun when it says "calculating......"
dont you agree?
Last edited on
No problem - I started the same way as you did, btw, don't go anywhere - wait for more posts from more experienced guys!

and john to answer why i put sleep(500)
I like the suspence of it calculating :D its so unfun when its just BAM and answer
its more fun when it says "calculating......"
dont you agree?


if speaking about your code, then yes, why not? If it's only for fun, especially when your code isn't that complex, but couple of weeks ago i was working under card shuffling alghoritm (still working under it - now it's time for double dummy solver - bridge app), human-like simulation, then I've made tests for 10-20 million different hands. Results were consistent with probability distribution of different splits of suits. What was great about it, those tests accomplished in less than 8 minutes on my machine

I hope you got my point... oufff... i changed your topic into personal blog, sorry for that
Last edited on
Hey do you think you could answer another question for me?
sure thing, after all this is C++ forum :) There are many people here willing to help!
Topic archived. No new replies allowed.