I'm having problems with this program. Everytime i enter a fractional decimal number, it displays -0.0000 or sometimes garbage values.

Ex:
Enter any fractional decimal number: 5.7
-2.888blah blah blah garbage

another Ex:
Enter any fractional decimal number: 25.7
-0.000000

#include<stdio.h>

int main(){

    long double fraDecimal,fraBinary,bFractional = 0.0,dFractional,fraFactor=0.1;
    long int dIntegral,bIntegral=0;
    long int intFactor=1,remainder,temp,i;

    clrscr();

    printf("Enter any fractional decimal number: ");
    scanf("%Lf",&fraDecimal);

    dIntegral = fraDecimal;
    dFractional =  fraDecimal - dIntegral;

    while(dIntegral!=0){
     remainder=dIntegral%2;
     bIntegral=bIntegral+remainder*intFactor;
     dIntegral=dIntegral/2;
     intFactor=intFactor*10;
    }

   for(i=1;i<=6;i++){

       dFractional = dFractional * 2;
       temp =  dFractional;

       bFractional = bFractional + fraFactor* temp;
       if(temp ==1)
         dFractional = dFractional - temp;

       fraFactor=fraFactor/10;
   }

   fraBinary =  bIntegral +  bFractional;
   printf("Equivalent binary value: %lf",fraBinary);

   getch();
}

Edited 2 Years Ago by EarhawkPH

im using C++ . I dont do the cin cout thingy, just from a normal turbo c.

I think that long double is not supported in many (most?) C compilers ...

So use someting like this:

#include <stdio.h>
#include <ctype.h>


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}


double takeInDbl( const char* msg )
{
    double dbl;
    for( ; ; )
    {
        printf( msg );
        if( scanf( "%lf", &dbl) == 1
            && getchar() == '\n' )
            break;

        /* else ... if reach here ... */

        printf( "\nInvalid entry ... "
                "only numbers allowd here ... \n\n" );
        while( getchar() != '\n' );
    }
    return dbl;
}



int main()
{
    do
    {
        double decimal = takeInDbl( "Enter a decimal: " );
        printf( "You entered %f\n", decimal );
    }
    while( more() ) ;

    return 0;
}

Edited 2 Years Ago by David W

I think that long double is not supported in many (most?) C compilers ...

long double is required to be supported. However, on some platforms it has the same range as double, so the two types are synonymous in functionality in those cases.

I'm more inclined to say that the OP's problem is an artifact of Turbo C being a dinosaur. And since I'm not about to install Turbo C on my machine, and modern compilers don't exhibit the problem, troubleshooting and fixing the code is difficult.

Just saying ...

that if I was using a MinGW compiler set to C90 or C99 ... I observed these messages:

C:\MinGW\examples\test_long_double\checkOutDouble.c In function 'takeInDbl':
28 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] unknown conversion type character 'L' in format [-Wformat=]
28 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] too many arguments for format [-Wformat-extra-args]
C:\MinGW\examples\test_long_double\checkOutDouble.c In function 'main':
48 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] unknown conversion type character 'L' in format [-Wformat=]
48 9 C:\MinGW\examples\test_long_double\checkOutDouble.c [Warning] too many arguments for format [-Wformat-extra-args]

And ... an example run: (Note erroneous output ...)

Enter a decimal: 123
You entered 0.000000
More (y/n) ?
Enter a decimal: 4567890
You entered 0.000000
More (y/n) ?
Enter a decimal: 98765.4321
You entered 0.000000
More (y/n) ?

See also:

http://en.wikipedia.org/wiki/Long_double

... With the GNU C Compiler, long double is 80-bit extended precision on x86 processors regardless of the physical storage used for the type ...

http://www.mingw.org/

Enter a decimal: 123
You entered 0.000000
More (y/n) ?
Enter a decimal: 456789
You entered 0.000000
More (y/n) ?

Edited 2 Years Ago by David W

This article has been dead for over six months. Start a new discussion instead.