Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1070375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:33:17+00:00 2026-05-16T20:33:17+00:00

Is there a way to store user inputs in switch case from one operation

  • 0

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;

`

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:33:19+00:00Added an answer on May 16, 2026 at 8:33 pm

    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:

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

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to store file creation time in linux? May be filesystem
Is there any way to store a session variable in OpenEdge SQL similar to
is there any way to store duplicate keys in a dictionary? I have a
Is there any way to store the generic parameter type passed in at construction
Is there a way to store values on client side permanently? I have a
Is there a way to store the state of objects in GXT? That is,
is there a way to store a class name in localstorage ?? If there
Is there a JavaScript way to store data that is unique to a browser
is there any easy way of store XML data into core data? Currently, my
I'd like to know if there is any way to store a series of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.