C 我的do while循环循环未正确循环
我可能给出的已经够多了,但长话短说,我正在开发一个ATM机程序,我试图将“switch”语句放在循环中的主函数中,这样用户就可以获得更多的事务 我遇到了一个问题,我想存100,但当我检查余额时,它仍然是0。我知道其他一切都很好,但这个循环让我很难受,我非常感谢任何帮助C 我的do while循环循环未正确循环,c,do-while,C,Do While,我可能给出的已经够多了,但长话短说,我正在开发一个ATM机程序,我试图将“switch”语句放在循环中的主函数中,这样用户就可以获得更多的事务 我遇到了一个问题,我想存100,但当我检查余额时,它仍然是0。我知道其他一切都很好,但这个循环让我很难受,我非常感谢任何帮助 不要介意所有额外的东西,它只是为了给我一个关于我在做什么的想法 int main () { char option; float balance ; int count = 1; option =
不要介意所有额外的东西,它只是为了给我一个关于我在做什么的想法
int main ()
{
char option;
float balance ;
int count = 1;
option = displayMenu();
do
{
switch (option)
{
case 'D':
getDeposit(balance);
main();
count++;
break;
case 'W':
getWithdrawal(balance);
main();
count++;
break;
case 'B':
displayBalance(balance);
main();
count++;
break;
case 'Q':
printf("Thank you!");
break;
main();
}
} while ( count <= 5);
return 0;
}
char displayMenu()
{
char option;
printf("\n Welcome to HFC Federal Credit Union \n");
printf("\n Please select from the following menu: \n ");
printf("\n D: Make a deposit \n ");
printf("\n W: Make a withdrawal \n ");
printf("\n B: Check your account balance \n ");
printf("\n Q: To quit \n ");
scanf("\n%c" , &option);
return option;
}
float getDeposit(float balance)
{
float deposit;
printf("\n Please enter the amount you want to deposit! ");
scanf("%f" , &deposit);
balance += deposit;
return balance;
}
float getWithdrawal(float balance)
{
float withdrawal;
printf("\n Please enter the amount you want to withdraw! ");
scanf("%f" , &withdrawal);
balance -= withdrawal;
return balance;
}
void displayBalance(float balance)
{
printf("\n Your current balance is %f " , balance);
}
int main()
{
字符选项;
浮动天平;
整数计数=1;
选项=显示菜单();
做
{
开关(选件)
{
案例“D”:
取得存款(结余);
main();
计数++;
打破
案例“W”:
提取(余额);
main();
计数++;
打破
案例“B”:
显示天平(天平);
main();
计数++;
打破
案例‘Q’:
printf(“谢谢你!”);
打破
main();
}
}而(count您在循环的每次迭代中都递归调用main()
。只要删除此调用,就可以开始了
您还需要将函数的返回值分配给balance
,否则它们将无法影响其值。您尚未更改main()中的变量。
您可以将循环更改为:
do
{
switch (option)
{
case 'D':
balance = getDeposit(balance);
count++;
break;
case 'W':
balance = getWithdrawal(balance);
count++;
break;
case 'B':
displayBalance(balance);
count++;
break;
case 'Q':
printf("Thank you!");
break;
}
} while (count <= 5);
do
{
开关(选件)
{
案例“D”:
余额=获得存款(余额);
计数++;
打破
案例“W”:
余额=取款(余额);
计数++;
打破
案例“B”:
显示天平(天平);
计数++;
打破
案例‘Q’:
printf(“谢谢你!”);
打破
}
}虽然(count这段代码有很多问题……以下是我的主要观点(但不是全部,我只是回答问题):
> p>你一遍又一遍地调用main,为了简单起见,你可以认为这是每次重新启动应用程序(除了栈问题,我忽略和其他讨厌的副作用)。
您没有初始化余额(和好友)变量。它们可能包含“垃圾”数据
您正在忽略所使用函数的返回值。如果您未使用指针,则应使用赋值
你的菜单打印功能出了问题…我怀疑这是否是你想要的
下面是一个快速修复(未经测试):
intmain(){
字符选项;
浮动余额=0;
整数计数=1;
做{
option=displayMenu();//已移动到循环中。
开关(选件){
案例“D”:
余额=获得存款(余额);
计数++;
打破
案例“W”:
余额=取款(余额);
计数++;
打破
案例“B”:
平衡=显示平衡(平衡);
计数++;
打破
案例‘Q’:
printf(“谢谢你!”);
打破
}
}而(count我认为主要问题是循环外开关控制变量的更新。
以结尾回应“Q”是有必要的……那么只允许5就没有必要了。
我还修复了其他一些问题;并对它们发表了评论。
我稍微提高了可测试性(5->6)。我保持计数,只是扩展到6,以便允许完整的测试“D100,B,W50,B,Q”。
顺便说一句,从函数返回平衡值,而不是使用指针或全局变量,这是一个很好的设计。但是您需要使用返回值,而不是忽略它
/* include necessary headers, do not skip this when making your MCVE */
#include <stdio.h>
/* prototypes of your functions,
necessary to avoid the "immplicitly declared" warnigns
when compiling "gcc -Wall -Wextra"; which you should
*/
char displayMenu(void);
float getDeposit(float balance);
float getWithdrawal(float balance);
void displayBalance(float balance);
/* slightly better header of main, with "void" */
int main (void)
{
char option;
float balance=0.0; /* initialise your main variable */
int count = 1;
/* your very important update of the switch control variable has been moved ... */
do
{
option = displayMenu(); /* ...here */
/* If you do not update your switch variable inside the loop,
then it will forever think about the very first command,
this explains most of your problem.
*/
switch (option)
{
case 'D':
balance=getDeposit(balance); /* update balance */
/* removed the recursive call to main(),
it is not needed as a solution to the problem that the program
always uses the first command (when updating inside the loop)
and otherwise just makes everything much more complicated and
risky.
*/
count++;
break;
case 'W':
balance=getWithdrawal(balance); /* update balance */
count++;
break;
case 'B':
displayBalance(balance);
count++;
break;
case 'Q':
printf("Thank you!");
/* adding a way to get out of the loop,
using a magic value for the count,
this is a mehtod frowned upon by most,
but it minimises the changes needed to your
own coding attempt.
*/
count=0;
break;
}
} while ( (count <= 6)&&(count>0) ); /* additionally check for the magic "Q" value
check against count<=6, to allow testing D,B,W,B,Q */
return 0;
}
/* use explicitly empty parameter list for functions */
char displayMenu(void)
{
char option;
printf("\n Welcome to HFC Federal Credit Union \n");
printf("\n Please select from the following menu: \n ");
printf("\n D: Make a deposit \n ");
printf("\n W: Make a withdrawal \n ");
printf("\n B: Check your account balance \n ");
printf("\n Q: To quit \n ");
scanf("\n%c" , &option);
return option;
}
float getDeposit(float balance)
{
float deposit;
printf("\n Please enter the amount you want to deposit! ");
scanf("%f" , &deposit);
balance += deposit;
return balance;
}
float getWithdrawal(float balance)
{
float withdrawal;
printf("\n Please enter the amount you want to withdraw! ");
scanf("%f" , &withdrawal);
balance -= withdrawal;
return balance;
}
void displayBalance(float balance)
{
printf("\n Your current balance is %f " , balance);
}
/*包括必要的标题,制作MCVE时不要跳过此项*/
#包括
/*你的功能原型,
避免“不恰当声明”警告的必要性
在编译“gcc-Wall-Wextra”时,您应该
*/
字符显示菜单(无效);
浮动存款(浮动余额);
浮动取款(浮动余额);
无效余额(浮动余额);
/*略好的总管,带有“空隙”*/
内部主(空)
{
字符选项;
浮动余额=0.0;/*初始化主变量*/
整数计数=1;
/*您对开关控制变量的重要更新已被移动*/
做
{
option=displayMenu();/*…此处*/
/*如果不更新循环内的开关变量,
然后它会永远想着第一个命令,
这就解释了你的大部分问题。
*/
开关(选件)
{
案例“D”:
余额=获取存款(余额);/*更新余额*/
/*已删除对main()的递归调用,
该程序不需要作为问题的解决方案
始终使用第一个命令(在循环内部更新时)
否则只会让一切变得更复杂
有风险的
*/
计数++;
打破
案例“W”:
余额=获取取款(余额);/*更新余额*/
计数++;
打破
案例“B”:
显示天平(天平);
计数++;
打破
案例‘Q’:
printf(“谢谢你!”);
/*添加了一种摆脱循环的方法,
使用魔法值进行计数,
这是一种大多数人都不赞成的方法,
但它最大限度地减少了对您的应用程序所需的更改
自己的编码尝试。
*/
计数=0;
打破
}
}while((计数0));/*另外检查神奇的“Q”值
对照count检查您为什么一直调用main()
?…这不是意味着
/* include necessary headers, do not skip this when making your MCVE */
#include <stdio.h>
/* prototypes of your functions,
necessary to avoid the "immplicitly declared" warnigns
when compiling "gcc -Wall -Wextra"; which you should
*/
char displayMenu(void);
float getDeposit(float balance);
float getWithdrawal(float balance);
void displayBalance(float balance);
/* slightly better header of main, with "void" */
int main (void)
{
char option;
float balance=0.0; /* initialise your main variable */
int count = 1;
/* your very important update of the switch control variable has been moved ... */
do
{
option = displayMenu(); /* ...here */
/* If you do not update your switch variable inside the loop,
then it will forever think about the very first command,
this explains most of your problem.
*/
switch (option)
{
case 'D':
balance=getDeposit(balance); /* update balance */
/* removed the recursive call to main(),
it is not needed as a solution to the problem that the program
always uses the first command (when updating inside the loop)
and otherwise just makes everything much more complicated and
risky.
*/
count++;
break;
case 'W':
balance=getWithdrawal(balance); /* update balance */
count++;
break;
case 'B':
displayBalance(balance);
count++;
break;
case 'Q':
printf("Thank you!");
/* adding a way to get out of the loop,
using a magic value for the count,
this is a mehtod frowned upon by most,
but it minimises the changes needed to your
own coding attempt.
*/
count=0;
break;
}
} while ( (count <= 6)&&(count>0) ); /* additionally check for the magic "Q" value
check against count<=6, to allow testing D,B,W,B,Q */
return 0;
}
/* use explicitly empty parameter list for functions */
char displayMenu(void)
{
char option;
printf("\n Welcome to HFC Federal Credit Union \n");
printf("\n Please select from the following menu: \n ");
printf("\n D: Make a deposit \n ");
printf("\n W: Make a withdrawal \n ");
printf("\n B: Check your account balance \n ");
printf("\n Q: To quit \n ");
scanf("\n%c" , &option);
return option;
}
float getDeposit(float balance)
{
float deposit;
printf("\n Please enter the amount you want to deposit! ");
scanf("%f" , &deposit);
balance += deposit;
return balance;
}
float getWithdrawal(float balance)
{
float withdrawal;
printf("\n Please enter the amount you want to withdraw! ");
scanf("%f" , &withdrawal);
balance -= withdrawal;
return balance;
}
void displayBalance(float balance)
{
printf("\n Your current balance is %f " , balance);
}