-2

I'm trying to work on a project where I'm forbidden from using global variables. In my project, I have the following function:

int addAccount()
{
   int ID_number = 0;
   int ID;
   int ID_array[10];
   double account_balance;
   double balance_array[10];
   int choice = getChoice ();
   if (choice == 1)
   {
      if (ID_number + 1 > 10)
      {
         printf("Error\n");
      }
      else
      {
         ID_number = ID_number + 1;
         printf("Please enter the id\n");
         scanf("%d", &ID); 
         ID_array[ID_number - 1] = ID;
         printf("Please enter the starting balance\n");
         scanf("%lf", &account_balance);
         balance_array[ID_number - 1] = account_balance;
      }

   }
   return;

I need to somehow get the values for several of these variables and use them in another function (particularly ID, ID_number, and account_balance). The function that I need these values for is as follows:

void displayAccounts ()
{
   int count;
   int choice = getChoice ();
   int ID_number = ID_number ();

   if (choice == 2)
   {
      for (count = 0; count < ID_number; count++)
      {
         printf("Account #%d: ID is %d\n", count + 1, ID_array[count]);
         printf("Account balance is %.2lf\n", balance_array[count]);
      }

   }
}

I know how to return one value, but I don't know how to make multiple values usable outside of the function where they occur. Is what I'm trying to do even possible or is it likely that I'm going about my project the wrong way?

Titanguy654
  • 33
  • 1
  • 5

3 Answers3

2

The most obvious choice is to create a struct, which can have any number of fields (including arrays), and which can be both passed to and returned from a function. In that way you can pass around the necessary state.

You can of course also optimize it a bit by having a root function define the struct variable, and then just pass around pointers to the same instance.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

Use a struct:

struct Account
{
  int count;
  int ID_number;
  int balance_array[10];

  // etc etc
};

Pass a pointer to addAccount:

void addAccount(struct Account *account)
{
  account->ID_number = 0;
  // etc
}

And then pass it to displayAccounts

void displayAccount(struct Account *account)
{

}

Eg:

struct Account account;
addAccount(&account);
displayAccount(&account);

Note that in C you need to use the struct prefix unless you typedef the struct.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • When I try implementing this I get the error "error: unknown type name 'Account'" in the line "void addAccount(Account *account)" – Titanguy654 Oct 17 '14 at 12:43
  • Sorry, my mistake, I left off the `struct` keyword in a few places. – Sean Oct 17 '14 at 13:27
0

You can use a structure to send the values. Also the pointers can be a good alternate to return multiple values in C.

You can Refer to The following links too:

http://www.stackoverflow.com/questions/2620146/how-do-i-return-multiple-values-from-a-function-in-c

http://www.c4learn.com/c-programs/return-multiple-values-from-function.html

OshoParth
  • 1,492
  • 2
  • 20
  • 44