【问题标题】:Switch-Case Operator -- CSwitch-Case 运算符 -- C
【发布时间】: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


【解决方案1】:

为什么不在开关之外声明你的变量。您甚至可以在开关周围放置大括号,以防止变量泄漏到周围的函数中,如下所示:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      第一个问题:您为 ac_no、name、ac_allocated 和 ac1 使用了错误的类型。您显然希望在这些位置存储字符串,因此您需要声明 char 数组,而不是普通的 char

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

      每个*_SIZE 都足够大,可以容纳您的输入数据加上 0 终止符的 1 个额外字符。 IOW,如果您的帐号长度最多为 10 个字符,则 AC_NO_SIZE 需要为 11。

      第二个问题:不要在 switch 语句的开头声明变量;任何初始化都将被跳过。

      第三个问题:在特定范围内声明的自动变量在该范围外将不可用;当该范围退出时,它们将不复存在。您声明的所有变量都不能在 switch 语句之外使用。

      第四个问题:如果这个 switch 操作在函数内部,并且你想在函数调用之间保留这些值,你可以做以下三件事之一:

      • 在文件范围内声明这些项目,在任何函数之外(不推荐):
        
        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;
          ...
        
      • 在函数中将这些项目声明为 `static`(稍微不推荐);它们在函数外部不可见,但它们的值将在函数调用之间保留:
        
        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;
        }
        
      • 在调用函数中声明这些项目并将它们作为参数传递给此函数(推荐):
        
        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
               ...
        

      所有这些都假设我正确理解了您的问题。

      【讨论】:

        【解决方案4】:

        也许您应该使用结构来保存帐户数据和函数以使其可读和可维护。

        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);
        

        待续... :)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-02
          • 1970-01-01
          • 1970-01-01
          • 2020-08-16
          相关资源
          最近更新 更多