Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,565 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,881 people online right now. Registration is fast and FREE... Join Now!




Turning text into numbers

 
Reply to this topicStart new topic

Turning text into numbers, Turning a textual check amount into a numerical amount

coolgymnast01
11 Apr, 2008 - 08:29 PM
Post #1

New D.I.C Head
*

Joined: 26 Mar, 2008
Posts: 21

Hi, so I'm trying to turn a textual amount (e.g., "nineteen thousand nine hundred fifty two") into a numerical amount (e.g., "19952") using a linear search. Here is what I have so far, but I know they're is much more to do.

CODE
#include<iostream>
using namespace std;

int LinearSearch(char [], int, int);
const int SIZE = 11;

int main()
{
    char phrase[100];
    char thou[SIZE][6] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    char hun[SIZE][6] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    char ten[SIZE][6] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    char one[SIZE][6] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

    cout << "Enter textual check amount: ";
    cin >> phrase;

    int nthou = LinearSearch(thou,SIZE,phrase);
    int nhun = LinearSearch(hun,SIZE,phrase);
    int nten = LinearSearch(ten,SIZE,phrase);
    int nones = LinearSearch(one,SIZE,phrase);

    
    int value = (nthou*1000) + (nhun*100) + (nten*10) + (nones*1);
    cout << value << endl;

    return 0;
}

int LinearSearch(char list[],int numElems,int search_item)
{
    int index = 0;
    bool found = false;
    int position = 0;
        while (index < numElems && !found)
        {
            if (list[index] == search_item)
            {
                found = true;
                position = index;
            }
            index++;
        }
        return position;
}


Anything will help!! Thank you!!

*Mod Edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Turning Text Into Numbers
11 Apr, 2008 - 09:13 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 343



Thanked: 10 times
Dream Kudos: 100
My Contributions
Edit* Opps.. totally read your post wrong sorry about that.. what I did was just convert an ascii string to a number.
Your problem is much harder than that

This post has been edited by skaoth: 11 Apr, 2008 - 09:16 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Turning Text Into Numbers
12 Apr, 2008 - 08:10 PM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Well generally you are going to have to create a parser. So the first thing is to create tokens. Then you use a finite state machine to read in the tokens and convert them to numbers.

I think it would probably best to first do a FSM that can recognize three digit quantities: "five hundred and sixty three"

I see four kinds of tokens here:
DigitValue (five & three)
PlaceModifier (hundred)
SecondDigit (sixty)
ignorable (and)

so you would need a Finite State Machine that looked something like this:

total = 0

state 0 - look for:
DigitValue - add digit to total, goto state 1
SecondDigit - add secondDigit value to total, goto state 2

State 1 - look for:
PlaceModifier ("hundred") - multiply total by 100, goto state 3
End of Input - return total.

state 2 - look for:
DigitValue - add digit value, goto state 4
End of input - return total.

state 3 - look for:
SecondValue - add in second value goto state 2
Ignorable - goto state 3
DigitValue - add in digit value goto state 4

state 4 - look for:
End of Input

I may have something a little off as I am tired but it is something like that.

Once you have a FSM that can read in three digit numbers, then you can create a more complicated one that can read in larger numbers.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month