Finding the Value of a Char

Could someone explain the correct method for seeing if a char is equal to a certain letter/number, this method comes up with an error. I think the relevant lines would be 2, 12, 18 and 23.

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
int num1, num2, den1, den2;
    char slash, code;
    float dec1, dec2;
    int numAdd, denAdd;
    int numMult, denMult;
    cout<< "Input a fraction!" << "\n";
    cin>>num1>>slash>>den1;
    cout<< "Input another fraction!" << "\n";
    cin>>num2>>slash>>den2;
    cout<< "You can convert to decimal [A], add [B], or multiply [C] your fraction. Enter your code now.";
    cin>>code;
    if (code == "A"){
        dec1 = num1 / den1;
        dec2 = num2 / den2;
        cout<<"Decimal 1 = "<<dec1;
        cout<<"Decimal 2 = "<<dec2;
    }
    if (code == "B"){
        numAdd = num1 * den2 + den1 * num2;
        denAdd = den1 * den2;
        cout<<"Your fraction is: "<<numAdd<<slash<<denAdd;
    }
    if (code == "C"){
        numMult = num1 * num2;
        denMult = den1 * den2;
        cout<<"Your fraction is: "<<numMult<<slash<<denMult;
    }


I'm using XCode and the error's i get (on line's 12, 18, 23) are:

Result of comparison against a string literal is unspecified (use strncmp instead)

Comparison between pointer and integer ('int' and 'const char *')

Thanks!
Well, since you're using XCode, replace code == "A" with strncmp(code, 'A', 1). Should work. If not, I'll leave it to someone else who knows more on XCode than I do.
Now i get 'No matching function for call to strncmp'

could this be an error with needing one of these:

#include <iostream>

for strings? If so what is it?
1
2
// if (code == "A"){
if ( code == 'A' ){


'A' 'B' 'C' are literals of type char.

"A" "B" "C" are literal strings.

'A' 'B' 'C' are literals of type char.

"A" "B" "C" are literal strings.


That makes sense, thanks!
Topic archived. No new replies allowed.