Hey i need help with writing a simple program

Ok i have an assigment in my computer science 104 class in college it goes as follows:

Write a program that reads in a number supplied by the user and prints out that number in words. For example, if I were to type in:
125
The program should respond with:
one hundred twenty-five
Be careful to test this program so that it works with all numbers in the range 0 to 9999.

I need this to work with while loops and if statements only if possible, anything will help just a way i could start off trying to learn this. Thanks anyone,

Ryan Perrott

Comments

  • Okay, I'm going to brainstorm out loud here so...

    First, I'd convert the number to a string so:

    //num will be our number:

    char string[64], one[64], final[64];

    sprintf(string, "%i", num);

    Then, I'd test each individual "digit" now a character:

    while(i < strlen(string))
    {
    if(string[i] == '0')
    //it can be 0 - 9...
    strcpy(one, "Zero");
    strcat(final, one);

    }

    So, keep testing the digits until you are done. For each digit, make one equal to that number in words, then add it to the final string. Then, print out the final:

    cout >> final;

    or
    printf();

    Hope this gets you somewhere on track.

  • remember to do pseudocode and a flowchart before you start programming
    the clearer the idea ,the clearer the sequence of logical events,the easiest way to program.
    : Ok i have an assigment in my computer science 104 class in college it goes as follows:
    :
    : Write a program that reads in a number supplied by the user and prints out that number in words. For example, if I were to type in:
    : 125
    : The program should respond with:
    : one hundred twenty-five
    : Be careful to test this program so that it works with all numbers in the range 0 to 9999.
    :
    : I need this to work with while loops and if statements only if possible, anything will help just a way i could start off trying to learn this. Thanks anyone,
    :
    : Ryan Perrott
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories