Thread: Newbie Question

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    Newbie Question

    I want to get the workers salary, then display the salary after taxes are taken out(salary divided by 25%). So I need to know the correct way to divide salary by 25% then make it into a variable called net. Thanks alot.


    net = salary / 25%

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    25percent = 0.25 so you need to make all your variables double...
    Code:
    double net = 0;
    double salary = 0;
    double tax = .25;
    //set salary and other things that your program does
    /*[edit]:ooops!*/net = ( salary*tax );
    axon
    Last edited by axon; 09-25-2003 at 04:40 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    net = slaray*.25;

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Thanks alot

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    int salary, net; // defines two integer variables

    You don't want to "divide by 25%" ! You need to multiply by 0.25 to get the tax. tax = salary * 0.25; Then, you can subtract this from the salary.

    The cleanest way to do this would be to multiply the salary by 0.75 to get the net directly.

    It would also be "better" (more flexable) to make the tax rate a variable: float TaxRate = 0.25 ; Then, calculate net = Salary * (1-TaxRate) ;

    If you want to show dollars and cents, you'll need to use float rather than int.

    I hope that gets you started. If you have problems, post your code. And, if you haven't done so yet, read the board guidelines. They will tell you to use a clear-specific title for your posts, and to post whatever code you have so far. Also see the post regarding code tags. This will tell you how to post your code.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    I did as you suggested, net = salary/tax; , with salary input at 100 it gives me an answer of 400????? For some reason it seems to be multiplying (salary)100 times 25


    double salary; //workers hourly wage
    double net; //workers wage after taxes are taken out
    double tax; //ammount of taxes to be subtracted from salary
    tax = (.25);
    cout <<"Please input workers salary: ";
    cin >>salary;
    cout <<"\n";
    net = salary/tax;
    cout <<"Here is workers salary after taxes: "<<net;
    return 0;

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Dougdbug you nailed it, thanks alot, & thanks to everyone for the help.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Wink Glad I could help !

    Computer problems are alot like math word-problems.

    There are two problems to solve when writing a program. First, you have to figure out how to solve the problem (the formula or algorithm). Then, you have to figure out how to make the program do it for you.

    In this case it would have been helpful to try-out the formula with a calculator first... I should have sugggested that. If you have a slightly more complicated formula, a spreadsheet might be a better way to try it out.

    When things get really complicated, or really long (with looping etc.), it's easier to go ahead and write the program and try-out your formula/algorithm in the program.

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    I have another problem here, I added hours & overtime into the program, overtime pay being salary+1, if over 40 hours. it compiles correctly but it takes the last dollar & rounds up, 41*11 = 451, 451 * .75 = 338.25. When I run the program it comes up with 339 as the gross after taxes answer?!?!

    double salary; //workers hourly wage
    double gross;
    double tax;
    double hours;

    cout <<"Please enter workers total hours: ";
    cin >>hours;
    cout <<"\n";


    cout <<"Please input workers salary: ";
    cin >>salary;
    cout <<"\n";

    gross = salary*hours;

    if (hours <= 40)
    tax = gross*.75;
    else
    gross = (salary+1) * hours;
    tax = (gross+1) *.75;


    cout <<"Workers gross pay is: "<<gross<<endl;
    cout <<"Here is workers salary after taxes: "<<tax;
    return 0;

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Use code tags please. You've already been told once.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Your problem occurs because you haven't placed braces around your over 40 hour code:
    Code:
    if (hours <= 40)
    tax = gross*.75;
    else
    gross = (salary+1) * hours;
    tax = (gross+1) *.75;
    This code means that your after tax salary is always calculated based on (gross+1). Change it to this:
    Code:
    if (hours <= 40)
    tax = gross*.75;
    else
    {
    gross = (salary+1) * hours;
    tax = (gross+1) *.75;
    }
    Then it works fine.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Can I ask why you calculate >40 hours tax based on (gross+1)? Shouldn't the tax always be just 75% of gross earnings? I would do it like this:
    Code:
    cout <<"Please enter workers total hours: ";
    cin >>hours;
    cout <<"\n";
    
    
    cout <<"Please input workers salary: ";
    cin >>salary;
    cout <<"\n";
    
    if (hours <= 40)
        gross = salary*hours;
    else
        gross = (salary+1) * hours;
    
    tax = gross*.75;
    Besides being more correct (IMO), that code is actually faster (technically) and more efficient.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    robshira: I suggest always using braces...even when you are only executing a single line/item. It is a good hait to get into at an early stage. It saves debugging time when your programming assignemnts get LARGE! so for the if else always use braces:
    Code:
    if ( condition ) {
           //stuff......
    } else if ( condition2 ) {
          //more stuff.....
    } else { //default
         //some more....
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    7
    Ok, first of all id like to thank all those that helped : ) Now, I still have the same problem, the code is adding 40+ hours(overtime pay) to be more than it acually is.

    Code:
    #include <iostream.h>
    
    main()
    
    {
    
    	double salary; //workers hourly wage
        double gross;
    	double tax;
    	double hours;
        cout <<"Please enter workers total hours: ";
    		cin >>hours;
    		cout <<"\n";
    	
    	
    	cout <<"Please input workers salary: ";
    	cin >>salary;
    		cout <<"\n";
    	
    		gross = salary*hours;
    
    	if (hours <= 40)
    tax = gross*.75;
    else
    {
    gross = (salary+1) * hours;
    tax = (gross+1) *.75;
    }
    
    	
        cout <<"Workers gross pay is: "<<gross<<endl;   
    	cout <<"Here is workers salary after taxes: "<<tax;
    		return 0;
    }

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    if (hours <= 40)
      tax = gross*.75;
    else
    {
      gross = (salary+1) * hours;
      tax = (gross+1) *.75;
    }
    
    
    review the above like you have posted vs below
    
    
    if (hours <= 40)
    {
      net = gross*.75;
    }
    else
    {
      gross = (salary+1) * hours;
      net = gross *.75;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM