Thread: Using call by reference in this program

  1. #1
    Registered User
    Join Date
    Oct 2006

    Using call by reference in this program

    I am trying to write a program to fiqure out employee's tax percentages from an input file. I figured out how write most of it, but I'm not very good with call by reference functions, so Im really unsure on how to incorporate them into this function. The given functions we are supposed to use are these
    void readFile(int *EmpID, float *hourly, int *hours);
    float week2annual(float hourly, int hours);
    float findrate(float annual);
    I would appreciate some help if anyone has any to offer. Thanks very much.

    Comp 1200c
    Assignment 6
    #include <stdio.h>
    #define Weeks 52
    #define infiledata "assign6in.txt"
    #define outfiledata "assign6out.txt"
    void readFile(int *EmpID, float *hourly, int *hours);
    float week2annual(float hourly, int hours);
    float findrate(float annual);
    int main (void)
    /* Establish variables	*/
    FILE *infile
    FILE *outfile
    float salary, maximum, maxtax;
    int maxEmpID;
    /*	Get information from data file.	*/
    infile = fopen (infiledata, 'r');
    /*	Assure there is a data file.	*/
    if ( infile == NULL )
    printf("Cannot open data file. \n");
    /* Read data file.	*/
    readFile (*EmpID, *hourly, *hours);
    fscanf( infile, "%d %f %f",
    				 &EmpID, &hourly, &hours );
    /*	Make sentinel value stop the program	*/
    while	(EmpID != 0 )
    /* Do calculation	*/
    week2annual ( hourly, hours );
    salary = hourly * hours * Weeks;
    if ( salary > 319101.00 )
    	tax = 0.35;
    else if ( salary > 146750.00 )
    	tax = 0.33;
    else if ( salary > 70350.00 )
    	tax = 0.28;
    else if ( salary > 29050.00 )
    	tax = 0.25;
    else if ( salary > 7150.00 )
    	tax = 0.15;
    	tax = 0.10;
    /*	Figure out the maximum taxed employee	*/
    if ( salary > maximum )
    	maximum = salary;
    	maxEmpID = EmpID;
    	maxtax = tax;
    fprintf (outfile, "4d% 9.2% 4.2f%", EmpID, salary, tax );
    printf("Employee #04d - Salary: $%9.2f - Tax Rate: %4.2f%% n",
    			EmpID, salary, tax);
    fscanf( infile, "%d %f %f", &EmpID, &salary, &tax );
    printf("The top employee is: /n" )
    printf("Employee #04d - Salary: $%9.2f - Tax Rate: %4.2f%% n",
    			maxEmpID, maximum, maxtax);
    return 0;

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    My recommendations, with what you already have, would be to first learn to use indentation, then always use full bracing, then get rid of the syntax errors. After that, you might want to post the input file, or at least a smallish snippet thereof. Then you'd be closer to the point at which better help could be provided. Perhaps even take a stab at defining the functions.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Hi Matt,

    code like this if statement in your program, needs curly braces, otherwise only the FIRST line of code after the "if" will be considered part of the if statement, and the other lines will ALWAYS be executed. So that's not what you want:

    Instead of this:
    if ( salary > maximum )
    	maximum = salary;
    	maxEmpID = EmpID;
    	maxtax = tax;
    You want this:
    if ( salary > maximum )  
        maximum = salary;
        maxEmpID = EmpID;
        maxtax = tax;
    That's true for every muliti-line code expression which ends in a semi-colon, and every function in your program, including main() (you forgot the closing brace on main).

    When are you going to start showing us some other functions in your programs? Here, you have 1) Get input, 2) Calculations and 3) Output.

    I'm sure you could find better descriptive names for your functions. The point is, you NEED to start breaking your programs down into more than just ONE function. Working with functions is a BIG part of programming, and this isn't September any more.

    Even though your program works correctly, you will be marked down, and possibly even have your work rejected, if it's not using more than just main(). That's usually a requirement! If it's not a requirement in your class, you should absolutely learn it anyway.

    One function programs are unwanted and unacceptable beyond the simplest of programming lessons. Not by this board - by EVERYONE.


  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    You're missing lots of semicolons.

    fopen() takes a string for the mode parameter, not a character.

    You're mixing & up with *. & takes the address of a variable which can then be assigned to a pointer (or passed to a function that takes a pointer).

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. a program that will call an execute but with arguments
    By what3v3r in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2006, 09:44 PM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM