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

自定义函数无法运行,如何解决?(语言-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日

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同