You are using a version without Ads of this website. Please, consider donating:

### Making a basic calculator. Help!

I am trying to make a basic calculator. So the first problem i have is with division. How can i get a decimal when i divide? The second problem is how can i have an if statement with multiple combinations e.g. if (equation == "Multiply" or "multiply" or "+"). I know that's wrong btw :)

Also I would like some information about how to add other functions like pi and square root. Also some information about how to add a GUI.

Anyway any and all information is appreciated. Thanks.
btw i am a newbie.

This is the code so far:

 ```#include #include using namespace std; void calculator(); void what(); int main(){ what(); return 0; } void calculator(){ int a; int b; int sum; int rem; string letter = "r"; string equation; cout << "Multiply, Divide, Add or Subtract. Which would you like to do? \n" ; cin >> equation; if (equation == "Multiply"){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a * b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if (equation == "Subtract"){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a - b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if (equation == "Add"){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a + b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if (equation == "Divide"){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a / b; rem = a % b; cout << "The Answer is: \n"; cout << sum << letter << rem <<"\n\n\n"; } else { cout << "Invalid \n\n\n"; } what(); } void what(){ calculator(); }```
Last edited on
I am a beginner, but I can help you with a few things. First off if you want a variable to store a quantity with a decimal you should make it a float, not an int. For example if you try to store 6.2 into an int, it will only store 6. If you try to store 6.2 into a float, it will store 6.2. If you want an if statement to hold multiple conditions you can use the ||. || is for or. && is for and.

`if (a == 1 || a == 2)`

for square root

 ``123`` ``````#include float a, b; b = sqrt (a);``````

as for pi, you probably have to just make it a constant like this:

`const float PI = 3.14159;`

http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
I dont know why but when i use the pipes it makes any text i write go to the first if statement i use. I think the || only works for integers.
the rest works though. Thanks :)
tell me few things so dat i can make a solution...
does your given program runs a little bit...i am sure it doesnt reads what the user is asking i.e. the add, subtract n all that.....if the input 'equation' is string it cant compare in if statement with = sign ..it should be

#include<string.h>
n so on

n for that decimal...the inputs 'a' n 'b' are integers so u cant expect a float value.....either of them or both of them should be initialized as float..

check out 'math.h' for more like pi and squareroot.....if any problem again....upload your changed program again..
Last edited on
First off, your definition of functions is very odd but i managed to fix your program. I used an array of chars instead of a string seeing as you wanted it to not be case sensitive. I also modified your if statements to support the array. However if you have any questions about the code, please feel free to ask. Oh and if you want decimals in your division, then all you need to do is declare your variables (a and b) as either a double or a float, and take out the: `rem = a % b;` declaration of remainder because the % is only to be used with integers or `int`. Code is below:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````#include #include using namespace std; void calculator(); void what(); int main(){ what(); return 0; } void calculator(){ int a; int b; int sum; int rem; const char letter='r'; char equation[8]; cout << "Multiply, Divide, Add or Subtract. Which would you like to do? \n" ; cin >> equation; if (equation[0]=='M'&&equation[1]=='u'&&equation[2]=='l' &&equation[3]=='t'&&equation[4]=='i'&&equation[5]=='p'&& equation[6]=='l'&&equation[7]=='y'|| equation[0]=='m'&&equation[1]=='u'&&equation[2]=='l' &&equation[3]=='t'&&equation[4]=='i'&&equation[5]=='p'&& equation[6]=='l'&&equation[7]=='y') { cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a * b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if (equation[0]=='S'&&equation[1]=='u'&&equation[2]=='b' &&equation[3]=='t'&&equation[4]=='r'&&equation[5]=='a'&& equation[6]=='c'&&equation[7]=='t'|| equation[0]=='s'&&equation[1]=='u'&&equation[2]=='b' &&equation[3]=='t'&&equation[4]=='r'&&equation[5]=='a'&& equation[6]=='c'&&equation[7]=='t'){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a - b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if (equation[0]=='A'&&equation[1]=='d'&&equation[2]=='d'|| equation[0]=='a'&&equation[1]=='d'&&equation[2]=='d') { cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a + b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if (equation[0]=='D'&&equation[1]=='i'&&equation[2]=='v' &&equation[3]=='i'&&equation[4]=='d'&&equation[5]=='e'|| equation[0]=='d'&&equation[1]=='i'&&equation[2]=='v' &&equation[3]=='i'&&equation[4]=='d'&&equation[5]=='e') { cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a / b; rem = a % b; cout << "The Answer is: \n"; cout << sum << letter << rem <<"\n\n\n"; } else { cout << "Invalid Response\n\n\n"; } what(); } void what(){ calculator(); }``````
@guatemala007

This sort of code is a nightmare:

 ``123456`` ``````else if (equation[0]=='S'&&equation[1]=='u'&&equation[2]=='b' &&equation[3]=='t'&&equation[4]=='r'&&equation[5]=='a'&& equation[6]=='c'&&equation[7]=='t'|| equation[0]=='s'&&equation[1]=='u'&&equation[2]=='b' &&equation[3]=='t'&&equation[4]=='r'&&equation[5]=='a'&& equation[6]=='c'&&equation[7]=='t'){``````

It would be easier to write a function that convert a string to upper case, then compare that.

However, by far, what is much easier is to present a menu using a ShowMenu function, ask the user to input one character for the desired option. cin this into a char variable. To process, use a switch statement, where each option is a case clause. Each case clause would call a function that carries out that menu option.

Also, one needs to check for zero in a division expression. Be careful with this - it is not as easy as you think. Google C++ floating point comparisons to read all about it.

HTH
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include using namespace std; void calculator(); void what(); int main(){ what(); return 0; } void calculator(){ double a; double b; double sum; double rem; string letter = "r"; string equation; cout << "Multiply, Divide, Add or Subtract. Which would you like to do? \n" ; cin >> equation; if ((equation == "Multiply") || (equation == "multiply") || (equation == "*")){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a * b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if ((equation == "Subtract") || (equation == "subtract") || (equation == "-")){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a - b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if ((equation == "Add") || (equation == "add") || (equation == "+")){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a + b; cout << "The Answer is: \n"; cout << sum <<"\n\n\n"; } else if ((equation == "Divide") || (equation == "divide") || (equation == "/")){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = a / b; rem = a % b; cout << "The Answer is: \n"; cout << sum << letter << rem <<"\n\n\n"; } else if ((equation == "squareroot") || (equation == "Squareroot")){ cout << "Enter Number: \n"; cin >> a; sum = sqrt(a); cout << "The Answer is: \n"; cout << sum << letter <<"\n\n\n"; } else if ((equation == "power") || (equation == "Power") || (equation == "^")){ cout << "Enter First Number: \n"; cin >> a; cout << "Enter Second Number: \n"; cin >> b; sum = pow(a, b); rem = a % b; cout << "The Answer is: \n"; cout << sum << letter << rem <<"\n\n\n"; } else { cout << "Invalid \n\n\n"; } what(); } void what(){ calculator(); }``````
Last edited on
@Vynius
So getting the input of 2 numbers should be a function, because that code is repeated 5 times.As well as that consider putting the remaining code for each else-if into a function.

#include <iostream>
using namespace std;
int main()
{
double one, two;
cout << "please input the first number:";
cin >> one;
cout << "please input the second number:";
cin >> two;
cout << "\nSudtraction:" << one - two;
cout << "\nMultiplication:" << one * two;
cout << "\nDivision:" << one / two;
cout << "\nThanks for using austins calculator";
cout << "\n";
main();

system("PAUSE");
return 0;
}
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: