Thread: input reading problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    31

    input reading problem

    i entered this simple code from a book and its not working.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void main()
    {
     char input[20];
     double miles, kilometers;
    
     printf("Enter a value in miles:\n");
     miles=atof(gets(input));
    
     kilometers = miles*1.609;
    
     printf("%.2f miles works out to %.2f kilometers", miles, kilometers);
    }

    its multiplying correctly and everything but it reads the wrong thing. ill put in a number of miles and it always comes out to 37813992.

    and this is off topic but what is the point of double variables?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    %f won't work for doubles, you'll have to use %lf

    A double is a floating point datatype, like float, but unlike float it takes up double the amount of bits, 64, with float being 32 bits.
    A double can have 15 decimals compared to a float's 7 and can store numbers in roughly 10 times farther the range of a float.
    Last edited by OnionKnight; 02-11-2006 at 09:33 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    Code:
    #include <stdio.h>
    #include <math.h>
    
    void main()
    {
     char input[20];
     double miles, kilometers;
    
     printf("Enter a value in miles:\n");
     miles=atof(gets(input));
    
     kilometers = miles*1.609;
    
     printf("%.2lf miles works out to %.2lf kilometers", miles, kilometers);
    }

    i put that in and its doing the same thing. it only reads 37813992. and i tried changing them to floats but it still did the same thing. any idea what is still wrong?

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I compiled it and it works for me with both %f and %lf.
    Code:
    Enter a value in miles:
    1
    1.00 miles works out to 1.61 kilometers
    Are you using and old compiler?
    Also, you should use int main instead of void main.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    i use the new version of bloodshed. its free and a lot of source codes from my book dont work with it lol. i use windows so i use void and just start>run>exe my programs. is it possible that its just my compiler? if so..could you possibly give me a link to a decent, free one?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You have one of the best, and it happens to be free.
    Code:
    #include <stdlib.h>
    Visit the FAQ. Consider tossing the book.
    Last edited by Dave_Sinkula; 02-11-2006 at 10:20 PM. Reason: Backfilled a link to the FAQ.
    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.*

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    31
    ha...wow. it works now and i feel like an idiot. oh well. thanks for the help!

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Ah that's right, atof() is declared in stdlib.h and not in stdio.h.
    What happened was that since your compiler did not know how the atof() function looks like it assumes that it returns an int, when it actually returns a double. This will cause the compiler to convert this "int" to a double. And usually floating point values when represented as an integer has very high values which can explain the high value in your result.

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> What happened was that since your compiler did not know how the atof() function looks like it assumes that it returns an int, when it actually returns a double. This will cause the compiler to convert this "int" to a double. And usually floating point values when represented as an integer has very high values which can explain the high value in your result.

    How did it find a suitable atof to link with in the first place? Surely, stdio.h doesn't just define some atof returning an int to fool newbies.

  10. #10
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by OnionKnight
    %f won't work for doubles, you'll have to use %lf

    A double is a floating point datatype, like float, but unlike float it takes up double the amount of bits, 64, with float being 32 bits.
    A double can have 15 decimals compared to a float's 7 and can store numbers in roughly 10 times farther the range of a float.
    You use %f for doubles and floats with printf, not %lf.

    The float passed to printf automatically gets promited to a double, which is why there's only one format specifier (%f). This is not the case with scanf, because you pass a pointer, not the value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not reading file input :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 02:55 PM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 09:41 AM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  5. Problem with reading strings
    By goron350 in forum C++ Programming
    Replies: 8
    Last Post: 06-05-2005, 12:05 AM