【发布时间】:2010-09-06 12:03:28
【问题描述】:
有没有办法将用户输入从一个操作存储在 switch case 中,并在运行时跨 switch 操作使用它。
示例:如果它是银行的软件,我想从用户那里获取信息并验证他的 a/c 号码是否正确,并检查他是否有足够的银行余额来取款.
我需要知道如何存储一个操作的值,以便我可以将它用于进一步的操作。
switch(ops)
{
char ac_no;
long amt,amt2,init_dep;
char name,ac_allocated;
case OpenAC:
{
printf("1.Name:\n");
scanf("%s",&name);
printf("2.A/Cno_allocated:\n");
scanf("%s",&ac_allocated);
printf("3.Initial deposit:\n");
scanf("%d",&init_dep);
break;
}
case Deposit:
{
printf("Enter the a/c number: ");
scanf("%s",&ac_no);
printf("Amount:Rs. ");
scanf("%ld",&amt);
break;
}
case Withdraw:
{
printf("Enter the a/c number: ");
scanf("%s",&ac_no);
printf("Amount:Rs. ");
scanf("%ld",&amt2);
{printf("Cannot withdraw.Rs.500 minimum balance mandatory.\n");}
break;
}
return ops;
}
我还尝试在 switch(ops) 中声明变量以将值存储在其中(如下例所示,在下一步中验证 a/c 编号,但没有帮助。)
修改后的代码:
`
char ac_no;
long amt,amt2,init_dep,dep1;
char name,ac_allocated,ac1;
case OpenAC:
{
printf("1.Name:\n");
scanf("%s",&name);
printf("2.A/Cno_allocated:\n");
scanf("%s",&ac_allocated);
ac_allocated = ac1;
printf("3.Initial deposit:\n");
scanf("%d",&init_dep);
init_dep = dep1;
//break;
}
case Deposit:
{
printf("Enter the a/c number: ");
scanf("%s",&ac_no);
if(ac_no == ac1)
{
printf("Amount:Rs. ");
scanf("%ld",&amt);
}
break;
`
【问题讨论】:
-
顺便说一句,Linux 标签具有误导性,这里似乎没有任何特定于 Linux 的内容。
-
删除了“linux”标签。
-
好的。我刚刚添加了,因为我在 Linux 上编程。对不起
标签: c switch-statement