Thread: simple question again

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    simple question again

    what im trying to do is ask the user to input some numbers say like : 1 2 3 4
    then have it output in english .. EX: one two three four.. whatever the order its in..

    i think im on the right track but its not working since after im done extracting the digits it = 0.


    sorry about the last post. i hope this is better
    Code:
     #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        
    int number,right,placecount;
    
    
    printf("\n%25cNumbers to words converter\n",0);
    printf("\nEnter the number:");
    
    scanf("%i", &number);
    
       
    
      do
      {
                right = number %10;
                number = number /10;
                ++placecount;
                }
                while(number!=0);
                
                do{
                                 switch(right)
                                 {
                                              case '0':
                                                   printf("zero\n");
                                                   break;
                                                         case '1':
                                                              printf("one\n");
                                                              break;
                                                              
                                                              default:
                                                                      break;
                                                                      }
                                                                      }
                                                                      
                while(placecount >0);
    
    system("PAUSE"); 
    return 0;
    
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your problem is you're processing your entire number in that first do/while loop. In the second do/while loop, right never changes so it'll print the same thing over and over again (whatever right ended up being at the end of the first loop).

    Maybe some proper indentation would help you see exactly what the ........ is going on in there...
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Unhappy

    so in the second loop whould i put the number extraction again?
    im very confused

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just make it one loop. The problem you're going to run into is, you're processing it from 1's place, but you'll want to print the most significant place first. Otherwise if you try processing 12345 you'll see: five four three two one
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    ok i recoded it and i still cant see why it doesnt work!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        
    int number,right,placecount;
    
    
    printf("\n%25cNumbers to words converter\n",0);
    printf("\nEnter the number:");
    
    scanf("%i", &number);
    
       
       do
       {
                right = number %10;
                number = number /10;
                
                
                switch(right)
                {
                             case '0':
                                  printf("zero\n");
                                  break;
                                  
                             case '1':
                                  printf("one\n");
                                  break;
                                  
                             default:
                                     break;
                                     }
                                     }
               while(number !=0);
               
               
    
    system("PAUSE"); 
    return 0;
    
    
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't make heads or tails out of what your algorithm to solve the problem, could be.

    You need to break down each number into it's componenets:

    ten thousands, thousands, hundreds, ten's, and one's.

    NOW you're ready to print out in english, some numbers:

    1,346 = "one thousand three hundred forty-six".

    One little trick I use to save a variable for later, in it's ORIGINAL value, is like this, early on in the program.

    onumber = number. Where "onumber" is the Original number. Then you can tear apart number, but still restore it easily like this:

    number = onumber

    If you could reduce your indentation to a more standard 2 to 4 spaces, it would help improve it's readability. Horizontal space on screen, is limited. Don't waste it.

    Adak

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The problem is you're checking for '0' instead of 0. '0' is not the same thing. When you compare it against '0' it's not the numeric value 0, it's the numeric representation of that character in whatever environment you're in.

    Try this program to see the difference:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("'0' = %d, 0 = %d\n", '0', 0);
      return 0;
    }
    Also, see the ASCII table here (since you're most likely in an ASCII environment): http://www.neurophys.wisc.edu/www/comp/docs/ascii.html

    You can see that '0' is 48 from the tiny little program I posted and it should match where 0 is in the ASCII table at that link.
    Last edited by itsme86; 09-07-2006 at 07:51 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    yes i can see that, then how do you case '0' then? and i replaced the 0 with 1 and 2 just to see if switch would pick it up and it didnt so the program is still not working =(
    thank you for your help

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why would you do a case for '0' when right will never be '0'? You should only have a case for expected values. The default case takes care of the rest.

    I'm not sure why it's not working for you...works fine for me when I change '0' to 0 and '1' to 1 in the cases. Well, except for a logic bug you have when the number entered ends in 0.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      int number, right, placecount;
    
      printf("\n%25cNumbers to words converter\n",0);
      printf("\nEnter the number:");
    
      scanf("%i", &number);
    
      do
      {
        right = number % 10;
        number = number / 10;
    
        switch(right)
        {
          case 0:
            printf("zero\n");
            break;
          case 1:
            printf("one\n");
            break;
          default:
            break;
        }
      } while(number !=0);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./numtoenglish
    
                            Numbers to words converter
    
    Enter the number:111
    one
    one
    one
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Talking

    i got it working! I had the understanding that when you used switch that the case had to be ' 1' instead of 1 . i now understand its different and produces different reults. and thats why case never recognised it.
    thank you guys for all your help, its much appreciated

  11. #11
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    but the only problem i have like that one guy said. when i type out 123
    i get the result three two one. when i want one two three!
    im sure this is a easy fix.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Kind of . . . you need something like this:
    Code:
    for(x = 0; x < digits; x ++) {
        for(y = 0; y < x; y ++) {
            number /= 10;
        }
    
        digit = number % 10;
        /* print digit */
        number = onumber;
    }
    digits could be calculated like so
    Code:
    for(x = 0; number /= 10; x ++);
    number = onumber;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    im more confused now than when i started with. i wanted to do this as simple as possible and with few loops as possible.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, sorry. (I did say "kind of". ) To print "one two three" for 123, you need to start at the highest digit and count down to the lowest. The first thing you need to do is figure out how many digits the number has. This can be accomplished with this loop:
    Code:
    for(digits = 0; number /= 10; digits ++);
    number = onumber;
    or
    Code:
    digits = 0;
    for(;;) {
        number = number / 10;  /* remove a digit */
        if(!number) {  /* no digits left */
            break;  /* end the loop */
        }
    }
    number = onumber;
    Then you need to somehow get the digitsth digit, and then the one less than that, etc. Sounds like another loop is required.
    Code:
    for(x = 0; x < digit; x ++) number /= 10;
    number %= 10;
    or
    Code:
    x = 0;
    for(;;) {
        if(x == digit) {
            number = number % 10;  /* remove digits higher than the current one */
            break;  /* end the loop */
        }
    
        number = number / 10;  /* remove the lowest digit */
    }
    But wait . . . you want to process all the digits, not just one. Sounds like another loop.
    Code:
    for(which = digits-1; which >= 0; which --) {
        /* process the whichth digit */
    }
    I don't see any other way for it; you need at least 3 loops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    thanks for all the help! i do understand where your coming from but i dont see how digits and onumber tie in to my program? digit is not defined as anything and i dont understand how its useful in my program. sorry for the confusion.
    maybe im not cut out for this

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM