Make a char into an operation?

Is there any way to make an operation into a variable, in order to get it as an input value? Here is a short example of a general idea, I know this will no where near work, its just an extremely simplified thing for context of the idea:

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
#include <iostream>
#include <math.h>
#include <string>

using namespace std; 

int main ()

{ 
     int number1, number2;
          cin >> number1;
          cin >> number2;
     char operation;
          if (operation == "+")
               {
                     cout << number1 + <<
               }
          if (operation == "-")
               { 
                     cout << number - <<
               }
          else 
               {
                    cout << "
                    
                     cout << number 2 << endl;
}

return 0;
 


Is it even possible to get an operation as an character or even a floating value, and if so, how would it work? And if this code may work, with adjustment that would be much simpler! Thanks!
Last edited on
I think what you are thinking about is operator overloading.

http://www.cplusplus.com/doc/tutorial/classes2/

We can define a function that uses an operator for a class. That will let us add, subtract or equate classes according to our own definitions.

Another more basic idea for your specific problem might be:

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
#include <iostream>
#include <math.h>
#include <string>

using namespace std; 

int main ()

{ 
     int number1, number2;
     char operation;
     cin >> number1;
     cin >> operation;
     cin >> number2;
          
     if (operation == '+')
     {
          cout << number1 + number2;
     }
     else if (operation == '-')
     { 
          cout << number1 - number2;
     }
     return 0;
}
So in my attempt to make a basic calculator, with your method of turning the character into an operation I came up with this code, however I have had two main problems, this is the over all code:

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
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

void newLine ()
    {
        cout << endl;
    }

int main()

{
double number1;
double number2;


    {
    int number1, number2;

     char operation;
     cout << "Please enter the first number:";
        cin >> number1;
        if (number1 > 1000000)
            {
                cout << "Please enter a lower number, this calculator is basic" << endl;
                newLine ();
                    {
                       return main ();
                    }
            }
        else
            {

            }
     cout << "Please enter numerical operation:";
        cin >> operation;
     cout << "Please enter second number:";
        cin >> number2;
        if (number2 > 1000000)
            {
                cout << "Please enter a lower number, this calculator is basic" << endl;
                cout << endl;
                return main();
            }

     if (operation == '+')
        {
          cout << number1 + number2;
        }
     else if (operation == '-')
        {
          cout << number1 - number2;
        }
     else if (operation == '*' or 'x')
        {
          cout << number1 * number2;
        }
     else if (operation == '/')
        {
            cout << number1 / number2;
        }
     else if (0 <= operation or operation > 0)
        {
            cout << "That is not recognized as an operation, /n try (+, -, /, *, x)" << endl;
            newLine ();
                {
                    return main ();
                }
        }
    }

    cout << endl;

return main();
}



My first problem is that whenever the '/' is put in for division, the calculator carries out multiplication instead:

1
2
3
4
 else if (operation == '/')
        {
            cout << number1 / number2;
        }




My second problem is that, when a number or anything is entered for char operation besides the given operation values, it will automatically do multiplication. I tried to stop this, but it still does not work at all:
1
2
3
4
5
6
7
8
 
else if (0 <= operation or operation > 0)
        {
            cout << "That is not recognized as an operation, /n try (+, -, /, *, x)" << endl;
            newLine ();
                {
                    return main ();
                }

else if (operation == '*' or 'x')

This will always be true. Anything that isn't zero will evaluate to true when used as a boolean expression. The above is basically the same as saying

operation == '*' or true

You should be doing something like this instead:

else if (operation == '*' || operation == 'x')

Also, don't make main() recursive. It is a special entry point function and it should be left as such.
Last edited on
Topic archived. No new replies allowed.