Math Formulas using Variables

Hello All,

Working on a program that converts infix to postfix math formulas. Ive successfully converted the infix to a postfix notation but now i am having trouble solving the equation from the postfix form. Im trying to set a result equal to three variables as shown below:

result = op1 ch op2;

where op1 and op2 are numbers and ch is the operator (+-*/) depending on what the user entered into a string. The error im getting is that it expected a ";" before postfix so clearly it doesn't understand what im trying to do. Im looking for help on how to put the answer from op1 ch op2 into result. Thanks for all replies!
result = op1 ch op2;


That is the equivalent of: result = op1 '*' op2.

You see, '*' is interpreted by the compiler as a character constant, not as an arithmetic operator.

What data types are, result, op1, and op2 declared with?
Irrelevant what datatypes, this code won't compile.

But it seems that you are trying to directly multiply the character constants of a string with another one, and put the result into 'result'.
If this is the case, then you should revise the topics arithmetic operations, and datatypes + character arrays(pointers) in C++.
Because the fact is that a string is just a pointer to a char, and is not recognised by the compiler as what seems to be their representations, in fact strings are just a series of
8bit numbers treated as 'characters' to gain readable output.

If the variables you declared, except char ch; are of an integral or floating point type,
Then you still would have to revise: arithmetic operations, and datatypes.
Because it just doesn't make sense to say: result = op1 42 op2, which is the equivalent of: result = op1 ch op2; (in the case that ch is of type char, which it's name indicates).

Im trying to set a result equal to three variables as shown below:

ch in this case is not a variable.
This is one possible way of doing it:
1
2
3
4
5
6
7
8
9
10
11
int op1=0,op2=0,result=0,code;
cout<<"enter 1 for addition, 2 for subtraction...etc.";
cin>>code;
switch(code)
{
case 1: cout<<" addition result= "<<op1 + op2;
                        break;
case 2: cout<<"etc

case default: cout<<"you entered something other than 1 - 4
}
I'm trying to get it to calculate the cir, and the area, and the diameter, but I just do not know how to make the formula actually print and calculate, help.
Sorry I started last week
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>



using namespace std;

double r;
double pi = 3.14;
double d = 2 * r;
double cir = d * pi;
bool is_negativer = r <= 0;
string type;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "This program will help you roughly determine the area, circumference, and diameter of a circle." <<endl;
	cout << "Input in all lowercase what you want to calculate, e.g circumference, area. " <<endl;
	getline(cin, type);
		if(type == "circumference")
		{
			cout << "Please enter the radius of the circle" <<endl;
			cin >> r;
			while(is_negativer == false)
				{
					cout << "Invalid answer, it needs to be positive dummy" <<endl;
					cout << "please re-enter the radius" <<endl;
					cin >> r;
					
				}
			
			cout << cir <<endl;
		}
		else if(type == "area")
		{
			cout << "The required information you will provide is: radius" <<endl;
			cin >> r;
			while(is_negativer == false)
				{
					cout << "Invalid answer, it needs to be positive dummy" <<endl;
					cout << "please re-enter the radius" <<endl;
					cin >> r;
					
				}

		}
		
	return 0;
}
Last edited on
@addictedgamer6

Start your own thread, don't invade this one.
Topic archived. No new replies allowed.