#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Values are not being passed down


    I'm in an introductory course for programming in C, and I got stuck in my assignment. There are no error messages from compiling, but there is obviously something wrong, because the output values are consistently wrong, often being zero, close to zero, or negative. Please help, I've been at this forever.

    #include <stdio.h>
    //IPC144I - Assignment 2
    //Dingas

    double grossPaySalaried(double annualSalary){
    return(annualSalary/52);
    }
    double grossPayHourly(double rate, double hours){
    double tmpHGross=hours*rate;
    if(hours>44){
    tmpHGross+=(hours-44)/2;
    }
    return(tmpHGross);
    }
    double longTermBenefits(double gross){
    int LTB;
    if(gross>=100){
    LTB=(gross/100)*2;
    }
    else{
    LTB=0;
    }
    return(LTB);
    }
    double employmentInsurance(double gross){
    double tmpEI=gross*0.014;
    if(tmpEI>11.80){
    tmpEI=11.80;
    }
    return(tmpEI);
    }
    double canadaPensionPlan(double gross){
    double tmpCCP;
    if(gross>700){
    tmpCCP=1120;
    }
    else{
    tmpCCP=gross*1.6;
    }
    return(tmpCCP);
    }
    void taxes(double gross, double *fTaxPointer, double *pTaxPointer){
    double yearGross=gross*52;
    double fTaxTemp, pTaxTemp;
    if(yearGross<=20000){
    fTaxTemp=yearGross*0.16;
    }
    else if(yearGross>20000){
    fTaxTemp+=20000*0.16;
    }
    if(yearGross>40000){
    fTaxTemp+=20000*0.23;
    fTaxTemp+=(yearGross-40000)*0.29;
    }
    pTaxTemp=fTaxTemp*0.47;
    *fTaxPointer=fTaxTemp;
    *pTaxPointer=pTaxTemp;
    }
    void outputSalaried(int id, double salary, double gross, double ltb, double ei, double cpp, double pTax, double fTax, int year, int month, int day){
    printf("Dingas Payroll Systems");
    printf("\n102 Dingas Road, Dingview ON");
    printf("\n\nEmployee Number: %d For Week Ending: %d%d%d", id,day,month,year);
    printf("\n\nSalary Paid :%8.2f", gross*52);
    printf("\n\nGROSS PAY: %8.2f", gross);
    printf("\n\nDEDUCTIONS:");
    printf("\nLong Term Benefits: %8.2f", ltb);
    printf("\nEmployment Insurance: %8.2f", ei);
    printf("\nCanada Pension Plan: %8.2f", cpp);
    printf("\nProvincial Tax: %8.2f", pTax);
    printf("\nFederal Tax: %8.2f", fTax);
    double totalDeductions=ltb+ei+cpp+pTax+fTax;
    double netPay=gross-totalDeductions;
    printf("\n\nTOTAL DEDUCTIONS: %8.2f", totalDeductions);
    printf("\n\nNET PAY: %8.2f", netPay);
    }
    void outputHourly(int id, double rate, double hours, double gross, double ltb, double ei, double cpp, double pTax,double fTax,int year,int month,int day){
    printf("Dingas Payroll Systems");
    printf("\n102 Dingas Road, Dingview ON");
    printf("\n\nEmployee Number: %d For Week Ending: %d%d%d", id,day,month,year);
    printf("\n\nHourly Rate: %8.2f Hours Worked: %8.2f", rate,hours);
    printf("\n\nGROSS PAY: %8.2f", gross);
    printf("\n\nDEDUCTIONS:");
    printf("\nLong Term Benefits: %8.2f", ltb);
    printf("\nEmployment Insurance: %8.2f", ei);
    printf("\nCanada Pension Plan: %8.2f", cpp);
    printf("\nProvincial Tax: %8.2f", pTax);
    printf("\nFederal Tax: %8.2f", fTax);
    double totalDeductions=ltb+ei+cpp+pTax+fTax;
    double netPay=gross-totalDeductions;
    printf("\n\nTOTAL DEDUCTIONS: %8.2f", totalDeductions);
    printf("\n\nNET PAY: %8.2f", netPay);
    }

    main(){
    double fTaxPointer;
    double pTaxPointer;
    double salary, annualSalary, rate, hours, gross, ltb, ei, cpp, pTax, fTax;
    int id, year, month, day;
    char category, input;

    printf("\nPlease enter your 3 digit employee number: ");
    scanf("%d", &id);
    printf("\nHourly, or salaried? (h/s): ");
    scanf(" %c", &category);

    if(category=='h'){
    printf("\nHourly rate?: ");
    scanf("%f", &rate);
    printf("\nNumber of hours worked?: ");
    scanf("%f", &hours);
    gross=grossPayHourly(rate,hours);
    }
    else if(category=='s'){
    printf("\nEnter your annual salary: ");
    scanf("%f", annualSalary);
    gross=grossPaySalaried(annualSalary);
    }

    printf("\nEnter the year(YYYY): ");
    scanf("%d", &year);
    printf("\nEnter the month(MM): ");
    scanf("%d", &month);
    printf("\nEnter the day(DD): ");
    scanf("%d", &day);
    printf("hi");

    ltb=longTermBenefits(gross);
    ei=employmentInsurance(gross);
    cpp=canadaPensionPlan(gross);
    taxes(gross, &fTaxPointer, &pTaxPointer);
    fTax=fTaxPointer;
    pTax=pTaxPointer;

    printf("\n\n\n");

    if(category=='h'){
    outputHourly(id, rate, hours, gross, ltb, ei, cpp, pTax,fTax,year,month,day);
    }
    else if(category=='s'){
    outputSalaried(id, salary, gross, ltb, ei, cpp, pTax, fTax,year,month,day);
    }

    printf("\n\n\n");

    }





    -------------------OUTPUT------------------------

    Please enter your 3 digit employee number: 123

    Hourly, or salaried? (h/s): h

    Hourly rate?: 12

    Number of hours worked?: 12

    Enter the year(YYYY): 1234

    Enter the month(MM): 12

    Enter the day(DD): 12
    hi


    Derek Eliopoulos Payroll Systems
    102 Dingas Road, Dingview ON

    Employee Number: 123 For Week Ending: 12

    Hourly Rate: -0.06 Hours Worked: 0.00

    GROSS PAY: -0.00

    DEDUCTIONS:
    Long Term Benefits: 0.00
    Employment Insurance: -0.00
    Canada Pension Plan: -0.00
    Provincial Tax: -0.00
    Federal Tax: -0.00

    TOTAL DEDUCTIONS: -0.00

    NET PAY: 0.00
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222
    Unreadable mess, so why should anybody bother to read it? That K&R format, which hides open braces on the end and in the middle of lines, makes matters even worse.

    Yes, you had formatted your code by indenting it, but HTML strips all that out. You need to use code tags to preserve that indenting. Here's what your code looks like with code tags -- original formatting recovered via the Reply button:
    Code:
    #include <stdio.h>
    
    	double grossPaySalaried(double annualSalary){
    		return(annualSalary/52);
    	}
    	double grossPayHourly(double rate, double hours){
    		double tmpHGross=hours*rate;
    		if(hours>44){
    			tmpHGross+=(hours-44)/2;
    		}
    		return(tmpHGross);
    	}
    	double longTermBenefits(double gross){
    		int LTB;
    		if(gross>=100){
    			LTB=(gross/100)*2;
    		}
    		else{
    			LTB=0;
    		}
    		return(LTB);
    	}
    	double employmentInsurance(double gross){
    		double tmpEI=gross*0.014;
    		if(tmpEI>11.80){
    			tmpEI=11.80;
    		}
    		return(tmpEI);
    	}
    	double canadaPensionPlan(double gross){
    		double tmpCCP;
    		if(gross>700){
    			tmpCCP=1120;
    		}
    		else{
    			tmpCCP=gross*1.6;
    		}
    		return(tmpCCP);
    	}
    	void taxes(double gross, double *fTaxPointer, double *pTaxPointer){
    		double yearGross=gross*52;
    		double fTaxTemp, pTaxTemp;
    		if(yearGross<=20000){
    			fTaxTemp=yearGross*0.16;
    		}
    		else if(yearGross>20000){
    			fTaxTemp+=20000*0.16;
    		}
    		if(yearGross>40000){
    			fTaxTemp+=20000*0.23;
    			fTaxTemp+=(yearGross-40000)*0.29;
    		}
    		pTaxTemp=fTaxTemp*0.47;
    		*fTaxPointer=fTaxTemp;
    		*pTaxPointer=pTaxTemp;
    	}
    	void outputSalaried(int id, double salary, double gross, double ltb, double ei, double cpp, double pTax, double fTax, int year, int month, int day){
    		printf("Dingus Payroll Systems");
    		printf("\n102 Dingas Road, Dingview ON");
    		printf("\n\nEmployee Number: %d         For Week Ending: %d%d%d", id,year,month,day);
    		printf("\n\nSalary Paid :%8.2lf", gross*52);
    		printf("\n\nGROSS PAY:                    %8.2lf", gross);
    		printf("\n\nDEDUCTIONS:");
    		printf("\nLong Term Benefits:	%8.2lf", ltb);
    		printf("\nEmployment Insurance:	%8.2lf", ei);
    		printf("\nCanada Pension Plan:	%8.2lf", cpp);
    		printf("\nProvincial Tax:		%8.2lf", pTax);
    		printf("\nFederal Tax:			%8.2lf", fTax);
    		double totalDeductions=ltb+ei+cpp+pTax+fTax;
    		double netPay=gross-totalDeductions;
    		printf("\n\nTOTAL DEDUCTIONS:	%8.2lf", totalDeductions);
    		printf("\n\nNET PAY:			%8.2lf", netPay);
    	}
    	void outputHourly(int id, double rate, double hours, double gross, double ltb, double ei, double cpp, double pTax,double fTax,int year,int month,int day){
    		printf("Dingus Payroll Systems");
    		printf("\n102 Dingas Road, Dingview ON");
    		printf("\n\nEmployee Number: %d         For Week Ending: %d%d%d", id,year,month,day);
    		printf("\n\nHourly Rate:	%lf		Hours Worked:	%lf", rate,hours);
    		printf("\n\nGROSS PAY:                    %lf", gross);
    		printf("\n\nDEDUCTIONS:");
    		printf("\nLong Term Benefits:	%8.2lf", ltb);
    		printf("\nEmployment Insurance:	%8.2lf", ei);
    		printf("\nCanada Pension Plan:	%8.2lf", cpp);
    		printf("\nProvincial Tax:		%8.2lf", pTax);
    		printf("\nFederal Tax:			%8.2lf", fTax);
    		double totalDeductions=ltb+ei+cpp+pTax+fTax;
    		double netPay=gross-totalDeductions;
    		printf("\n\nTOTAL DEDUCTIONS:	%8.2lf", totalDeductions);
    		printf("\n\nNET PAY:			%8.2lf", netPay);
    	}
    	
    main(){
    	double fTaxPointer, pTaxPointer, salary, annualSalary, rate, hours, gross, ltb, ei, cpp, pTax, fTax;
    	int  id, year, month, day;
    	char category, input;
    	
    	printf("\nPlease enter your 3 digit employee number: ");
    	scanf("%d", &id);
    	printf("\nHourly, or salaried? (h/s): ");
    	scanf(" %c", &category);
    	
    	if(category=='h'){
    		printf("\nHourly rate?: ");
    		scanf("%f", &rate);
    		printf("\nNumber of hours worked?: ");
    		scanf("%f", &hours);
    		gross=grossPayHourly(rate,hours);
    	}
    	else if(category=='s'){
    		printf("\nEnter your annual salary: ");
    		scanf("%f", annualSalary);
    		gross=grossPaySalaried(annualSalary);
    	}
    	
    	printf("\nEnter the year(YYYY): ");
    	scanf("%d", &year);
    	printf("\nEnter the month(MM): ");
    	scanf("%d", &month);
    	printf("\nEnter the day(DD): ");
    	scanf("%d", &day);
    	
    	ltb=longTermBenefits(gross);
    	ei=employmentInsurance(gross);
    	cpp=canadaPensionPlan(gross);
    	taxes(gross, &fTaxPointer, &pTaxPointer);
    	fTax=fTaxPointer;
    	pTax=pTaxPointer;
    	
    	printf("taxes: %lf %lf",fTax,pTax);
    	printf("\n\n\n");
    	
    	if(category=='h'){
    		outputHourly(id, rate, hours, gross, ltb, ei, cpp, pTax,fTax,year,month,day);
    	}
    	else if(category=='s'){
    		outputSalaried(id, salary, gross, ltb, ei, cpp, pTax, fTax,year,month,day);
    	}
    	
    	printf("\n\n\n");
    	
    }
    Makes a big difference, don't you think? From here on, use code tags.

    Originally Posted by dingleton
    There are no error messages from compiling,
    Not quite. Here is what I get when I try to compile your program:
    C:\otros\dcw>gcc -Wall dingle1.c
    dingle1.c: In function `outputSalaried':
    dingle1.c:61: warning: use of `l' length character with `f' type character
    dingle1.c:62: warning: use of `l' length character with `f' type character
    dingle1.c:64: warning: use of `l' length character with `f' type character
    dingle1.c:65: warning: use of `l' length character with `f' type character
    dingle1.c:66: warning: use of `l' length character with `f' type character
    dingle1.c:67: warning: use of `l' length character with `f' type character
    dingle1.c:68: warning: use of `l' length character with `f' type character
    dingle1.c:69: parse error before `double'
    dingle1.c:71: `totalDeductions' undeclared (first use in this function)
    dingle1.c:71: (Each undeclared identifier is reported only once
    dingle1.c:71: for each function it appears in.)
    dingle1.c:71: warning: use of `l' length character with `f' type character
    dingle1.c:72: `netPay' undeclared (first use in this function)
    dingle1.c:72: warning: use of `l' length character with `f' type character
    dingle1.c: In function `outputHourly':
    dingle1.c:78: warning: use of `l' length character with `f' type character
    dingle1.c:78: warning: use of `l' length character with `f' type character
    dingle1.c:79: warning: use of `l' length character with `f' type character
    dingle1.c:81: warning: use of `l' length character with `f' type character
    dingle1.c:82: warning: use of `l' length character with `f' type character
    dingle1.c:83: warning: use of `l' length character with `f' type character
    dingle1.c:84: warning: use of `l' length character with `f' type character
    dingle1.c:85: warning: use of `l' length character with `f' type character
    dingle1.c:86: parse error before `double'
    dingle1.c:88: `totalDeductions' undeclared (first use in this function)
    dingle1.c:88: warning: use of `l' length character with `f' type character
    dingle1.c:89: `netPay' undeclared (first use in this function)
    dingle1.c:89: warning: use of `l' length character with `f' type character
    dingle1.c: At top level:
    dingle1.c:92: warning: return-type defaults to `int'
    dingle1.c: In function `main':
    dingle1.c:104: warning: float format, double arg (arg 2)
    dingle1.c:106: warning: float format, double arg (arg 2)
    dingle1.c:111: warning: format argument is not a pointer (arg 2)
    dingle1.c:129: warning: use of `l' length character with `f' type character
    dingle1.c:129: warning: use of `l' length character with `f' type character
    dingle1.c:95: warning: unused variable `input'
    dingle1.c:141: warning: control reaches end of non-void function

    C:\otros\dcw>
    Why did you ignore the warnings that your compiler threw at you? Ignoring warnings is a very stupid thing for a programmer to do. Warnings mean that there is something wrong with your code. Never ignore warnings! Warnings are much more important than error messages! Always turn warnings on and up! And never ignore warnings!

    Some of those warnings and errors I got appear to be because you did the second more stupid thing: you are giving us C99 code which many of us cannot compile. Either clean up your code or try to figure out for yourself which messages are caused by C99.

    That last one is because you were very sloppy and lazy. You implicitly declared main to return int and then you neglected to return an int.

    The warnings of "use of `l' length character with `f' type character" are because in printf you do not use "%lf", but rather "%f".

    However, you do need "%lf" with scanf when you scan in a double; "%f" is for float. Didn't you read the man page for scanf?:
    Originally Posted by man page scanf
    l

    Indicates either that the conversion will be one of d, i, o, u, x, X, or n and the next pointer is a pointer to a long int or unsigned long int (rather than int), or that the conversion will be one of e, f, or g and the next pointer is a pointer to double (rather than float). Specifying two l characters is equivalent to L. If used with %c or %s the corresponding parameter is considered as a pointer to a wide character or wide-character string respectively.
    That might be what's messing up the values that you are inputting, which in turn would affect output values.

    And actually, if you were to have deigned to read the warnings, you would have read these:
    Originally Posted by compiler output
    dingle1.c:104: warning: float format, double arg (arg 2)
    dingle1.c:106: warning: float format, double arg (arg 2)
    dingle1.c:111: warning: format argument is not a pointer (arg 2)
    Those warnings apply to this section of code; indicated lines are in red:
    Code:
    		scanf("%f", &rate);
    		printf("\nNumber of hours worked?: ");
    		scanf("%f", &hours);
    		gross=grossPayHourly(rate,hours);
    	}
    	else if(category=='s'){
    		printf("\nEnter your annual salary: ");
    		scanf("%f", annualSalary);
    I already covered the problem with the first two scanf statements, which is that you are telling scanf to store a float value into a double variable. A float is half the size of a double, so that means that half of your doubles are filled with garbage. And if I'm right, it's the important half that is garbage, so you must be getting some really bizarrely huge values there.

    Now think about this: the man page told you how to handle doubles with scanf and the warnings told you that those scanfs had a problem. So why couldn't you see that for yourself?

    That third scanf's problem is that scanf expects a pointer and you just gave it a double. Again, both the man page and the warning had told you what to do and what you did wrong. It's not rocket science. AOBTW, when you correct that warning you will then get that same warning about storing a float in a double.

    Never ignore warnings. This should have served as an object lesson in the great importance of warnings. So never ignore warnings!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Many, many thanks dwise1. Finally got things working.

    I was using the compiler that my school has on their server, and it didn't give me any warnings and was totally leaving me in the dark. Guess that means I should go look for a different compiler.

IMN logo majestic logo threadwatch logo seochat tools logo