2
#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
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 5
    `fx[0] and fx[3] are obviously integers` are you sure about that? – Alberto Sinigaglia Jul 30 '21 at 14:33
  • convert them to int – sittsering Jul 30 '21 at 14:33
  • 2
    ASCII `'6'` is 0x36 (54) and ASCII `'2'` is 0x32 (50). `54 * 50 == 2700`. By the way, [please do not post images of text because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Text should be posted directly **as text** in your question. – MikeCAT Jul 30 '21 at 14:35

4 Answers4

5

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!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
2

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.

Chris Uzdavinis
  • 6,022
  • 9
  • 16
2

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
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • The character encoding is not involved in the promotion of a `char` value to an `int`. The conversion from text to integer values is done by the compiler when it converts the **text** `"6x^2+6x+4"` into `char` values. – Pete Becker Jul 30 '21 at 18:11
  • And note that there are no casts in this code. A cast is something you write in your source code to tell the compiler to do a conversion. The compiler converts `char` values to `int` in arithmetic expressions on its own; no casts involved. – Pete Becker Jul 30 '21 at 18:12
1

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
Nitheesh George
  • 1,357
  • 10
  • 13
  • 1
    *You are multiplying character types.* Not really - the `char` operands are promoted to `int` before the multiplication. – Adrian Mole Jul 30 '21 at 14:46
  • That is what I meant actually – Nitheesh George Jul 30 '21 at 15:30
  • Re: "converted ... then multiplication" -- that's not the sequence. The compiler uses the character encoding when it converts `"6x^2+6x+4"` to a sequence of `char` values, which is stores in `fx`. For the multiplication, the values stored in `fx[0]` and `fx[1]` are simply promoted to `int`. – Pete Becker Jul 30 '21 at 18:15