Can someone check my program?

This is a discussion on Can someone check my program? within the C Programming forums, part of the General Programming Boards category; The objective is to read from a data file and calculate the gross income, net income, and tax with the ...

  1. #1
    Unregistered
    Guest

    Question Can someone check my program?

    The objective is to read from a data file and calculate the gross income, net income, and tax with the maximum of 100 records. can someone please check my program for me, I dont have a complier right now so I dont know whats I need or dont need.

    thank you


    Sample File:

    Cindy Crawford:45:35:40:60
    Brad Pitt:25:35:15:25
    Al Pachino:40:40:0:48
    Ashley Judd:15:45:40:35
    .......

    PHP Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    #define LEN 31
    #define WEEKS 4
    #define TAX 0.2
    #define TAXOVER 0.28

    main(void){
        
    char name[MAX][LEN];
        
    int hours[MAX][WEEKS];
        
    float pay[MAX][3];
        
    int totName 0;
        
        
    /*function prototypes*/
        
    int getFile(char[MAX][LEN], int [MAX][WEEKS]);
        
    void calcGross(int[MAX][WEEKS], intfloat[MAX][3]);
        
    void calcNet(float[MAX][3], int);
        
    void calcTax(float[MAX][3], int);

        
    /*calling functions*/
        
    totName getFile(name[][LEN],hours[][WEEKS]);
        
    calcGross(hours[MAX][WEEKS], totNamepay[MAX][3]);
        
    calcTax(pay[MAX][3], totName);
        
    calcNet(pay[MAX][3], totName);



    return;

    }
        

    int getFile(char name[MAX][LEN], int hours[MAX][WEEKS])
    {
        
    FILE *fempHrs;
        
    int i=0;
        
        
    fempHrs fopen("lab1.txt""r");    /*open file and 
                                            check if there*/
        
    if(fempHrs == NULL)
        { 
            
    puts("ERROR: No Such File");
            exit(
    0);
        }

        for(
    i=0;i<MAX;i++)                /* for loop to scan for names*/
        
    {
            
    fscanf(fempHrs," %[^:]"name[i]);
                if (
    name[i] == EOF)        /*check if end of file*/
                    
    break;
                for(
    j=0;j;<WEEKS;j++)    /*for loop to scan hrs worked*/
                
    {
                    
    fscanf(fempHrs,"%*c%d"hours[i][j]);
                }                        
    /*end for loop j*/
        
    }                                /*end for loop i*/
        
    fclose(fempHrs);
        return 
    i;
    }                                    
    /*end getFile function*/

    void calcGross(int hours[MAX][WEEKS], int totNamefloat pay[MAX][3])
    {
        
    int i;
        
    float gross 0reghrsover;

        for(
    i=0i<totNamei++)
        {
            for(
    j=0j<WEEKSj++)
            {
                if(
    hours[i][j] <= 40)
                     
    gross hours[i][j]*17;
                else 
                    if(
    hours[i][j] > 40)
                    {
                        
    hrs hours[i][j] - 40;
                        
    over hrs 25.5;
                        
    reg 40 17;
                        
    gross over reg;
                    }                        
    /*end if*/
                    
    pay[i][0]+=gross;
            }                                
    /*end for loop j*/
        
    }                                    /*end for loop i*/
        
    return;
    }                                        
    /*end calcGross function*/

    void calcTax(float pay[MAX][3], int totName)
    {                                        
    /*function to calc Tax*/
        
    int i;

        for(
    i=0i<totNamei++)            /*loop for the # of records*/
        
    {
            if(
    pay[i][0] < 1800)        
                
    pay[i][1] = pay[i][0] * TAX;
            else
                if(
    pay[i][0] >= 1800)
                {
                    
    pay[i][1] = pay[i][0] * TAXOVER;
                }
        }
                return;
    }                                        
    /*end calcTax Function*/

    void calcNet(float pay[MAX][3], int totName)            
    {                                        
    /*function to calc netincome*/
        
    int i;
        
        for(
    i=0i<totNamei++)
        {
            
    pay[i][2] = pay[i][0] - pay[i][1];
        }

        return;
    }                                        
    /*end calcNet Function*/ 

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Go to www.borland.com and get the FREE compiler and debugger.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just doing a quick scan I found this:

    for(j=0;j;<WEEKS;j++) should be (j=0;j<WEEKS;j++)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    I have just gotten to a hold on a MSVisual 6.0 complier and i dont understand why im getting the following error message:
    Code:
    H:\Lab1\lab1.c(25) : error C2059: syntax error : ']'
    H:\Lab1\lab1.c(26) : error C2059: syntax error : ']'
    H:\Lab1\lab1.c(27) : error C2059: syntax error : ']'
    H:\Lab1\lab1.c(28) : error C2059: syntax error : ']'
    H:\Lab1\lab1.c(54) : warning C4047: '==' : 'char [31]' differs in 
    levels of indirection from 'const int '
    the following is the revised code:
    Code:
    ##include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 100
    #define LEN 31
    #define WEEKS 4
    #define TAX 0.2
    #define TAXOVER 0.28
    
    /*function prototypes*/
    int getFile(char[MAX][LEN], int[MAX][WEEKS]);
    void calcGross(int[MAX][WEEKS], int, float[MAX][3]);
    void calcNet(float[MAX][3], int);
    void calcTax(float[MAX][3], int);
    void output(char[MAX][LEN], float[MAX][3], int);
    
    
    int main(void){
    	char name[MAX][LEN];
    	int hours[MAX][WEEKS];
    	float pay[MAX][3];
    	int totName;
    
    
    /*calling functions*/
    	totName = getFile(&name[MAX][LEN], &hours[MAX][WEEKS]);
    	printf("%d", totName);
    	calcGross(&hours[MAX][WEEKS], totName, &pay[MAX][3]);
    	calcTax(&pay[MAX][3], totName);
    	calcNet(&pay[MAX][3], totName);
    	output(&name[MAX][LEN], &pay[MAX][3], totName);
    	return 0;
    
    }
    
    
    int getFile(char name[MAX][LEN], int hours[MAX][WEEKS])
    {
    	FILE *fempHrs;
    	int i;
    	int j;
    	char filename[20];
    
    	puts("Enter the file name. Ex: xxxx.txt");
    	gets(filename);
    	fempHrs = fopen(filename, "r");			/*open file and
    	check if there*/
    	if(fempHrs == NULL)
    	{
    		puts("ERROR: No Such File");
    		exit(0);
    	}
    
    	for(i=0;i<MAX;i++)
    	{
    		if ( fscanf(fempHrs," %[^:]", name[i]) == EOF )
    			break;
    		for(j=0;j<WEEKS;j++)
    		{
    			fscanf(fempHrs,"%*c%d", hours[i][j]);
    		}							/*end for loop j*/
    		printf("%d", i);
    	}								/*end for loop i*/
    	fclose(fempHrs);
    
    
    
    	return i;
    } /*end getFile function*/
    
    void calcGross(int hours[MAX][WEEKS], int totName, float pay[MAX][3])
    {
    	int i;
    	int j;
    	float gross=0, reg, hrs, over;
    
    	for(i=0; i<totName; i++)
    	{
    		for(j=0; j<WEEKS; j++)
    		{
    			if(hours[i][j] <= 40)
    				gross = (float)hours[i][j] * 17;
    			else
    				if(hours[i][j] > 40)
    				{
    					hrs = (float)hours[i][j] - 40;
    					over = hrs * 25.5;
    					reg = 40 * 17;
    					gross = over + reg;
    				} /*end if*/
    			pay[i][0]+=gross;
    		} /*end for loop j*/
    	 } /*end for loop i*/
    	 return;
    } /*end calcGross function*/
    
    void calcTax(float pay[MAX][3], int totName)
    { /*function to calc Tax*/
    	int i;
    
    	for(i=0; i<totName; i++)
    	{
    		if(pay[i][0] < 1800)
    			pay[i][1] = pay[i][0] * TAX;
    		else
    			if(pay[i][0] >= 1800)
    			{
    				pay[i][1] = pay[i][0] * TAXOVER;
    			}
    	}
    	return;
    } /*end calcTax Function*/
    
    void calcNet(float pay[MAX][3], int totName)
    { /*function to calc netincome*/
    	int i;
    
    	for(i=0; i<totName; i++)
    	{
    		pay[i][2] = pay[i][0] - pay[i][1];
    	}
    
    	return;
    } /*end calcNet Function*/
    
    void output(char name[MAX][LEN], float pay[MAX][3], int totName)
    {
    	int i=0;
    	
    	puts("*********************************************");
    	puts("*              Monthly Pay Record           *");
    	puts("*********************************************");
    	for(i=0;i<totName;i++)
    	{
    		printf("\t%.2f \t%.2f \t%.2f", &pay[i][0], &pay[i][1], &pay[i][2]);
    	}
    	return;
    }
    thank you for any help
    ps. i couldn't edit my first message as a unregistered user sorry.
    Last edited by lucy; 10-04-2001 at 09:41 PM.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi just a quick look at your program, good coding practice to define your function prototypes outside main...As so

    Code:
    #define WEEKS 4
    #define TAX 0.2
    #define TAXOVER 0.28
    
    /*function prototypes*/
        int getFile(char[MAX][LEN], int[MAX][WEEKS]);
        void calcGross(int[MAX][WEEKS], int, float[MAX][3]);
        void calcNet(float[MAX][3], int);
        void calcTax(float[MAX][3], int);
    
    int main(void){
        char name[MAX][LEN];
        int hours[MAX][WEEKS];
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    When wanting to prompt the user for the file name what should i do to have the filename inside the fopen()
    here's what i've go and im not sure if it's correct:
    Code:
    	
                   char filename[20];
    
    	puts("Enter the file name. Ex: xxxx.txt");
    	gets(filename);
    	fempHrs = fopen(filename, "r");
    also what do i need to do to print a string from the array, i know how to do so with a 1-D array but i dont know how i should print the string with a 2-D array?

    char name[20];
    printf ( " %s\n", name) ;
    Last edited by lucy; 10-04-2001 at 09:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2009, 09:45 PM
  2. Suggestions for my Check Book program
    By Nor in forum C++ Programming
    Replies: 2
    Last Post: 11-17-2008, 06:44 PM
  3. program to check hard disk transfer rate
    By shadow99er in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 05:04 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21