Thread: Using Double...

  1. #1
    Registered User
    Join Date
    Aug 2008

    Using Double...

    I made this code for show how many digits have a number, example...

    Enter a number: 546
    The number have 3 digits.

    with int he works fine, but i canīt make he work with double.

     double n, c;
     char resposta = 's';
       printf("          ****************************************************\n");
       printf("          *   Infomar a quantidade de digitos de um numero:  *\n");
       printf("          ****************************************************\n");
       printf("\n Digite um numero: ");
     	scanf("%lf", &n);
        for(c = 0; n; n /= 10)
    	printf("\n ----> O numero possui %lf digitos.", c);
       printf(" Deseja Continuar S/N?: ");
    	scanf( "\n %c", &resposta );
      }while(toupper(resposta) == 'S');

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    What's the problem? Guess: it never ends.

    Because if you have 12.2 inserted you will have:
    12.2 / 10 = 1.22 / 10 = 0.122 / 10 = 0.0122 etc etc. So n will alwasy be !=0 thus true that the while loop will never end. You might want n < 0 or n < 0.0001 (something small).


  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    London, UK
    Quote Originally Posted by C_ntua View Post
    or n < 0.0001 (something small).
    This is called an epsilon value and is essential when comparing doubles to just about any value.

    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM