Thread: Passing variables to functions (was: Problem with functions)

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    Problems passing global single dimension arrays into functions.

    Hi everyone. First off, thanks for your help on my array problem last time. However, I now have a problem with my functions, in particular, passing my global arrays into functions. No matter what I do, I just can't get them to talk to each other. Thanks for your suggestions, Scarlet7. It helped clean up some of my code, but the function problem remains. I tried looking over tutorials and in my C book, but none of the solutions seem to work.

    Code:
    #define ZERO 0.0
    #define COMMISSION_RATE 0.144
    #define BONUS_COMM_RATE 0.188
    #define TRUE 1
    #define FALSE 0
    #define HIGH_SALESPERSON 7
    #define ARRAY_SIZ 75
    #include <stdio.h>
    int s_code[ARRAY_SIZ];
    int c_code[ARRAY_SIZ];
    double sale_amount[ARRAY_SIZ];
    double sale_comm[ARRAY_SIZ];
    int flag[ARRAY_SIZ];
    void input_fun(int s_code[ARRAY_SIZ], int c_code[ARRAY_SIZ], double sale_amount[ARRAY_SIZ], int i);
    void output_fun(int o, int number, int branch[]);
    int sales_code();
    int cust_code();
    double sale();
    void comm_report (double comm, int bonus);
    void sale_error();
    
    main()
    {  int num_sales = 0;
       double total_sales, total_comm;
       int n = 0;
          input_fun(s_code, c_code, sale_amount, n);
          for (n = 0; n < num_sales; n++)
          {
             if (sale_amount[n] > 1450)
             {
                flag[n] = TRUE;
               sale_comm[n] = (208.8 + ((sale_amount[n] - 1450) * BONUS_COMM_RATE));
             }
             else
             {
                flag[n] = FALSE;
                sale_comm[n] = (sale_amount[n] * COMMISSION_RATE);
             }
             total_sales += sale_amount[n];
             total_comm += sale_comm[n];
          }
             output_fun(n, num_sales, flag);
    }
    
    
    void
    input_fun(int s_code[ARRAY_SIZ], int c_code[ARRAY_SIZ], double sale_amount[ARRAY_SIZ], int i)
    {
       int count;
       s_code[i] = sales_code();
       while (s_code[i] != ZERO)
       {
          if (s_code[i] <= HIGH_SALESPERSON && s_code[i] > ZERO)
          {
             c_code[i] = cust_code();
             sale_amount[i] = sale();
             count += 1;
             i++;
             s_code[i] = sales_code();
          }
          else
          {
             sale_error();
             s_code[i] = sales_code();
          }
       }
       return;
    }
    
    void
    output_fun(int o, int number, int branch[])
    {
       printf("\n\n        SALES REPORT");
       printf("\n\nSP CUST      SALE         COMM");
       for (o = 0; o < number; o++)
       {
          printf("\n%d %3d  $%9.2lf", &s_code[o], &c_code[o], &sale_amount[o]);
          if (branch[o] == TRUE)
          {
             printf("**");
          }
          else
          {
             printf("  ");
          }
          printf("  $%9.2lf", &sale_comm[o]);
       }
       return;
    }
    
    
    
    int
    sales_code()
    {
       int sval;
       printf("\n\nSalesperson code (1-7)(Enter 0 to quit): ");
       scanf("%d", &sval);
          return sval;
    }
    
    int
    cust_code()
    {
       int cval;
       printf("Customer code: ");
       scanf("%d", &cval);
          return cval;
    }
    
    double
    sale()
    {
       double amount;
       printf("Sale amount in dollars and cents: ");
       scanf("%lf", &amount);
          return amount;
    }
    
    
    void
    sale_error()
    {
       printf("Salesperson code must be 1 to 7");
          return;
    }
    When it prints, it prints out the first two lines, as it is a direct print command. The problem is that my statistics don't show up. I'm thinking that it's in my input function. Either way, I'm pulling my hair out over this.

    I know the output function prototype is constructed much differently from the input function, but neither one seems to work and I'm at my wits end as to which one is closer to correct.

    Thanks again in advance. I hope I don't come off as one looking for handouts, it's just that I'm very frustrated right now after pounding away at the code for so long.
    Last edited by OmniMirror; 05-06-2003 at 12:05 PM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    > for (n =0; n < num_sales; n++)

    add {} in the for loop, otherwise the following lines will be outside the loop:

    > total_sales += sale_amount[n];
    > total_comm += sale_comm[n];
    > s_code[n] = sales_code();
    > output_fun(n, num_sales, flag);
    Code:
          for (n =0; n < num_sales; n++)
          {
             if (sale_amount[n] > 1450)
             {
                flag[n] = TRUE;
                sale_comm[n] = (208.8 + ((sale_amount[n] - 1450) * BONUS_COMM_RATE));
             }
             else
             {
                flag[n] = FALSE;
                sale_comm[n] = (sale_amount[n] * COMMISSION_RATE);
             }
             total_sales += sale_amount[n];
             total_comm += sale_comm[n];
             s_code[n] = sales_code();
             output_fun(n, num_sales, flag);
          }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Thanks for the help. It loops through without any problems now, but I still have the stats problem. I put the output function outside the loop as it can only be called once (It has its own loop for printing, and yes I made sure it had brackets.).

    Code:
    #define ZERO 0.0
    #define COMMISSION_RATE 0.144
    #define BONUS_COMM_RATE 0.188
    #define TRUE 1
    #define FALSE 0
    #define HIGH_SALESPERSON 7
    #define ARRAY_SIZ 75
    #include <stdio.h>
    int s_code[ARRAY_SIZ];
    int c_code[ARRAY_SIZ];
    double sale_amount[ARRAY_SIZ];
    double sale_comm[ARRAY_SIZ];
    int flag[ARRAY_SIZ];
    void input_fun(int i, int count);
    void output_fun(int o, int number, int branch[o]);
    int sales_code();
    int cust_code();
    double sale();
    void comm_report (double comm, int bonus);
    void sale_error();
    
    main()
    {  int num_sales, n;
       double total_sales, total_comm;
          input_fun(n, num_sales);
          for (n = 0; n < num_sales; n++)
          {
             if (sale_amount[n] > 1450)
             {
                flag[n] = TRUE;
               sale_comm[n] = (208.8 + ((sale_amount[n] - 1450) * BONUS_COMM_RATE));
             }
             else
             {
                flag[n] = FALSE;
                sale_comm[n] = (sale_amount[n] * COMMISSION_RATE);
             }
             total_sales += sale_amount[n];
             total_comm += sale_comm[n];
          }
             output_fun(n, num_sales, flag);
    
    }
    
    
    void
    input_fun(int i, int count)
    {
       s_code[i] = sales_code();
       while (s_code[i] != ZERO)
       {
          if (s_code[i] <= HIGH_SALESPERSON && s_code[i] > ZERO)
          {
             c_code[i] = cust_code();
             sale_amount[i] = sale();
             count += 1;
             i++;
             s_code[i] = sales_code();
          }
          else
          {
             sale_error();
             s_code[i] = sales_code();
          }
       }
       return;
    }
    
    void
    output_fun(int o, int number, int branch[o])
    {
       printf("\n\n        SALES REPORT");
       printf("\n\nSP CUST      SALE         COMM");
       for (o = 0; o < number; o++)
       {
          printf("\n%d %3d  $%9.2lf", &s_code[o], &c_code[o], &sale_amount[o]);
          if (branch[o] == TRUE)
          {
             printf("**");
          }
          else
          {
             printf("  ");
          }
          printf("  $%9.2lf", &sale_comm[o]);
       }
       return;
    }
    
    
    
    int
    sales_code()
    {
       int sval;
       printf("\n\nSalesperson code (1-7)(Enter 0 to quit): ");
       scanf("%d", &sval);
          return sval;
    }
    
    int
    cust_code()
    {
       int cval;
       printf("Customer code: ");
       scanf("%d", &cval);
          return cval;
    }
    
    double
    sale()
    {
       double amount;
       printf("Sale amount in dollars and cents: ");
       scanf("%lf", &amount);
          return amount;
    }
    
    
    void
    sale_error()
    {
       printf("Salesperson code must be 1 to 7");
          return;
    }
    Through tracking this code, I have defined my variables and accessed my input function. In the input function, I call the s_code function. After testing the errors and quit at 0 (both correct), I start adding in data.

    My int count and int i are supposed to increment, which should move me to the next array element. I then repeat until I enter 0.

    Upon doing this, the program <i>should</i> bring the int i and int count values back to int n and int num_sales back in main().

    *UPDATE*
    I've basically isolated the problem to transferring my values back to main. If they transferred correctly, the conditions for making at least one run through the loop would be met. I know this because I had an extra s_code function call which would have made a logic error had it run through the loop.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    > input_fun(n, num_sales);

    The first time input_fun() is called n is not initialized and will contain a random value. input_fun() is using this value to index into s_code[i], this could cause a problem.

    >s_code[i] = sales_code();

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Sorry if the thread looks a bit confusing. I did a pretty big edit of my first post just now. I wish I could change the topic title, as my problem is more visible and less vague.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I wish I could change the topic title
    I changed it. PM me if you disagree...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Woohoo! Never mind, I figured it out. ^_^

    It took me forever, though. *phew!*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. Passing variables by reference
    By Tonto in forum C++ Programming
    Replies: 0
    Last Post: 07-16-2005, 12:56 AM
  4. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 06:12 PM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 09:49 PM