Thread: help!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    help!

    I have a problems with converting strings to number,
    for example,
    one thousand nine hundred eighty three -->> 1983

    I have thought for a long long time, but couldn't think of how to do the coversion.

    At the begining, I want to assign each number, i.e. one, two, three...ten, an array like a1[] = "one", a2[] = "two"......
    that more than 10 arrays will be needed. The main problem is that I can't extract "one" in the string of words to compare. I really get stuck now, please help me. thx!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    I wrote something like that.....

    #include "stdio.h"
    #include "string.h"

    #define MAXLINESIZE 1001
    #define MAXWORDSIZE 50
    #define MAXWORDNUMBER 20

    void main()
    {
    char words[MAXWORDNUMBER][MAXWORDSIZE];
    int i = 0, j = 0, k = 0, chr;

    printf( "Enter Value : " );

    /* This part is to split all words and assign each word to an array orderly */

    for( k = 0; (k < MAXLINESIZE) && ((chr = getchar()) != EOF)
    && (chr != ' '); k++ ) {

    if (chr != ' ') {
    words[i][j] = (char) chr;
    j++;
    words[i][j] = null terminate; /* I cannot type null terminate */
    }
    else {
    words[i][j] = null terminate; /* I cannot type null terminate */

    i++;
    j = 0;
    }
    }

    /* At this moment 'i' is the upper value of array 'words'.
    In other words, 'i+1' is the total no. of words you input. */

    for (k = 0; k < i + 1; k++){
    printf("%s ", words[k]);

    /* wirte the conversion program */

    }

    return;
    }



    but I couldn't complie it... is there any errors? Pls point out for me....
    thx a lot.......

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it doesn't compile, then yes, there are errors. If you pay attention to your compiler, it actually tells you what they are.

    > void main()

    This is right for your usage:

    int main( void )

    > if (chr != ' ') {

    This will never execute because you've specified that if 'chr' is a space, then the loop will exit.

    A null terminator is the value of zero. Commonly already defined as the word NULL.

    Thus:

    char *string = NULL;


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed