Thread: Number to Word (Billions)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by myphilosofi
    Here's what i've done so far..
    That code is terribly unwieldy. As Perspective suggested, you should break the code down into functions that can be reused.

    Quote Originally Posted by myphilosofi
    i don't know what to do if the user enters commas and a decimal point..
    Remove the commas as cyberfish suggested. If there is a decimal point, you need to take into account the cents.

    Quote Originally Posted by myphilosofi
    and my classmates say that scanf is not allowed. what should i do?
    Use fgets() instead. You could safely use scanf() if you provided a field width for the string format specifier, but as it stands your code is vulnerable to buffer overflow.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    here's another one:

    Code:
    void cents(char tenscent, char onescent, char salita[]){
    
            switch(tenscent){
                    case '0': specialcase2(onescent, salita);return; break;
                    case '1': specialcase(onescent, salita); return; break;
                    case '2': strcpy(salita, " and twenty "); break;
                    case '3': strcpy(salita, " and thirty "); break;
                    case '4': strcpy(salita, " and forty "); break;
                    case '5': strcpy(salita, " and fifty "); break;
                    case '6': strcpy(salita, " and sixty "); break;
                    case '7': strcpy(salita, " and seventy "); break;
                    case '8': strcpy(salita, " and eighty "); break;
                    case '9': strcpy(salita, " and ninety "); break;
            }
    
    
    int main(){
    
            char num[200] = "";
            char centwords[200] = "";
            char in;
            char outputwords[200] = "";
            char extrastring[200] = "";
            char extrastring2[200] = "";
            int counter = 0;
            int counternew = 0;
    
            while (in != '\n'){
                    in = getchar();
                    if ((in >= '0') && (in <= '9')){
                            num[counter] = in;
                            counter = counter + 1;
                    }
            }
    
            cents(num[counter - 2], num[counter -1], centwords);
    
    
            while(counternew <= (counter - 1)){
                    whole(num[counternew], extrastring, counter, num[counternew + 1]);
                    strcat(extrastring2, extrastring);
                    counternew = counternew + 1;
                    counter = counter - 1;
            }
    
    
            strcat(extrastring2, centwords);
            printf("%s\n", extrastring2);
    
            return 0;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  4. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 09:56 AM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM