Hi I\'m trying to pull one member of a string and set that equal to an int. I understand that you can\'t simply put the int equal to the string value but I\'m not sure how else
Each element in a string is castable to an int implicitly (a basic_string contains char type items which are mapped to ascii characters in standard in/out methods.) Since each character is a number from 0 to 255 mapping to this table you can see how this plays out:
if('0' == 48){cout << "0 char is 48 int";}
C++ has several conversion methods. With simple char types you can exploit the basic ascii table layout and treat it as a number:
int resultNumber = mystring[i] - '0';
Using the above method, you would be wise to check the character is in the range of '0' to '9' first. The following does that check and returns -1 in the case of an error.
int resultNumber = (mystring[i] >= '0' && mystring[i] <= '9')?mystring[i] - '0':-1;
But you can also use stoi because char will implicitly convert to a string. This has the distinct benefit of working for more than a simple char. There is minor overhead for casting a single character to a string vs the previous method, however.
int resultNumber = stoi(mystring[i]);
You can cast a whole string at once if you want (this will not work in your case because you want to check each character individually, but for a 2+ digit number it will work.)
C's atoi does the same thing but requires a char* (C style string) which will not implicitly work with a char type (casting to char* with &mystring[i] would also not work because it requires null termination).