Thread: simple question again

  1. #16
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well there's a much simpler way to do this: Just keep the input as a string!

    Here, one loop:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char *english[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
      char input[BUFSIZ];
      int i;
    
      printf("Enter the number: ");
      fflush(stdout);
      fgets(input, sizeof(input), stdin);
    
      for(i = 0;input[i] && input[i] != '\n';++i)
        if(isdigit(input[i]))
          puts(english[input[i] - '0']);
        else
          puts("unknown");
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./numtoenglish2
    Enter the number: 1234567890,123948
    one
    two
    three
    four
    five
    six
    seven
    eight
    nine
    zero
    unknown
    one
    two
    three
    nine
    four
    eight
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

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

    Thumbs up

    thank you and it works great ! except i wanted to do it the other way by number extraction. i guess it cant be done. thanks guys!

  3. #18
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well yeah it can be done the other way. It just requires you to think.
    If you understand what you're doing, you're not learning anything.

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

    Unhappy

    oh ok i guess thats something i cant do thanks!

  5. #20
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You give up so easily! There's 2 obvious solutions:

    1) Process the number from the highest place down to the 1's place
    2) Store the values from the 1's place to the highest place and then print them out in reverse order

    Someone already showed you a solution using option 1, but you make it sound like it's impossible to do it that way.
    If you understand what you're doing, you're not learning anything.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Quote Originally Posted by itsme86
    You give up so easily! There's 2 obvious solutions:

    1) Process the number from the highest place down to the 1's place
    2) Store the values from the 1's place to the highest place and then print them out in reverse order

    Someone already showed you a solution using option 1, but you make it sound like it's impossible to do it that way.
    i guess im not ready for that yet. dont know what to say i looked over the early code on option 1 but when i extracted the right most digit i couldnt figuare out how to store it and then extract the next digit and store that without replacing the first one. make sense?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int right_digit,number;
    printf("enter the number\n");
    scanf("%i",&number);  // number  = 123
    
       while(number !=0)
       {
           right_digit = number % 10; //extracts 3
           number/=10; //get the next digit = 2
           
           // at this point could i store 2 in right_digit without replacing the first stored number?
           // if i can store it in reverse then i could use another loop to grab 
           //the rightmost digit and display it ? because
           //by the time its done looping number = 0..  im failing on how 
           //to do this even though you guys probobly already told me a thousand times =(
            
            }
            
      
      
      system("PAUSE");	
      return 0;
    }

  7. #22
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    heres what i tried to do but dont work obvious reasons im sure!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      
      
      int right_digit,number;
      int newnum=0;
    
    printf("enter the number\n");
    scanf("%i", &number);  // number  = 123
    
       while(number !=0)
       {
           right_digit = number % 10;          //extracts 3
           
           number/=10;         //get the next digit = 2
           newnum = newnum + right_digit;  
            
            }
            
            while(number!=0)
            {
               right_digit = newnum %10;
               newnum = newnum /10;
               
            switch(right_digit)
            {
                 case 1:
                      printf("one\n");
                      break;
                 
                 case 2:
                      printf("two\n");
                      break;
                      
                 default:
                         break;
                              
      
      printf("new number = %i", right_digit); }
    }
     
      
      system("PAUSE");	
      return 0;
    }

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well for one thing the closing brace that terminates your switch statement is misplaced, putting the printf() in a case statement after the break, so it never executes.

    Code:
       while(number !=0)
       {
           right_digit = number % 10;          //extracts 3
           
           number/=10;         //get the next digit = 2
           newnum = newnum + right_digit;  
            
            }
    In this code (besides working on the indentation) you need to multiply newnum by 10 each time, otherwise, if the number is 123, newnum will be 1+2+3=6 instead of 321.
    Code:
        while(number !=0)
        {
            right_digit = number % 10;          //extracts 3
            
            number/=10;         //get the next digit = 2
            newnum = newnum + right_digit;  
            newnum *= 10;
        }
    If you do that, and number is 123, then newnum will be 321 (assuming you initialize newnum to zero, which you do).

    I think you should take itsme86's suggestion and read the input as a string, not as a number. That way the digits are already in the right order and all you have to do is parse the string with a for loop.
    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.

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Sorry if this adds more confusion, but recursion can make the "reversing" simple.
    Code:
    #include <stdio.h>
    
    void foo(int value)
    {
       static const char *digit[] = 
       {
          "zero", "one", "two", "three", "four", 
          "five", "six", "seven", "eight", "nine",
       };
       if ( value > 0 )
       {
          foo(value / 10);
          printf("%s ", digit [ value % 10 ] );
       }
    }
    
    int main(void)
    {
       foo(135);
       putchar('\n');
       return 0;
    }
    
    /* my output
    one three five 
    */
    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.*

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