Here is the FAQ for string to int conversion. It is a very good FAQ.. because it helped me write this function that I used in my blackjack game:
Code:
//Converts the first character of a playing card from 'string' to 'int' so numeric comparisons can be made.
//Example: string '2 of Diamonds' will be converted to integer '2'
//istringstream can only accept the &address of the character to be streamed to integer
int card_value(string card)
{
int result = 0;
char *card_ptr;
card_ptr = &card[0]; //string class variables can be treated like arrays
istringstream myStream(card_ptr); //create a 'myStream' object of class istringstream
if (myStream >> result) //String to Integer conversion (works only for numeric face values)
return result; //return an integer value 2 thru 10
else
{
if (*card_ptr != 'A')
return 10; //'J', 'Q', and 'K' will return a face value of 10
else
return 11; //'A' will return a value of 11
}
}