简体   繁体   中英

How to read floating-point numbers by pairs in C?

Write a program that requests two floating-point numbers and prints the value of their difference divided by their product. Have the program loop through pairs of input values until the user enters nonnumeric input. Use a function to return the value of the calculation.

I've successfully completed this exercise without using function but can't get it right using function. The program itself runs but doesn't return any value in fact it crashes.

Please any help would be appreciated.

Here is my program:

#include <stdio.h>
#include <string.h>

double result (double x, double y);

int main(void)
{
     double num1, num2, res;
     printf("This while calculate difference of two numbers by their product.\n");
     printf("Enter first number followed by second number\n");

     while (scanf("%lf %lf", &num1, &num2 ==2))
     {
         res= result(num1, num2);
         printf("the result is equal to %.3g\n", res);
         printf("Enter next set of numbers or q to quit\n");
     }
     return 0;
}
double result(double x, double y)
{
    double output;
    output = (y-x)/(x*y);
    return output;
}
while (scanf("%lf %lf", &num1, &num2 ==2))

本来应该是:

while (scanf("%lf %lf", &num1, &num2) ==2)

Try changing

  while (scanf("%lf %lf", &num1, &num2 ==2))

to

 while (scanf("%lf %lf", &num1, &num2) ==2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM