Spelling Out Numbers

This is a discussion on Spelling Out Numbers within the C Programming forums, part of the General Programming Boards category; Code: //Program requires user to enter a number //program should then print out the name of each digit //I cant ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Spelling Out Numbers

    Code:
    //Program requires user to enter a number
    //program should then print out the name of each digit
    //I cant seem to get my program to work it keeps spelling out
    //each digit as "zero"
    //does anyone see any mistakes? 
    //sorry Im a newbie at this.
    
    #include<stdio.h>
    #include <math.h>
    int main(void)
    {
      
      
      long long int n,k,i,d,a;
      
      
      
      printf("please enter your phone number: ");
      scanf("%llu",&n);
      a=n;
      
        for ( i=0; n>0; i++)            //for loop    
            {  n=n/10;  }
        
        for (k=i; k>=1; k--)
           { d=n/pow(10,k-1);       
        
      
      switch (n)
        {
      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; }
      
      a=a-d*pow(10,k-1);
      
      }
      
      return(0);
      }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    d=n/pow(10,k-1);
    switch (n)

    Are you sure?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Do you know about / are you allowed to use arrays?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    No, we have not learned arrays yet.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have no idea what you're trying to do with pow(), OK.

    Normal idiom would be to % 10, and save the result, for use in your switch statement.

    You can't save it as n, (maybe name it digit), because you're using n<0; as your test condition in the for loop.

    Then divide n by 10, and loop back.

    logic:
    Code:
    while n is > 0 (for loop is fine, also)
       digit = n mod 10
       switch (digit) {
       };
       n = n divided by 10
    end of loop

    While i, j, n and x, are very standard variable names for simple loop counter or indexers, or numbers, using single letters which have no bearing on the use of the variable, is a terrible idea. Make your variable names *meaningful* names.

    We have the memory to add a great deal of clarity to our programs, by doing this. Why not use it?

    P.S. It doesn't matter where you divide n by 10, as long as you do it AFTER the mod statement.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Think about when this loop will stop:
    Code:
        for ( i=0; n>0; i++)            //for loop    
            {  n=n/10;  }
    It will stop when n is not greater than zero. What cases are those? Well considering that n is only ever reduced in magnitude and thus will not become negative, the only option is zero.
    Ooops!?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Both run OK.

    Code:
    #include <stdio.h>
    
    int main(void) {
      
       int n=1234567, digit;
    
       printf("\nWhile loop:\n");
       while(n>0) {
          digit = n % 10;
          printf("%d\n", digit);
          n /= 10;
       }
       //or
       n = 1234567;
       printf("\nFor loop:\n");
       for(;n>0;) {
          digit = n % 10;
          printf("%d\n", digit);
          n /= 10;
       }
       return 0;
    }
    Last edited by Adak; 09-26-2011 at 04:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement Spelling Checker
    By anirban in forum Tech Board
    Replies: 6
    Last Post: 03-09-2011, 04:46 PM
  2. spelling checker
    By Tom.b in forum C Programming
    Replies: 8
    Last Post: 11-12-2009, 10:17 PM
  3. Please help :( problem on check spelling...
    By toxicherry in forum C Programming
    Replies: 4
    Last Post: 12-28-2007, 11:47 PM
  4. IDEA: Spelling Checker
    By fletch in forum Contests Board
    Replies: 14
    Last Post: 08-17-2002, 11:20 PM
  5. Spelling mistakes
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 03-05-2002, 07:24 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21