How would I turn a string with regular numbers in it into an int. I need to turn something like char tmp[3] into int x.
This is a discussion on string of numbers into int? within the C++ Programming forums, part of the General Programming Boards category; How would I turn a string with regular numbers in it into an int. I need to turn something like ...
How would I turn a string with regular numbers in it into an int. I need to turn something like char tmp[3] into int x.
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 } }
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel