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 <iostream>
#include <math.h>
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();
}
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
1 2 3
#include <math.h>
float a, b;
b = sqrt (a);
as for pi, you probably have to just make it a constant like this:
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>
if(strcmpi(equation,"Add")==0)
// then add it....
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..
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:
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.
@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.