如何在c中的for循环中使用嵌套的if-else语句

如何在c中的for循环中使用嵌套的if-else语句,c,for-loop,if-statement,nested,C,For Loop,If Statement,Nested,我的for声明有问题。我试图在里面有一个嵌套的if-else语句,并且使用指针。我什么都试过了,在互联网上到处找。我在有错误的行旁放置了注释,但如果您看到其他错误,请告诉我。多谢各位 #include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 void getinput(double*xptr, int*nptr) { int flag; do { flag =

我的for声明有问题。我试图在里面有一个嵌套的if-else语句,并且使用指针。我什么都试过了,在互联网上到处找。我在有错误的行旁放置了注释,但如果您看到其他错误,请告诉我。多谢各位

#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0

void getinput(double*xptr, int*nptr)
{
int flag;
    do
    {
      flag = TRUE;
      printf("What is the value of x and the number of terms:");
      scanf("%lf %i", xptr, nptr);
      if (*nptr <= 0)
      {
        printf("The number of terms must be positive\n");
        flag = FALSE;
      }
    }
    while(flag == FALSE);
}

double sinHyper(double *xptr, int *nptr) {
    int i;
    double sum;
    double ti;
    i = 0;
    ti = 0;
    for (i = 0; i < *nptr; i = i+1)// I'm getting a Warning: comparioson between pointer and integer
    {
        if (i == 0)
        {
            sum = xptr;
        } else {
            ti = 2*i+1;
            ti = ti*2*i;
            ti = (xptr*xptr)/ti;// I'm getting a error: invalid operands to binary * (have 'double*' and 'double*')
            sum = ti*sum;
        }
    }
    return (sum);
}

void main() {
   int n;
   double x;
   double sinhx;
   getinput(&x, &n);
   sinhx = sinHyper(&x, &n);
   printf("For an x of %.0f with %i terms the sinh(x) is %f", x, n, sinhx);
    return 0;
}
#包括
#包括
#定义真1
#定义FALSE 0
void getinput(双*xptr,int*nptr)
{
int标志;
做
{
flag=TRUE;
printf(“x的值和术语的数量:”);
scanf(“%lf%i”,xptr,nptr);

如果(*nptr您忘记在几个地方取消引用指针

这一行编译的事实

sum = xptr;
不应误导您:C允许您将指针转换为仅带有警告的数字,而在大多数情况下这是一个错误

sum = *xptr;
它不允许对指针进行乘法运算,因此对指针进行平方运算的表达式是错误的:

(xptr*xptr)
您应该取消对指针的引用两次,即写入

((*xptr)*(*xptr))
或者为
*xptr
的当前值创建一个单独的变量,并使用它:

const double x = *xptr;
ti = (x*x)/ti;
注意:这个练习应该纯粹是理论上的,因为
sinHyper
不会改变
*xptr
*nptr
。因此,您应该将它们作为值传递,而不是作为指针传递:

double sinHyper(const double x, const int n) {
    ...
}
...
sinhx = sinHyper(x, n);

我改变了一切,现在我得到一个警告:我的操作可能在for上没有定义line@Cplan代码的修改版本非常适合我()我修改了<代码> i= i+1 到一个更常见的代码> i++。现在它工作了,结果是我在计算中犯了一个错误,这就是为什么我没有得到我想要的值,但是非常感谢你帮了我很大的忙。点击旁边的灰色复选标记。这会让其他网站访问者知道你的问题已经解决,并在堆栈溢出时为你赢得一个新的徽章。