Computer Science trouble... again

Discussion in 'Programming General' started by yourmaster10, Jan 8, 2009.

Computer Science trouble... again
  1. Unread #1 - Jan 8, 2009 at 3:57 PM
  2. yourmaster10
    Joined:
    Jul 2, 2007
    Posts:
    199
    Referrals:
    0
    Sythe Gold:
    0

    yourmaster10 Active Member

    Computer Science trouble... again

    I'm trying to make a program to calculate taxes, and i'm getting a lot of output errors, but, i haven't changed my style of writing, nor have i changed anything else, any help? I'll copy the code to see if anyone can help.
    I'm getting syntax errors on lines 16(2 of them), 33, and 34(2 here as well)

    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    using namespace std
    ;void main()
    {
    	double grossIncome;
    	double hoursWorked;
    	double netIncome;
    	double incomeTax;
    	double provincialTax;
    	double pension;
    	double EI;
    	double income;
    	cout<< "Welcome"<<endl"This program will help you with your taxes, and will figure your net income."<<endl<endl;
    	cout<< "I will take your hourly salary and your hours worked in, and deduct all taxes"<<endl<<endl;
    	cout<< "then I will display your net income."<<endl<<endl;
    	cout<< "Please input your hourly salary";
    	cin>>income;
    	cout<<endl<<endl<<"Please input how many hours you worked this week"<<endl;
    	cin>>hoursWorked;
    	if (hoursWorked>40)
    		netIncome = (40 * income) + ((hoursWorked - 40)*(income * 1.5));
    	else netIncome = hoursWorked * income;
    	incomeTax = netIncome * .17;
    	provincialTax = netIncome * .07;
    	pension = netIncome * .039;
    	EI = netIncome * .0255;
    	grossIncome = netIncome - incomeTax - provincialTax - pension - EI;
    	cout<<endl<<"Your gross income is "<<grossIncome<<"! Wow, the taxman bites eh?"<<endl;
    	system ("pause");
    }
     
  3. Unread #2 - Jan 8, 2009 at 7:33 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Computer Science trouble... again

    My comments:
    • Don't be afraid to let the program take up more lines and space if that improves the readability (leave whitespaces to separate different sections, and spaces before and after operators, etc).
    • Don't hardcode your floating point values that represent the income tax rate and others. Implement them as constants so that the program could be more easily modified to calculate the same thing at a location with different rates.
    • Don't get used to using "endl" as it can't be used with C/C++'s printf() function which is better to use for output in smaller programs as not including the "std" namespace and iostream library usually reduces the size of your executable by about 500kb. Using the newline character "\n" is much better to get used to as it is used across many other languages, included but not limited to plain C.
    • Use getchar() to pause the program before closing so you can view output as it can be used in other compilers, even under different operating systems.
    • You didn't use the iomanip or math.h libraries so there is no point including them.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    using namespace std;
    int main() {
    	double grossIncome, hoursWorked, netIncome;
    	double incomeTax, provincialTax;
    	double pension, EI, income;
    	const INCOME_TAX_RATE = 0.17, PROVINCIAL_TAX_RATE = 0.07, PENSION_RATE = 0.039, EI_RATE = 0.0255;
    	
    	cout << "Welcome \n\n";
    	cout << "This program will help you with your taxes, and will figure your net income.\n\n";
    	cout << "I will take your hourly salary and your hours worked in, and deduct all taxes\n\n";
    	cout << "then I will display your net income.\n\n";
    
    	cout << "Please input your hourly salary: ";
    	cin >> income;
    
    	cout << "\n\nPlease input how many hours you worked this week\n";
    	cin >> hoursWorked;
    
    	if (hoursWorked > 40)
    		netIncome = (40 * income) + ((hoursWorked - 40) * (income * 1.5));
    	else netIncome = hoursWorked * income;
    
    	incomeTax = netIncome * INCOME_TAX_RATE;
    	provincialTax = netIncome * PROVINCIAL_TAX_RATE;
    	pension = netIncome * PENSION_RATE;
    	EI = netIncome * EI_RATE;
    	grossIncome = netIncome - incomeTax - provincialTax - pension - EI;
    	cout << "\nYour gross income is " << grossIncome << "! Wow, the taxman bites eh?\n";
    	getchar();
    }
    
    EDIT:
    If you stop using the "std" namespace your executable is a mere 16.7kb. :)

    Code:
    #include <stdio.h>
    
    int main() {
    	double grossIncome, hoursWorked, netIncome;
    	double incomeTax, provincialTax;
    	double pension, EI, income;
    	const double INCOME_TAX_RATE = 0.17, PROVINCIAL_TAX_RATE = 0.07, PENSION_RATE = 0.039, EI_RATE = 0.0255;
    	
    	printf("Welcome \n\n");
    	printf("This program will help you with your taxes, and will figure your net income.\n\n");
    	printf("I will take your hourly salary and your hours worked in, and deduct all taxes\n\n");
    	printf("then I will display your net income.\n\n");
    
    	printf("Please input your hourly salary: ");
    	scanf("%lf", &income);
    
    	printf("\n\nPlease input how many hours you worked this week\n");
    	scanf("%lf", &hoursWorked);
    
    	if (hoursWorked > 40) 
    		netIncome = (40 * income) + ((hoursWorked - 40) * (income * 1.5));
    	else netIncome = hoursWorked * income;
    
    	incomeTax = netIncome * INCOME_TAX_RATE;
    	provincialTax = netIncome * PROVINCIAL_TAX_RATE;
    	pension = netIncome * PENSION_RATE;
    	EI = netIncome * EI_RATE;
    	grossIncome = netIncome - incomeTax - provincialTax - pension - EI;
    	printf("\nYour gross income is %lf! Wow, the taxman bites eh?\n", grossIncome);
    	getchar();
    	getchar();
    }
     
  5. Unread #3 - Jan 8, 2009 at 7:48 PM
  6. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Computer Science trouble... again

    Code:
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    
    using namespace std;
    
    void main()
    {
    	double grossIncome, hoursWorked, netIncome, incomeTax, provincialTax, pension, EI, income;
    	printf("Welcome\nThis program will help you with your taxes, and will figure your net income.\n\n");
    	printf("I will take your hourly salary and your hours worked in, and deduct all taxes\n");
    	printf("then I will display your net income.\n");
    	printf("Please input your hourly salary: ");
    	scanf("%f", &income);
    	printf("\nPlease input how many hours you worked this week: ");
    	scanf("%f", &hoursWorked);
    	if (hoursWorked>40.0)
    		netIncome = (40 * income) + ((hoursWorked - 40)*(income * 1.5));
    	else netIncome = hoursWorked * income;
    	incomeTax = netIncome * 0.17;
    	provincialTax = netIncome * 0.07;
    	pension = netIncome * 0.039;
    	EI = netIncome * 0.0255;
    	grossIncome = netIncome - incomeTax - provincialTax - pension - EI;
    	printf("Your gross income is %f! Wow, the taxman bites eh?", grossIncome);
    }
    That is my take. Compiles without error.

    You had a few syntax errors for the most part. Such as:
    Code:
    <<endl<endl;
    Do excuse my use of printf() and scanf() if you intended to use the output and input streams (cout and cin). I just like to use them more.

    If you're using MS VC++ you will need to add
    Code:
    #include "stdafx.h"
    to the includes.

    Edit:

    *sighs* I guess it's too late to say InB4Nullware =/
     
  7. Unread #4 - Jan 9, 2009 at 12:19 AM
  8. yourmaster10
    Joined:
    Jul 2, 2007
    Posts:
    199
    Referrals:
    0
    Sythe Gold:
    0

    yourmaster10 Active Member

    Computer Science trouble... again

    thank you guys, I have to use cin/cout because that's the course standard,
    also, thanks for helping me clean up my variables/constants.
    Hmm... the std namespace was the only way i could get earlier programs to run, and i just copy/paste the beginning bit, b/c as of now, it stays almost the same all the time.

    And thanks for all the other pointers you two, made my day:)
    If you have any more time available, could you add my msn and help me get an order for a dif program I have to do, I don't want you to give me the code, just a little jump start if you will.

    EDIT:
    I assumed that nullware's would work... it has 42 warnings and 5 errors in VisualC++ 2005 express edition. I used your first btw.
    EDIT2:missed opening brace, wow! I need to read before I post, sorry Nullware.
     
  9. Unread #5 - Jan 9, 2009 at 8:16 AM
  10. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Computer Science trouble... again

    Haha. :D

    I'll add you on MSN tonight probably then.
     
  11. Unread #6 - Jan 9, 2009 at 11:34 AM
  12. yourmaster10
    Joined:
    Jul 2, 2007
    Posts:
    199
    Referrals:
    0
    Sythe Gold:
    0

    yourmaster10 Active Member

    Computer Science trouble... again

    K, talk to you tonight. I'll be on for a while Null... make sure you got [email protected] don't know if you added me yet or not, but nothing's popping up on msn.
     
< Making Proffessional Layouts | help me with my script >

Users viewing this thread
1 guest


 
 
Adblock breaks this site