#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Cool Need Some Help! Rookie :)


    Hello i have this program and i need some help to fix it,
    i think the problem is on command " puts(name[digit + 20 ]) "
    it is working fine until 109 but after this something is wrong
    this the program:
    /*count.c*/
    int putchar(int value);
    int puts(const char *string);

    char *name[] = {
    "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
    "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
    "sixteen", "seventeen", "eighteen", "nineteen",
    "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
    "eighty", "ninety", "one hundred", "one thousand" ,
    };

    char *itoa10(unsigned long num)
    {
    static char buf[12];
    int i;
    buf[10] = 0;
    for (i = 9; i >= 0; --i)
    {
    buf[i] = (char)((num % 10) + '0');
    num /= 10;
    }
    return buf;
    }

    void number_text(unsigned long number)
    {
    int digit;
    puts(itoa10(number));
    puts(": ");

    if(number >= 20)
    {
    digit = number / 10;
    puts(name[digit + 20 ]);
    putchar(' ');
    number %= 10;
    }


    puts(name[number]);
    putchar ('\r');
    putchar ('\n');
    }


    int main()
    {
    unsigned long number=0, i=0;
    int am;
    number = 80;
    am=1143;
    for(i = 80; i <= am ; i++)
    {
    number_text(number);
    number ++;
    // number *= 3;
    //++number;
    }
    system ("pause");
    }

    any help will be good! thanks :)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Trouble occurs well before the segfault.

    0000000109
    :
    one hundred
    nine

    0000000110
    :
    one thousand


    Were this my project I would start with a sound program that could write numbers through 99.

    Next I'd extend it to write all the numbers from 1 to 999, keeping "one" separate from "hundred" whereas you have a "one hundred" string.

    3rdly, I'd enable groups of thousands incorporating "thousand", "million", "billion". These would call on the write1to999 function to work with integers mod 1000.

    Fourthly, depending on if you started from the most significant group or the least significant you may need to rearrange the groups to print them in the correct order.

    >>> import WriteNumber
    >>> WriteNumber.WriteNumber(0)
    'zero'
    >>> WriteNumber.WriteNumber(-23421882342342882238828832987243987)
    'negative twenty-three decillion four hundred twenty-one nonillion eight hundred eighty-two octillion three hundred forty-two septillion three hundred forty-two sextillion eight hundred eighty-two quintillion two hundred thirty-eight quadrillion eight hundred twenty-eight trillion eight hundred thirty-two billion nine hundred eighty-seven million two hundred forty-three thousand nine hundred eighty-seven'
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    re:


    the thing is that the only solution i can apply is had to be only by modify the current program..
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,703
    Rep Power
    480
    Retain

    int main()
    {


    That part is good.

    Comments on this post

    • dwise1_aol disagrees : Yet he does not return a value. That part is bad.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo