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;
}