C++ Word-Number to int

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2012-04-10T04:34:01Z Indexed on 2012/04/10 5:29 UTC
Read the original article Hit count: 198

Filed under:

I'm developing a program that makes basic calculations using words instead of numbers. E.g. five + two would output seven.

The program becomes more complex, taking input such as two_hundred_one + five_thousand_six (201 + 5006)

Through operator overloading methods, I split each number and assign it to it's own array index.

two would be [0], hundred is [1], and one is [2]. Then the array recycles for 5006.

My problem is, to perform the actual calculation, I need to convert the words stored in the array to actual integers.

I have const string arrays such as this as a library of the words:

const string units[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

const string teens[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };

const string tens[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

If my 'token' array has stored in it two hundred one in index 0, 1, and 2, I'm not sure what the best way to convert these to ints would involve.

© Stack Overflow or respective owner

Related posts about c++