# Thread: Basic calculator program in C++

1. Here is what I came up with this might help yea some.
Code:
```#include <iostream>
#include <string>
using namespace std;

void diviminus(string );
void multiplus(string );
void calc();

int main()
{
string a;

cout << "Do you wish to use the Calculator? Y/N: ";
cin >> a;
if (a == "Y" || a == "y")
{
while (a == "Y" ||a == "y")
{
calc();
cout << "Would you like to use the calculator again? Y/N: ";
cin >> a;
}
}
else
{
cin.ignore(80,'\n');
return 0;
}
}

void multiplus(string cal)
{
float a;
float b;
float c;

if (cal == "multiply" || cal == "Multiply")
{
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a * b;
cout << a << " times " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
{
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a + b;
cout << a << " plus " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
}

void diviminus(string cal) {
float a;
float b;
float c;

if (cal == "divide" || cal == "Divide") {
cout << "Input a number: ";
cin >> a;
cout << "Input a second number: ";
cin >> b;
c = a / b;
cout << a << " divided by " << b << " equals " << c << ".";
cin.get();
}
else if (cal == "subtract" || cal == "Subtract") {
cout << "Input the number you want to subtract from: ";
cin >> a;
cout << "Input the amout you with to subtract: ";
cin >> b;
c = a - b;
cout << a << " minus " << b << " equals " << c << ".";
cin.ignore(80,'\n');
cin.get();
}
}

void calc()
{
string cal;
int cas;

cout << "Do you want to add, subtract, multiply, or divide?\n";
cin.ignore(80,'\n');
cin >> cal;
{
multiplus(cal);
}
else if (cal == "subtract" || cal ==  "Subtract")
{
diviminus(cal);
}
else if (cal == "multiply" || cal ==  "Multiply")
{
multiplus(cal);
}
else if (cal == "divide" || cal == "Divide")
{
diviminus(cal);
}
else
{
cout << "I'm sorry, please try again.";
calc();
}
}```

2. Thanks, I'll compare that code to mine in a sec. Turns out you're right about needing cal == "Y" || cal == "Y", as it fixed the "only goes to multi" problem. It still has that wonky bug, exept this time it just repeats the selected function twice, then displays "I'm sorry, please try again" and "Do you want to add, subtract, multiply, or divide?"

3. Code works perfectly now, thanks! Now how would you suggest I make it so you can add as many numbers as you want into the calculations? Like you could input

4 5 7 2 10

and it would output the sum of 4 plus 5 plus 7 plus 2 plus 10, and yet could input

8 1 3 10 2 6 8

without messing up the innards of the code?

4. Well if you were going to do that would all the numbers be input at the same time or would your output looks somethng like:
Code:
```Do you want to add, subtract, multiply, or divide?
Input a number: 5
Input a number: 6
5 plus 6 equals 11
do you want to add another number? Y
Input a number: 7
11 plus 7 equals 18
do you want to add another number? N```
Or what kind of formating do you want?

5. Preferrably your code-box'ed example, since it's more user friendly (or so I personally think).

6. It wouldn't be much of a code change... you would use recursive calls to do that.... do you need an example?

7. I'm not too used to using recursive calls, but now that I think of it, I could just use 3 floats and a while loop. :P

8. Yea you can use a while loop :P was thinking a bit differently and in this case a while would be more efficent I think... Here is an example for a recursive call in case you want to play with it some:
Code:
```#include <iostream>
#include <string>
using namespace std;

void recursivefunction(int );

int main()
{
recursivefunction(x);
}

void recursivefunction(int x)
{
int y;
string a;
cout<<"Enter a number: ";
cin>>y;
cout<<x<<" plus "<<y<<" equals "<<x+y;
cout<<"Do you want to add another number? ";
cin>>a;
if(a == "y" || a == "Y")
{
recursivefunction(x+y);
}
}```

9. Ahh, I see. But don't recursives have a limit on how many times they can recurse? I don't know how many, since I didn't test the recurse example in the recursion part of the tutorial here, though.

10. I just added this block of code onto the end of each half of the two math functions, and it works!

Code:
```            cout << "Would you like to add in another number? Y/N\n";
cin >> yn;
while (yn == "Y" || yn == "y") {
cout << "Input a number: ";
cin >> b;
c += b;
cout << "Your new total is " << c << ".\n";
cout << "Would you like to add in another number? Y/N\n";
cin >> yn;
}```

11. I'm trying to add support for modulus into the calculator, but what exactly is the operator for it? I'm using the statement c = a % b; for the main part of the function, and c %= b; for the while loop.

12. The limit of a recursive function is the amount of RAM in the computer since each call takes a little bit more data... modulus is used with the syntax
Code:
`a = b%c;`
it will give you the remander if b was divided by c.

It is also used in generating random numbers as an example:

Code:
```.
.
.
int randomnumber;
srand(clock());
randomnumber = 1+rand()%100;     //this is a number between 1 and 100
randomnumber = 25+rand()%76;     //this is a number between 25 and 100
.
.
.```

13. On the uses of modulus, neat. On the syntax of it, you mean I would use it as "c = a%b" and "c %=b" instead? Or am I wrong?

14. Code:
`c %= b;`
this is equivalent to the statement:
If it even compiles I have never used it like that...
Code:
`c = c%b;`

15. Well, it doesn't compile, hehe. Even the x = x%x version doesn't compile, though. It tells me "invalid operands of types "float" and "float" to binary "operator%"". Is there any special header file I need?