Thread: scanf and float value

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    Question scanf and float value

    why is it that if i put %.2f in the scanf line i get garbage in the conversion ? i have a %.2f in the printf line below to get a 2 decimal value so i thought it has to be the same for the scanf. with %f it works fine.

    // Convert Inches to Millimeters
    {
    float inch;

    printf("Enter inch value: ");
    scanf("%f", &inch);
    printf("\nThat is equal to %.2f millimeters",inch*25.4);

    return 0;
    }

    thanx
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you allowing decimal input for inches? They're entering "2.345346 inches"? I would just use an integer.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66
    what if someone decides to enter 2.25 inches ?
    will it be ok to use int ? will int ignore after the decimal point ?
    i wil try it out to see. however, to answer your question, yes. the inch values will be up to 4 decimal. and the conversion to mm has to stay within 2 decimal accuracy.
    is there a way to not print unnecacary 0's ? eg for 2.10 print 2.1 instead ? but retain 2 decimal accuracy for when needed ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading in char for float with scanf
    By ramparts in forum C Programming
    Replies: 3
    Last Post: 11-05-2006, 01:05 AM
  2. Pass by reference
    By mcgeady in forum C Programming
    Replies: 11
    Last Post: 02-17-2005, 03:01 AM
  3. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  4. I'm stuck and I dont know how to go any farther
    By Bobit in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-08-2003, 02:38 AM
  5. Newton-Raphson number squaring method/pointers
    By inakappeh in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 11:04 AM