Simple problem.

Hey!
I have a simple problem to solve and I encounter an error in the code I wrote. Could you tell me what is wrong with my particular code or if it`s fundamentally wrong what should I do. :)
The problem is: Write a simple arithmetic expression translator that reads in expressions such as 25.5 + 34.2 and displays their value. Each expression has
two numbers separated by an arithmetic operator.

The first error I get is: "no match for 'operator==' in 'sign == '+''". There are more errors, but I don`t think it`s needed to post every single one.

My logic on the code is this: give my sentinel value the lowest possible integer
do not stop to print the result of the two numbers, until you enter -2147483648.

Afterwards it is pretty much a simple if statement with the exception that when we divide the second number can not be 0.

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 <iostream>
#include <limits.h>
using namespace std;
int main()
{

float Num1;
float Num2;
string sign;
float result;
int sentinel;

sentinel = INT_MIN;


cout<<" Enter an integer or " << sentinel << "to stop";

 while(Num1!= sentinel)
 {
  cout<<"Input the expression please:";
  cin>>Num1;
  cin>>sign;
  cin>>Num2;
   if(sign=='+')
    {
        result = Num1+ Num2 ;
        cout<<"Your result is "<< result <<endl;
    }
  else if(sign=='-')
    {
         result = Num1 - Num2 ;
         cout << "Your result is " << result<<endl;
    }
  else if(sign=='*')
    {
          result = Num1 * Num2 ;
          cout << "Your result is " << result <<endl;
    }
  else if(sign=='/')
    {
          if(Num2=0)
          cout<< "No number can be divided by 0"<<endl;
          else
          {result = Num1+ Num2 ;
          cout << "Your result is " << result<<endl;}
    }
 }

 return 0;
}
Last edited on
character '+' != string "+". Compare strings with strings.
Line 9
Change string sign; to char sign;
Or as MiiNiPaa suggests.
Last edited on
Thank you for that. I realise that I don`t have a very good understanding on string, but I`m working on it.
Program runs, but I notice two problems.
First, for some reason it only adds the number regardless of my input(doesn`t multiply or substract or divide).
Second, it does not stop when I enter my sentinel value.
Last edited on
Strange it does multiply, subtract and divide when I run the code once I changed line 44:

result = Num1+ Num2 ;

to

result = Num1 / Num2 ;

It will also exit but you do have to enter two dummy values after the sentinel.

Maybe use a do loop and exit condition determined by a request.


1
2
3
4
5
6
7
    char reply;
    do
    {
        ...
        cout << "Do you wish to continue y/n";
        cin  >> reply;
    }while (reply == 'y');


Last edited on
Ahm, in the code I have last saved i have it fixed in line 44, which should be Num1 / Num2. And I tried a do-while loop instead, but it still does not work. Would you care to PM me or write here your code that as you say runs ?

NOTE: I changed the if(Num2=0) to if(Num2==0) in line 38.
Otherwise the division returns inf.

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

using namespace std;
int main()
{

float Num1;
float Num2;
char  sign;
float result;
char  reply;


do
 {
  cout<<"Input the expression please: ";
  cin>>Num1;
  cin>>sign;
  cin>>Num2;
  cout << endl;
   if(sign=='+')
    {
        result = Num1+ Num2 ;
        cout<<"Your result is "<< result <<endl;
    }
  else if(sign=='-')
    {
         result = Num1 - Num2 ;
         cout << "Your result is " << result<<endl;
    }
  else if(sign=='*')
    {
          result = Num1 * Num2 ;
          cout << "Your result is " << result <<endl;
    }
  else if(sign=='/')
    {
          if(Num2==0)
          {
          cout<< "No number can be divided by 0"<<endl;
          }
          else
          {
          result = Num1 / Num2 ;
          cout << "Your result is " << result<<endl;
          }
    }

        cout << "Do you wish to continue y/n? ";
        cin  >> reply;
    }while (reply == 'y');

 return 0;
}

Last edited on
Also try to use
#include <string>
Thank you. Finally fixed it. I kind of forget to write "==" in if statements. LoL...
Topic archived. No new replies allowed.