Thread: Decimal place range. simple help needed

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    Decimal place range. simple help needed

    How do i make the program to print Sorry... if user enter a 4 decimal place number???

    Code:
    # include <stdio.h>
    
    main()
    {
    	float length;
    	float cm;
    	float m;
    	float km;
    	float inch;
    	float yard;
    	float mile;
    
    	printf("Metric-Imperial Conversion\n\n");  /* This is the program name */
    
    	printf("This program allows values up to 3 decimal place\n");
    	printf("Get Length: ");    /*user input values to be convertered*/
    	scanf("&#37;f" , &length); 
    	
    	if ( length < 0 )    /*How do i make the program to print Sorry... if user enter a 4 decimal place number???*/
    	{
    	
    		printf("\aSorry, Pls re-enter value \n");
    	
    	}
    
    	else {
    
    	cm = length * 2.5400;
    	m = length * 0.91440;
    	km = length * 1.60934;
    
    	printf("\n\nFrom %.3f Inch to CM: %.3f \n", length, cm);
    	printf("From %.3f Yard to Metres: %.3f \n", length, m);
    	printf("From %.3f Miles to KM: %.3f \n\n\n", length, km);
    
    	inch = length * 0.393700;
    	yard = length * 1.093613;
    	mile = length * 0.621371;
    
    	printf("From %.3f CM to Inch: %.3f \n",length, inch);
    	printf("From %.3f Meters to Yard: %.3f \n",length, yard);
    	printf("From %.3f KM to Mile: %.3f \n\n", length, mile);
    
    	printf("Have a Good Day!!\n");
    	}
    }
    Last edited by Chris0724; 09-09-2008 at 02:38 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By 4 decimal place number, do you mean 1000 or do you mean 0.0001? If the first, that's pretty straightforward. If the second, that's not.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Multiply the user's value by 1000.0, convert to an int, convert back to float, and then divide by 1000.0.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    By 4 decimal place number, do you mean 1000 or do you mean 0.0001? If the first, that's pretty straightforward. If the second, that's not.
    hi,

    i mean some thing like the 0.0001. If user inputed this values, can it print the sorry statement?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    Quote Originally Posted by Dino View Post
    Multiply the user's value by 1000.0, convert to an int, convert back to float, and then divide by 1000.0.
    hi dino,

    i am sorry but i don't get you.. why is there a need to *1000 convet to int and back to float then divide again?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Chris0724 View Post
    hi dino,

    i am sorry but i don't get you.. why is there a need to *1000 convet to int and back to float then divide again?
    Because you want to round off/truncate at the third decimal point.

    BTW: It is not possible to store (say) 0.001 precisely inside a computer even if you wanted to (unless you're using BCD, but you probably aren't), so this whole thing is somewhat irrelevant. If you're worried about how things print, just use print formats to control how many decimals are printed.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    4
    ok.. thanks a lot for the advice

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Another option is to read in a character string instead of a float, parse the value to validate it for no more than 3 decimal places, and then use a function like sscanf(), then convert to float if the number is valid.
    Last edited by Dino; 09-09-2008 at 03:16 PM. Reason: typos
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    Another option is to read in a character string instead of a float, parse the value to validate it for no more than 3 decimal places, and then use a function like sscanf(), then convert to float if the number is valid.
    That, in my mind is the only thing you can do.

    However, what is the point of doing that. If the user enters 0.00001, then what happens? It just outputs 0.000 - which is the approximate distance in the new measure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with decimal place printing
    By jrb47 in forum C++ Programming
    Replies: 11
    Last Post: 11-15-2006, 10:21 PM
  2. Replies: 29
    Last Post: 10-25-2005, 09:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. deciaml place help
    By artgirloc in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2002, 07:47 PM
  5. good place for some simple c++ source?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-05-2002, 04:45 PM