Using Double...

This is a discussion on Using Double... within the C Programming forums, part of the General Programming Boards category; I made this code for show how many digits have a number, example... Enter a number: 546 The number have ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    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.

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    
    main()
    {
     double n, c;
     char resposta = 's';
    
      do
      {
       
       printf("\n\n");
       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)
         c++;
    
    	printf("\n ----> O numero possui %lf digitos.", c);
    
       
       printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
       printf(" Deseja Continuar S/N?: ");
    	scanf( "\n %c", &resposta );
    	system("cls");
      }while(toupper(resposta) == 'S');
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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).

    Cheers

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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.

    QuantumPete
    "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, 04:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 03: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, 09:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM

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