0

I tried without '0' and got error,Please explain it.

getline(cin,str);
int n=str[4]-'0';
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • The string has the digit's ascii value. Also, the ascii values for all digits are contiguous. So to convert from the ascii value to the digit value, you can subtract the ascii value of '0'. – L. Scott Johnson Nov 21 '20 at 18:20
  • The ASCII value of '0' is 48 and that of '9' is 57. Basically, any number (literal) you encounter is in between and hence the subtraction does the job. – Abin Thomas Nov 21 '20 at 18:23

0 Answers0