我是Java程序员,但是目前正在C平台上解决一些在线问题。 当从控制台读取任何输入时,我想停止读取输入。 就像这样 输入1 2 3 4 输出1 2 3 4 但事前,我不知道我会读多少书。 你们中许多人可能都知道在线法官会从文件中获取输入。 那么, ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
例如:
printf("Please enter the amount of money you want to accumulate: ");
scanf("%f", &cashGoal);
printf("You would like to save up to $%.2f\n", cashGoal);
返回此:
Please enter the amount of money you want to accumulate: Program ended with exit code: 0123123
You would like to save up to $123123.00
编辑:这是完整的代码,这是我的家庭作业项目,所以它有点长
#include <stdio.h>
#include <math.h>
FILE *fp;
#define PENNY 0.01
int daystoCashGoal(float);
int main(void) {
fp = fopen("csis.txt", "w");
float cashGoal;
printf("Please enter the amount of money you want to accumulate: ");
fprintf(fp, "Please enter the amount of money you want to accumulate: ");
scanf("%f", &cashGoal);
printf("\n");
fprintf(fp,"\n");
printf("You would like to accumulate $%.2f.\n\n", cashGoal);
fprintf(fp, "You would like to accumulate $%.2f.\n\n", cashGoal);
daystoCashGoal(cashGoal);
fclose(fp);
return 0;
}
//Calculates the number of days it will take to accumulate the user-desired amount, doubles the deposit for each day, and sums up the balance for each day
int daystoCashGoal(float cashGoal){
int day = 1;
float dailyBalance = 0, dailyDeposit;
printf("DAY DEPOSIT BALANCE\n");
fprintf(fp, "DAY DEPOSIT BALANCE\n");
printf("___ _______ _______\n");
fprintf(fp, "___ _______ _______\n");
while (dailyBalance <= cashGoal) {
dailyDeposit = (pow( 2, day - 1)) * PENNY;
dailyBalance += dailyDeposit;
printf("%3d %5s %10.2f %5s %10.2f\n", day,"", dailyDeposit,"", dailyBalance);
fprintf(fp, "%3d %5s %10.2f %5s %10.2f\n", day,"", dailyDeposit,"", dailyBalance);
if (dailyBalance <= cashGoal) {
day ++;
}
else if (dailyBalance >= cashGoal) {
continue;
}
}
if (dailyBalance >= cashGoal) {
printf("\nIt took %d days to accumulate at least $%.2f.\n", day, cashGoal);
fprintf(fp, "\nIt took %d days to accumulate at least $%.2f.\n", day, cashGoal);
}
return day;
}
由于此项目已经过分级,因此我没有在寻求其他代码方面的建议,但是退出代码问题正在影响我的其他项目,并且我不确定自己做错了什么。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.