views:

164

answers:

4

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

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;

`

+7  A: 

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
Patrick
+3  A: 

I'm not sure I understand your problem, but if you declare something inside the curly brackets of the switch statement, it will be out of scope when you hit the ending curly bracket and not usable the next time the switch statement is encountered.

teukkam
+2  A: 
John Bode
A: 

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... :)

levif