Aug 8, 2009 at 7:30am UTC
I started learning c++ and made a calculator thats very simple but as you can see in the code below, you can only solve problems with 2 numbers and I wanted to know how i could make this program solve a problem with an infinite amount of numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#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 Aug 8, 2009 at 7:31am UTC
Aug 8, 2009 at 6:46pm UTC
A question related to that code:
Sleep(500), 500 is miliseconds?
Aug 8, 2009 at 7:24pm UTC
exactly
sleep(1000) ---> 1 second delay
I remember sleep() was originaly in dos.h, no idea if it's still standard to use this function