comparison between pointer and integer

I need help with a calculator i created. It keeps giving me the error;"ISO C++ forbids comparison between pointer and integer" and "invalid conversion from `const char*' to `char'".

Here's the 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
78
79
80
81
82
83
84
85
86
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
   char operation; // operation
   double first; //first number
   double second; // second number
   char operationd; // displays operation later on
   double result; // sum/difference/product/quotient
   
cout << "Calc v2 "<< endl; 
           cout << "by Marvolo1300" << endl;
           cout << " "<< endl;
           
           cout<< " Choose your first number"<< endl;
           cin >> first;
           
           cout<< "You have chosen"<< first;
           
           cout<< "Choose the operation";
           cin >> operation;
           
           if (operation=="+")
           {
                           operationd= "Addition (+)";
           }
           
           
           
           if (operation =="-")
           {
                          operationd= "Subtraction (-)";
                          }
                          
                          
            if (operation=="*")
            {
                            operationd= "Multiplication (*)";
                            }
                            
            if (operation=="/")
            {
                            operationd= "Division (/)";
                            }
                            
           
           cout<< " You have chosen"<< operationd<< endl;
           
           
           cout<< "Choose the second number"<< 
           cin >> second;
           
           if (operationd=="+")
              {
                            cout<< "The sum is"<< first + second<< endl;
                            }
           
           if (operationd=="-")
              {
                            cout << "The difference is" << first - second << endl;
                            }
                            
           if (operationd=="*")
           
              {
                            cout<< "The product is" << first * second<< endl;
                            }
                            
           if (operationd=="/")
              {
                               cout<< "The quotient is" << first / second<< endl;
                               cout << ""<< endl;
                               cout << ""<<endl;
                               
                               
           cout<< "Press ENTER to exit";
           
           cin.ignore(LONG_MAX,'\n');
           cin.get();
           
           return 0;
   
}
closed account (z05DSL3A)
The double quotes (") in line like if (operation=="+") should be single quote ('). Double quotes makes it a string literal, a single quote make is a character literal.
and line like operationd= "Addition (+)"; you are trying to assign a string to a char.

It also looks like you are missing a closing brace (}) at line 77.
Last edited on
Still doesn't work

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

using namespace std;

int main()
{
   char operation; // operation
   double first; //first number
   double second; // second number
   char operationd; // displays operation later on
   double result; // sum/difference/product/quotient
   
cout << "Calc v2 "<< endl; 
           cout << "by Marvolo1300" << endl;
           cout << " "<< endl;
           
           cout<< " Choose your first number"<< endl;
           cin >> first;
           
           cout<< "You have chosen"<< first;
           
           cout<< "Choose the operation";
           cin >> operation;
           
           if (operation=='+')
           {
                           operationd= 'Addition (+)';
           }
           
           
           
           if (operation =='-')
           {
                          operationd= 'Subtraction (-)';
                          }
                          
                          
            if (operation=='*')
            {
                            operationd= 'Multiplication (*)";
                            }
                            
            if (operation=='/')
            {
                            operationd= 'Division (/)';
                            }
                            
           
           cout<< " You have chosen"<< operationd<< endl;
           
           
           cout<< "Choose the second number"<< 
           cin >> second;
           
           if (operationd=='+')
              {
                            cout<< "The sum is"<< first + second<< endl;
                            }
           
           if (operationd=='-')
              {
                            cout << "The difference is" << first - second << endl;
                            }
                            
           if (operationd=='*')
           
              {
                            cout<< "The product is" << first * second<< endl;
                            }
                            
           if (operationd=='/')
              {
                               cout<< "The quotient is" << first / second<< endl;
                               }
                               
              cout << ""<< endl;
              cout << ""<<endl;
                               
           cout<< "Press ENTER to exit";
           
           cin.ignore(LONG_MAX,'\n');
           cin.get();
           
           return 0;
   
} 
You seem to be confusing two different variables. operation is a char and, as such, can only store a single character. Examples are '+', '-', etc.. operationd is declared a char but it appears that your intentions are to store a string-representation of operation in it. In order to do so, it should be a string. Strings can store multiple characters. Examples of strings are "", "+", "Addition (+)", etc.. Note the different types of quotes that surround char literals and string literals.
Wouldn't a switch statement work a little better?
@moorecm

how would i declare a string?
 
#include <string> // on some implementations this is done for you in iostream 


1
2
3
string s;
s = "Addition (+)";
cout << s << endl;

it worked! ty! i didn't know before that char can only hold one character.
Last edited on
Topic archived. No new replies allowed.