【发布时间】:2018-10-22 20:11:21
【问题描述】:
我正在尝试编写一个程序来计算您的更改并报告总金额。我能够编写函数来计算变化,但我不确定如何让它在循环中运行。另一件事是,当程序询问他们的姓名时,我希望用户按 Enter 或返回退出,但我也不确定如何。这是我的第一堂编程课,我正在努力变得更好。感谢您的宝贵时间。
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
float countChange(int quarters, int dimes, int nickles, int pennies);
int main(void)
{
int a,b,c,d;
char yourname[20];
printf("Your total money is $ %0.2f \n", countChange(12,23,34,45));
printf("What is your name (Return/Enter to quit)?");
scanf("%s", yourname);
printf("\nHow many quarters do you have? \n" );
scanf("%d", &a);
printf("\nHow many dimes do you have? \n" );
scanf("%d", &b);
printf("\nHow many nickles do you have? \n" );
scanf("%d", &c);
printf("\nHow many pennies do you have? \n" );
scanf("%d", &d);
printf("All counted, %s has $ %0.2f\n", yourname, countChange(a,b,c,d));
return 0;
}
float countChange(int quarters, int dimes, int nickles, int pennies)
{
float QuartersTotal, DimesTotal, NicklesTotal, PenniesTotal, total;
QuartersTotal= quarters*0.25;
DimesTotal= dimes*0.10;
NicklesTotal= nickles*0.05;
PenniesTotal= pennies*0.01;
total= QuartersTotal+ DimesTotal+ NicklesTotal+ PenniesTotal;
return total;
}
【问题讨论】:
-
你想把哪个部分放入循环中?
-
@CoffeeTableEspresso 我希望程序一直询问用户他们的姓名和他们拥有的硬币数量,直到他们按 Enter 或 Return 退出。谢谢。
-
好的,我已经将我的答案更新为循环,直到他们不输入他们的名字。希望这会有所帮助!