nameJack
Nov 16th, 2001, 11:13 PM
if i type in "twelve" on a cin syntax, can c++ change the twelve into number eg : t-116, w-119 and vice versa
i tried using
char letter[] = "twelve";
and the output is twelve
but i want it as integer. maybe the sum of all the ascii characters
can anyone help me?
CornedBee
Nov 17th, 2001, 02:25 AM
you'd need to make a list of all the number words that are legal and then search the array for the word typed in and return the appropriate number.
int NumWordToInt(char* szNumWord) // requires string.h and stdlib.h
{
const static char nums[10][] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
int i;
char* pc;
char temp[100]:
// convert word to lower case
for(pc = szNumWord; *pc; pc++)
*(temp + (pc - szNumWord)) = tolower(*pc);
*(temp + (pc - szNumWord)) = '\0';
// search for the string
for(i = 0; i < 10; i++)
{
if(strcmp(nums[i], temp) == 0) return i; // found a match
}
return -1; // error return
}
You can always make the function more sophisticated, but you get the idea.