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 <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();
}
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

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:

const float PI = 3.14159;

More information can be found here:
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>
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..
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:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#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;
    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:

1
2
3
4
5
6
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <math.h>
#include <string>

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.

Did you see what I said about having a menu?



#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 << "\nThe answers are:\nAddition:" << one + 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.