下面“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;
}
修改处见注释,供参考:
#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;
}
mark
字符串比较不是==,用strcmp函数
#include <string.h>
while(strcmp(y,posi)==0||strcmp(Y,posi)==0);
参数传递