2015-09-27 85 views
-3

这里是我的代码:基本功能生产无限循环

#include <stdio.h> 
#include <math.h> 
int main(void) 
{ 
    double x, y, z; 
    double numerator; 
    double denominator; 
    printf("This program will solve (x^2+y^2)/(x/y)^3\n"); 
    printf("Enter the value for x:\n"); 
    scanf("%lf", x); 
    printf("Enter the value for y:\n"); 
    scanf("%lf", y); 
    numerator = sqrt(x) + sqrt(y); 
    denominator = pow((x/y),3); 
    z = (numerator/denominator); 
    printf("The solution is: %f\n", z); 
    return(0); 

} 

谁能给我一个(希望)快速指针来解决我的无限循环?

+1

你应该通过'的scanf(“%LF”的变量中读取,&x);'scanf函数会修改程序中的变量的值,因此你总是需要在你的函数中传递一个变量的引用 –

+1

'sqrt(x)'返回平方根,不是平方的,使用'pow(x,2)'。 –

+6

代码中没有无限循环if处理*输入*时遇到问题,就这么说... –

回答

0

有在你的函数没有循环,所以我认为这是scanf()您的通话是导致该错误:

您需要通过参考scanf(),即使用scanf("%lf",&x)代替scanf("%lf",x)

顺便说一句,根据你的功能定义,你应该使用pow(x,2)而不是sqrt(x)它返回平方根。

0

因为这是你的第一个问题

**Welcome to stack overflow** 

您的代码犯规进入一个无限循环,有一个运行时错误。 你scanf函数的代码是有缺陷的利用这样的:

scanf("%lf",&x); 
scanf("%lf",&y); 

你想scanf的修改包含在你的value.Please的地址字段中的值读教程。

还可以使用

numerator=pow(x,2) + pow(y,2);//numerator=x^2+y^2 
0

它不是无限循环,你的代码只是返回无穷。这是因为scanf()需要一个指向变量的指针,它应该放置读取数字。要获取变量的地址,你可以使用&操作是这样的:

scanf("%lf", &x); 
scanf("%lf", &y);