Totally unrelated result when multiplying two integers?

Viewed 95
#include <iostream>
using std::cout;
using std::endl;
        
int main(void) {
    std::string fx = "6x^2+6x+4";
        
    int part1 = fx[0] * fx[3];
                cout << fx[0] << endl;
                cout << fx[3] << endl;
                cout << part1;
}

So I have this string and fx[0] and fx[3] are obviously integers: when I print them to the console they print out just fine; however, part1 (their multiplication) equals some totally unrelated number? Can anyone help?

Here is the output:

6
2
2700
4 Answers

Your fx[0] and fx[3] variables are of type char (which is an integer type in C++). However, the actual values in those two elements of your fx string will be representations of the digits, 6 and 2, not the numerical values of those digits.

Very often, those representations will be ASCII codes (but that's not required); however, what is required is that the representations of the digits 0 thru 9 have contiguous, sequential values. Thus, by subtracting the value of the digit, 0, we can convert to their numerical representations.

In your case, the following line will do the conversion:

    int part1 = (fx[0]-'0') * (fx[3]-'0');

The reason why you see the correct values when printing fx[0] and fx[3] is because the version of the cout << operator that takes a char argument is designed to print the represented character (not its 'ASCII' code); however, the cout << operator for an int type (like your part1) will print the actual value represented internally. (Try changing one of your lines to cout << (int)fx[0] << endl; to see the difference.)


P.S. Don't forget the #include <string> header – some implementations do that implicity inside the <iostream> header, but don't rely on that!

Characters are values representing glyphs from some representation, usually the ASCII table. The numeric value of a character is not the same as the glyph that is printed on the screen. To convert a numeric-looking char to an actual "0-based" numeric value, subtract '0' from your char value.

(fx[3]-'0']) will be the numeric value of character represented at position 3.

well, first of all, string::operator[] returns a char... then, a char can be casted to an int, and the cast works checking the ID in the ASCII table (in your case)

In ASCII, the ID of "6" and "2" are respectively 54 and 52 (you can check it here for example)... so your program is taking the two char, casting them to int, and multiplying them (54 * 50 = 2700)

If you need to interpret those as the integer value they represent, you can check this answer:

int val = '6' - '0'; // val == 6

You are multiplying character types. so the characters '6' and '2' will converted to its integer values 54 and 50 respectively then multiplication is applied. This works based on C++ type conversion rule. Then you will get 2,700. Try the modified sample code

#include <iostream>
using std::cout;
using std::endl;

int main(void) {
    std::string fx = "6x^2+6x+4";

    int part1 = fx[0] * fx[3];
    cout << fx[0] << endl;
    cout << fx[3] << endl;
    cout << part1;
    cout << std::endl;
    cout << (int)fx[0] << " " << (int)fx[3] << std::endl;
}

And the results

6
2
2700
54 50
Related