Thread: Problem starting a homework problem for class. Please help!

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Problem starting a homework problem for class. Please help!

    Well I have started writing a code to create this program that i have to have done by tommorow but i am stuck at the moment. Anybody please help!!

    Here is the problem:

    Write a prgram that converts numbers to words. For examlpe, 834 should result in "eight three four." Likewise, 1697 should convert to "one six seven nine." You may assume that the input is less than one million. (Hint: remember the % operator!).

    Is there anybody that can help me out with this?? Thanks!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    What have you tried?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Posted in other topic.

    Quote Originally Posted by King Mir

    Here's a hint:

    1893%10=3
    1893/10=189
    189%10=9
    189/10=18
    etc.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    So how do I make the actual conversion from number to text?

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    http://cboard.cprogramming.com/searc...earchid=543735

    Look at that, that's the result of the board search for "numbers to words". You're likely to find an answer there.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    you use either if-else or switch():

    example:

    Code:
    switch(num) {
       case 1: puts("one"); break;
       case 2: puts("two"); break;
       .....
       default: puts("Invalid Input"); break;
    }
    where num holds the value returned by the modulo operation, not the division.

    also, in the loop:

    Code:
    while((input % 10) != 0) {
       num=input%10;
       input=input/10;
       switch(num) {
       ...
       }
    }
    hope this helps.
    It is not who I am inside but what I do that defines me.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    13
    I have came up with this code but everything prints out backwards. EX. I input 123 and it will output three, two, one. How do I fix this part?

    Here is what I have so far:

    Code:
    #include "stdafx.h"
    int _tmain(int argc, _TCHAR* argv[])
    {
    int number, input;
    printf("Please enter a number: ");
    scanf("%d", &input);
    while ((input < 0.0) || (input > 1000000.0))
    {
    printf("\nError! Enat a number between 0 and 1,000,000! Try again: ");
    scanf("%d", &input);
    printf("\n");
    }
    while((input % 10) != 0) {
    number=input%10;
    input=input/10;
    switch(number) {
    case 1: puts("one"); break;
    case 2: puts("two"); break;
    case 3: puts("three"); break;
    case 4: puts("four"); break;
    case 5: puts("five"); break;
    case 6: puts("six"); break;
    case 7: puts("seven"); break;
    case 8: puts("eight"); break;
    case 9: puts("nine"); break;
    default: puts("Invalid Input"); break;
    } 
    }
    getchar();
    getchar();
    return(0);
    }

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    [input] is uninitialized. Turn on all warnings in your compiler. If the compiler doesn't say a word, it's about time to switch.
    Why compare [input], which is an int, to 1000000.0, which is a floating-point number?
    Why use double getchar()s? Why use getchar() at all? Run your program from command line, like all real men do.
    You can also remove the default: case; [number] should never be any other value.

    You can put the results so far into an array, then output the array in reverse. Assuming, of course, that you've learned arrays already...
    Last edited by jafet; 10-09-2006 at 12:02 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Quote Originally Posted by jafet
    [input] is uninitialized. Turn on all warnings in your compiler. If the compiler doesn't say a word, it's about time to switch.
    Why compare [input], which is an int, to 1000000.0, which is a floating-point number?
    Why use double getchar()s? Why use getchar() at all? Run your program from command line, like all real men do.
    You can also remove the default: case; [number] should never be any other value.

    You can put the results so far into an array, then output the array in reverse. Assuming, of course, that you've learned arrays already...
    1. Input doesn't NEED to be initialized.

    2. I agree that comparing an int to a float is wrong, and should be changed.

    3. I assume he IS running his program from command line "like all real men", and thats EXACTLY why he's using getchar(), so it doesnt (compile. close.) in an instant.

    4. Is there something wrong with safe habits like always putting a default? I think not

    5. Im sure he's thankful for the suggestion, but the opposite for you elitist attitude.

    Sorry, had to do that...

  10. #10
    Registered User
    Join Date
    Sep 2006
    Location
    Iowa
    Posts
    14
    Cpre 185 at ISU eh? lol. Btw, I'm also having he same problem that my words print out backwards. How can you fix this? No, I haven't learned arrays yet.
    Last edited by Jayhawk_26; 10-09-2006 at 12:38 AM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by JDD
    3. I assume he IS running his program from command line "like all real men", and thats EXACTLY why he's using getchar(), so it doesnt (compile. close.) in an instant.

    4. Is there something wrong with safe habits like always putting a default? I think not

    5. Im sure he's thankful for the suggestion, but the opposite for you elitist attitude.
    3. Have you seen this? It gets asked more times than I'd like to remember.

    4. In general, no. But it does show a fundamental misunderstanding of a simple operator.

    5. Sometimes the best help is accompilshed using a blunt instrument to jump start the organ between the ears.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    25
    Quote Originally Posted by Dave_Sinkula
    3. Have you seen this? It gets asked more times than I'd like to remember.
    It was never asked in this thread, getchar() works, and it doesnt knock performance, so why NOT use it.

    4. In general, no. But it does show a fundamental misunderstanding of a simple operator.
    Not necessarily , it likely shows a simple habit.

    5. Sometimes the best help is accompilshed using a blunt instrument to jump start the organ between the ears.
    But most of the time the best help is accomplished with understand and compassion.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    1) Yes, I was wrong about that.

    3) Do you even know what a command line is?

    4) The code between default: and break; can be removed. It bloats code.

    5) Some programming courses don't allow students to use constructs which have not yet been taught. (Unfortunately, yes.)

    Stop playing Robin Hood, mercilessly spoonfeeding the poor and meek. I am here to solve problems, not settle differences.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    and Nothing Else Matters
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    117
    zac_haryy, if you havent discussed arrays yet, why not have 5 integer variables (since integers can only have a max value of 64k sumthin? you could then assign them sentinel values like (-1) for instance, you then store those "switched" numbers in them, then simply switch and display them in reverse order if they dont hold the sentinel value.
    It is not who I am inside but what I do that defines me.

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This is a sample where you can just read it in as a char string using correct functions. Also it filters out the numbers even if the string also contains some non-numeric characters.
    Code:
    #include <stdio.h>
    int main(){
    	char buffer[256];
    	fgets(buffer,255,stdin);
    	int i;
    	for(i=0;buffer[i]!='\0';i++){
    		switch(buffer[i]) {
    			case '0': printf("zero "); break;
    			case '1': printf("one "); break;
    			case '2': printf("two "); break;
    			case '3': printf("three "); break;
    			case '4': printf("four "); break;
    			case '5': printf("five "); break;
    			case '6': printf("six "); break;
    			case '7': printf("seven "); break;
    			case '8': printf("eight "); break;
    			case '9': printf("nine "); break;
    		} 
    	}
    	fgets(buffer,255,stdin);
    }
    Read about scanf:
    http://www.gidnetwork.com/b-59.html
    http://www.gidnetwork.com/b-62.html
    http://www.gidnetwork.com/b-63.html
    http://www.gidnetwork.com/b-64.html
    Last edited by maxorator; 10-09-2006 at 06:38 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Me with this homework problem Please!!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2002, 07:54 PM
  2. C Homework Problem
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 03:43 AM
  3. Homework problem
    By bacon in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 07:21 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM