0
char* array{};
string digit4;

cout << "Enter a 4 digit-integer : " << endl;
cin >> digit4;

cout << "The 4 digit-integer you have entered is : " << digit4 << endl;

array = &digit4[0];

cout << "Digit 1 is : " << array[0] << endl;

int digit1 = (array[0] + 7) % 10;

cout << digit1 << endl;

return

I tried to convert a string to an array and use the first digit of array[0] to a formula of ( array[0] + 7 ) % 10. If I input 1234, ( 1 + 7 ) % 10 should be 8 but I'm getting 6 instead of 8. Any help would be great. Thank you for reading.

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
Takirin
  • 3
  • 2
  • 3
    The value of the characters that represent `'0'` through `'9'` do _not_ correspond with integers `0` through `9`. Try `int digit = (digit4[0] - '0' + 7) % 10;` and to better see what's going on, output the character's actual value: `cout << "Digit 1 is : " << (int)digit4[0] << endl;` – paddy Jul 25 '22 at 04:54
  • 2
    `'1'` is not the same as `1`. – Aykhan Hagverdili Jul 25 '22 at 04:54
  • 3
    There are no arrays in this code, just a string and a pointer. – john Jul 25 '22 at 04:57
  • *"You have to realize that characters and strings are just numbers, like everything else in the computer. When you write `'a'` in the source code, it is pre-processed into the number 97, which is an integer constant."* from [dupe](https://stackoverflow.com/a/5030541/12002570) – Jason Jul 25 '22 at 05:12

2 Answers2

2

The expression array[0] evaluates to the character code for the digit '1', which is 49 in ASCII.

Therefore, assuming that you are using ASCII, the expression

(array[0] + 7) % 10;

is equivalent to

(49 + 7) % 10

which is 6.

If you want to get the value that is represented by the digit, then you can simply subtract '0' (which is the character code for the digit '0', which is 48 in ASCII) from the character code of the digit.

Therefore, to solve your problem, you can simply change the line

int digit1 = (array[0] + 7) % 10;

to:

int digit1 = ( array[0] - '0' + 7 ) % 10;
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

The array[0] is 49 in ascii, when you use + you convert it to int like int(array[0]) which the result is 49. you should convert array[0] to int number like This post

  • The difference between a character code and the value that a digit represents has nothing to do with whether a value is an `int` or not. Both can be represented as `char` or `int`. A `char` is also an integer type. – Andreas Wenzel Jul 25 '22 at 05:19
  • @AndreasWenzel Yes `char` is 1 byte `int` as [here](https://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-c) mentioned, but when use `+` ,the char converted to `int` in ascii and the conversion result is 49 which cause the wrong problem. I edit my answer, hope there in no ambiguity – Mohammadreza Hojatoleslami Jul 25 '22 at 05:27
  • Yes, you are right that `array[0]` is a `char` and that it will get promoted to an `int` when used as an operand in the `+` operation. However, whether such a promotion takes place or not is not relevant in this case. As stated in my previous comment, both the character code and the value that a digit represents can be represented as a `char` and as an `int`, because both are integer types. Therefore, the title of the question you linked to ("Convert char to int in C and C++") is misleading.... – Andreas Wenzel Jul 25 '22 at 05:39
  • 1
    .... Converting between a `char` and an `int` has nothing to do with converting between a character code and the value that a digit represents. – Andreas Wenzel Jul 25 '22 at 05:44