简体   繁体   English

开关盒操作员 - C.

[英]Switch-Case Operator — C

Is there a way to store user inputs in switch case from one operation and use it across switch operations at run-time. 有没有办法在一个操作中将用户输入存储在交换机案例中,并在运行时跨交换机操作使用它。

Example: If its a software for a Bank and I want to take information from the user and validate if his a/c number is correct and also check if he has enough bank balance to withdraw money. 示例:如果它是银行的软件,我想从用户那里获取信息并验证他的账号是否正确,还要检查他是否有足够的银行余额来取款。

I need to know how to store the value of one operation,so that I could use it for further ops. 我需要知道如何存储一个操作的值,以便我可以将它用于进一步的操作。

 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;

    }

I also tried declaring variables in the switch(ops) to store the value in them(like in the following case to validate the a/c number in the next step but it doesn't help.) 我还尝试在switch(ops)中声明变量以将值存储在它们中(如下例所示,以便在下一步中验证a / c号但它没有帮助。)

Edited code: 编辑代码:

` `

                            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;

` `

Why not declare your variables outside the switch. 为什么不在交换机外声明变量。 You could even put braces around the switch to prevent the variables from leaking to the surrounding function, like this: 您甚至可以在开关周围放置大括号以防止变量泄漏到周围的函数,如下所示:

// code from the surrounding function
{
char ac_no; 
long amt,amt2,init_dep; 
char name,ac_allocated; 

switch(ops) 
   { 
   case OpenAC: 
      ...
   }
} // end of block

我不确定我是否理解你的问题,但是如果你在switch语句的大括号内声明了某些内容,当你点击结束的大括号时它将超出范围,并且在下次遇到switch语句时不可用。

First issue: you're using the wrong type for ac_no, name, ac_allocated, and ac1. 第一个问题:你使用了错误的类型ac_no,name,ac_allocated和ac1。 You're obviously wanting to store strings at these locations, so instead of a plain char , you need to declare arrays of char: 你显然希望存储字符串在这些位置,所以不是一个普通的char ,你需要声明字符数组:

char ac_no[AC_NO_SIZE];
char name[NAME_SIZE];
char ac_allocated[AC_ALLOCATED_SIZE];
char ac1[AC1_SIZE];

where each *_SIZE is large enough to hold your input data plus 1 extra character for the 0 terminator. 其中每个*_SIZE足够大,可以保存输入数据,还有0个终结符的额外字符。 IOW, if your account numbers are at most 10 characters long, AC_NO_SIZE needs to be 11. IOW,如果您的帐号长度最多为10个字符,则AC_NO_SIZE必须为11。

Second issue: do not declare variables at the head of a switch statement; 第二个问题:在switch语句的头声明变量; any initializations will be skipped. 任何初始化都将被跳过。

Third issue: auto variables declared inside a specific scope will not be available outside of that scope; 第三个问题:在特定范围内声明的自动变量在该范围之外是不可用的; they will cease to exist when that scope exits. 当该范围退出时,它们将不复存在。 None of the variables you declare will be available outside of the switch statement. 您声明的变量都不会在switch语句之外可用。

Fourth issue: if this switch operation is inside of a function, and you want to preserve these values between function calls, you can do one of three things: 第四个问题:如果此开关操作在函数内部,并且您希望在函数调用之间保留这些值,则可以执行以下三个操作之一:

  • Declare these items at file scope, outside of any function (not recommended): 在任何函数之外的文件范围内声明这些项目(不推荐):
     char ac_no[AC_NO_SIZE]; char name[NAME_SIZE]; char ac_allocated[AC_ALLOCATED_SIZE]; char ac1[AC1_SIZE]; long amt, amt2, init_dep; void foo() { int ops; ... ops = bar(ops); ... } int bar(int ops) { switch(ops) { case OpenAC: printf("Name: "); fflush(stdout); scanf("%s", name); // note no &; true for all char [] types printf("A/C no allocated: "); fflush(stdout); scanf("%s", ac_allocated); printf("Initial deposit: "); fflush(stdout); scanf("%ld", &init_dep); ... } return ops; ... 
  • Declare these items as `static` in the function (slightly less recommended); 在函数中将这些项声明为`static`(稍微不推荐); they will not be visible outside of the function, but their value will be retained between function calls: 它们在函数外部不可见,但它们的值将在函数调用之间保留:
     int bar(int ops) { static char ac_no[AC_NO_SIZE]; static char name[NAME_SIZE]; static char ac_allocated[AC_ALLOCATED_SIZE]; static char ac1[AC1_SIZE]; static long amt, amt2, init_dep; switch(ops) { case OpenAC: printf("Name: "); fflush(stdout); scanf("%s", name); printf("A/C no allocated: "); fflush(stdout); scanf("%s", ac_allocated); printf("Initial deposit: "); fflush(stdout); scanf("%ld", &init_dep); ... } return ops; } 
  • Declare these items in the calling function and pass them as parameters to this function (recommended): 在调用函数中声明这些项,并将它们作为参数传递给此函数(推荐):
     int foo(int ops, char *ac_no, char *name, char *ac_allocated, char *ac1, long *amt, // Since these values are being modified in the function, long *amt2, // we must pass pointers to them, otherwise the changes long *init_dep) // will not be reflected in the calling function { switch(ops) { case OpenAC: printf("Name: "); fflush(stdout); scanf("%s", name); // no & before name; same is true for rest of parameters printf("A/C no allocated: "); fflush(stdout); scanf("%s", ac_allocated); printf("Initial deposit: "); fflush(stdout); scanf("%ld", init_dep); // no & before init_dep since it's already a pointer ... 

All of this assumes I'm understanding your problem correctly. 所有这些都假设我正确地理解了你的问题。

Maybe you should use structure to hold account data and functions to make it readable and maintainable. 也许你应该使用结构来保存帐户数据和功能 ,使其可读和可维护。

struct account_s { /* Fields borrowed to @John Bode */
    char ac_no[AC_NO_SIZE];
    char name[NAME_SIZE];
    char ac_allocated[AC_ALLOCATED_SIZE];
    char ac1[AC1_SIZE];
    long amt, amt2, init_dep;
};

int openAC(struct account_s *account);

to be continued... :) 未完待续... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM