ads3579 2022-05-30 23:33 采纳率: 69.2%
浏览 90
已结题

自定义函数无法运行,如何解决?(语言-c语言)

下面“programrepeat”函数单独拿出来运行没问题,但是和前两个函数一起运行就运行不了。大家帮帮忙
#include<stdio.h>
#include<math.h>

double interest(double amount, int years, double interest_rate)
{
return amount * pow(1 + interest_rate, years);
}

int untilDoubling(double interest_rate)
{
int untilDoubling = round(log(2)/log(1+interest_rate));//Aufrunden
return untilDoubling;
}

int programrepeat(charquestion,charpossible,char*positive)
{
int programrepeat;
while(*positive!='y'&&*positive!='Y'&&*positive!='n'&&*positive!='N')
{
printf("%s\n",question);
scanf("%s",positive);
}

programrepeat=1;
return programrepeat;

}

int main(void)
{

double betrag, zinssatz, auszahlung;
int jahre, jahre_wait;
char *ques,*poss,*posi;

do
{
printf("What amount do you want to invest? ");
scanf("%lf", &betrag);
fflush(stdin);

printf("What is the interest rate?");
scanf("%lf", &zinssatz);
fflush(stdin);

printf("How long should the term of the savings contract be? ");
scanf("%d", &jahre);
fflush(stdin);

auszahlung = interest(betrag, jahre, zinssatz);//Funktionsaufruf

printf("You get: %lf\n", auszahlung);
jahre_wait = untilDoubling(zinssatz);//Funktionsaufruf
printf("You need %d years to double your money\n",jahre_wait);

ques = "Wollen Sie noch eine Berechnung durchfuehren?";
printf("%s\n",ques);
scanf("%s",posi);
programrepeat(ques,poss,posi);
}
while(*posi=='y'||*posi=='Y');

return 0;

}

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2022-05-31 10:34
    关注

    修改处见注释,供参考:

    #include <stdio.h>
    #include <math.h>
    
    double interest(double amount, int years, double interest_rate)
    {
        return amount * pow(1 + interest_rate, years);
    }
    
    int untilDoubling(double interest_rate)
    {
        int untilDoubling = round(log(2) / log(1 + interest_rate));//Aufrunden
        return untilDoubling;
    }
    
    int programrepeat(char* question, char* possible, char* positive)//修改
    {
        int programrepeat;
        do{                               //修改
            printf("%s\n", question);
            getchar();                  //修改
            scanf(" %c", &(*positive));  //修改
        }while (*positive != 'y' && *positive != 'Y' && *positive != 'n' && *positive != 'N');
        programrepeat = 1;
        return programrepeat;
    }
    
    int main(void)
    {
        double betrag, zinssatz, auszahlung;
        int   jahre, jahre_wait;
        char* ques, * poss,  posi; //修改 * posi
        do{
            printf("What amount do you want to invest? ");
            scanf("%lf", &betrag);
            fflush(stdin);
    
            printf("What is the interest rate?");
            scanf("%lf", &zinssatz);
            fflush(stdin);
    
            printf("How long should the term of the savings contract be? ");
            scanf("%d", &jahre);
            fflush(stdin);
    
            auszahlung = interest(betrag, jahre, zinssatz);//Funktionsaufruf
    
            printf("You get: %lf\n", auszahlung);
            jahre_wait = untilDoubling(zinssatz);//Funktionsaufruf
            printf("You need %d years to double your money\n", jahre_wait);
    
            ques = "Wollen Sie noch eine Berechnung durchfuehren?";
            //printf("%s\n", ques);   //修改
            //scanf("%s", posi);      //修改
            programrepeat(ques, poss, &posi);
        } while (posi == 'y' || posi == 'Y');
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月8日
  • 已采纳回答 5月31日
  • 创建了问题 5月30日

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码