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;
`
First issue: you’re using the wrong type for ac_no, name, ac_allocated, and ac1. You’re obviously wanting to store strings at these locations, so instead of a plain
char, you need to declare arrays of char:where each
*_SIZEis large enough to hold your input data plus 1 extra character for the 0 terminator. IOW, if your account numbers are at most 10 characters long, AC_NO_SIZE needs to be 11.Second issue: do not declare variables at the head of a switch statement; 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.
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:
All of this assumes I’m understanding your problem correctly.