Thread: distance formula help

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    distance formula help

    I've made a small program to calculate the distance between two points. However, the answer keeps coming up as '0.00'. I seem to always run into problems doing math equations such as this in c. I'd appreciate any advise, thank you

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <ctype.h>
    // use toupper() and tolower() with <ctype.h>
    
    /*		Program: Find the distance between two points
    		Resources:
    			Distance Formula = sqrt(pow(x_2 - x_1, 2) + pow(y_2-y_1, 2))
    
    
    */
    int main(void)
    {
    	char progAuth; // program authorization (yes or no)
    	double xVal1, xVal2; //both x values
    	double yVal1, yVal2; //both y values
    	double ans; // the answer
    	
    	printf("I'd like to find the distance between two points. May I? (y/n): \n");
    	scanf("%c", &progAuth); 
    	
    	progAuth = toupper(progAuth);
    	
    	if(progAuth == 'Y')
    	{
    		printf("Enter number for x1: ");
    		scanf("%lf", &xVal1);
    		printf("Enter number for y1: ");
    		scanf("%lf", &yVal1);
    		printf("Enter number for x2: ");
    		scanf("%lf", &xVal2);
    		printf("Enter number for y2: ");
    		scanf("%lf", &yVal2);
    		
    		ans = sqrt(pow(xVal2 - xVal1, 2.0) + pow(yVal2 - yVal1, 2.0));
    		
    		printf("\n\nYour answer is: %.2lf\n", ans);
    		printf("Goodbye!");
    		
    		
    	}
    	else
    		printf("Goodbye!\n");
    		
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Rem out the authorization part of it (including the scanf() for it), and see if that fixes it.
    /*
    Rem'd out code in here

    */

    Your C code for calculating the distance works fine, in very limited testing, otherwise.

    My theory is that the newline left over from the first scanf() for authorization, is goofing up the first double you try to enter.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fflush vs gets
    Read this. make sure you understand it.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by Bayint Naung View Post
    fflush vs gets
    Read this. make sure you understand it.
    So basically it is saying do not use scanf because of the lingering \n? If so, how can I reformat this program without using scanf?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Arex Bawrin View Post
    So basically it is saying do not use scanf because of the lingering \n? If so, how can I reformat this program without using scanf?
    Add this line, right under the first scanf():

    getchar();

    and your troubles will be over.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Bayint Naung View Post
    fflush vs gets
    Read this. make sure you understand it.
    Quote Originally Posted by Adak View Post
    Add this line, right under the first scanf():

    getchar();

    and your troubles will be over.
    That's not the problem. He is only using scanf() for input, not scanf() and gets(). As bad as scanf() can be, if you only use scanf() to read input, the way input is handled is consistent. Instead you compile OP's code:

    Code:
    C:\Documents and Settings\Owner\My Documents\sandbox\main.c|4|warning: C++ style comments are not allowed in ISO C90|
    C:\Documents and Settings\Owner\My Documents\sandbox\main.c|4|warning: (this will be reported only once per input file)|
    C:\Documents and Settings\Owner\My Documents\sandbox\main.c||In function 'main':|
    C:\Documents and Settings\Owner\My Documents\sandbox\main.c|37|warning: ISO C90 does not support the '%lf' ms_printf format|
    ||=== Build finished: 0 errors, 4 warnings ===|
    I suspect that this one is what is the problem. So OP, learn how to print a double. Cprogramming.com FAQ > Format output using printf() (C)

    On my system, in spite of the warning, it seems to work:
    Code:
    I'd like to find the distance between two points. May I? (y/n):
    y
    Enter number for x1: 10
    Enter number for y1: 21
    Enter number for x2: 12
    Enter number for y2: 3
    
    
    Your answer is: 18.11
    Goodbye!
    The moral being that warnings are as bad as errors. They could have a profound effect on something for someone, like the OP, or be perfectly fine, like it is for me, in this case. But the warning is pointing out that something could go wrong. So you want to compile on a high warning setting.
    Last edited by whiteflags; 02-25-2011 at 11:55 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    For C99, %lf for double is fine.
    Only for C89, but quite a number of compilers support it anyway.
    Edit: yes this is not the problem.
    But doesn't hurt to know.
    Last edited by Bayint Naung; 02-25-2011 at 12:19 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And I deny neither of those facts. How could I, since my own build logs are telling me as much. Still, I think it is clearly making a difference here.

    edit - actually, I amend my comments. %lf is for long double, not double, so no, it is not "fine" for printf even in C99.
    Last edited by whiteflags; 02-25-2011 at 12:37 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.9
    For C99 standard, you should be able to find in this forum...


    — %lf conversion specifier allowed in printf
    Edit: I agree that this could be the reason. if OP is not using C99 compiler.
    Edit:

    L A following a, A, e, E, f, F, g, or G conversion corresponds to
    a long double argument. (C99 allows %LF, but SUSv2 does not.)
    Not sure which man pages you are reading. from here perhaps?

    According to the C99 spec, %lf and %f are equivalent, and both can be used for float and double.
    For long double, %Lf should be used. As long as long double and double are equivalent, %lf will work too, however...
    Last edited by Bayint Naung; 02-25-2011 at 12:41 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's a laugh:

    I tested it on the old Turbo C compiler, ver. 1.01, which pre-dates anything from the C standard, maybe? It used the AT&T standard, and supports %lf for printing doubles.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Bayint Naung View Post
    Not sure which man pages you are reading.
    What you quoted applies to long double. Are you trying to say that long double and double are the same? They aren't, necessarily.

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    %lf or %f for double is OK in C99
    %lf is not for Long double. it's non-standard!
    Use %Lf for long double.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    ........ me can we all shut up now. I don't know if C99 is being compiled here. I would hope not, since apparently otherwise the OP's compiler is FUBAR. I'm sorry my reading of the man pages did not meet everyone's standards, but imagine how I feel. I'm getting tired of defending perfectly reasonable advice, especially since the only other advice that's been given here today is totally irrelevant.

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Quote Originally Posted by whiteflags View Post
    ........ me can we all shut up now. I don't know if C99 is being compiled here. I would hope not, since apparently otherwise the OP's compiler is FUBAR. I'm sorry my reading of the man pages did not meet everyone's standards, but imagine how I feel. I'm getting tired of defending perfectly reasonable advice, especially since the only other advice that's been given here today is totally irrelevant.
    Yes, you are right. and I've admitted it in previous already.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure, you admitted you were wrong, but did you admit it using the correct "penitents" font, eh?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database setup for searching based on string distance
    By Mario F. in forum General Discussions
    Replies: 4
    Last Post: 02-24-2010, 06:15 AM
  2. Segfaulting Distance Program
    By radiohead in forum C Programming
    Replies: 2
    Last Post: 01-09-2006, 08:48 PM
  3. Distance Formula in my program..... I need help fast!!!
    By Mackology101 in forum C Programming
    Replies: 3
    Last Post: 09-23-2004, 10:10 PM
  4. Distance Formula Implecations: Urgent!
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 03-20-2004, 10:52 PM
  5. Fuzzy Logic
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-13-2002, 04:58 PM