Her opinion, your opinion

This is a discussion on Her opinion, your opinion within the C++ Programming forums, part of the General Programming Boards category; Ok, the teacher gave us a out-of-book program to write. Now she says that the program is doing the intention, ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Her opinion, your opinion

    Ok, the teacher gave us a out-of-book program to write. Now she says that the program is doing the intention, but that she found "about 15 lines that can be done in a better manner". Now i'm sure there are 15, but based on what we have learned in class (because i can't apply things she hasn't taught for in-class work) i cannot rlly find anything. Could you please review/run my code and tell me what you may have done different? The only thing i truely hate is how i managed the deductions of the constants in the find_net function.


    /* Edit: The iomanip is included because i plan to add a setiosflags line to format output. Thats not required so don't count that as an error*/


    Code:
    /*	Steven Billington
    	December 20, 2002
    	NatPine.cpp
    
    	Program finds netpay for employees.
    */
    
    #include "stdafx.h"
    #include <iostream.h>
    #include <iomanip.h>
    
    /*	Prototype functions*/
    double find_gross(double &worked, double &rate);
    double find_net(double &grosspay);
    
    /*	Const for deductions*/
    const double FED_TAX = 0.18;
    const double S_TAX = 0.045;
    const double HOSP = 26.65;
    const double UNION = 7.85;
    
    int main(int argc, char* argv[])
    {
    	/*	Input/function calls*/
    	double hours,
    		   hr_rate,
    		   gross,
    		   net_pay;
    
    	/*	Prompt user*/
    	cout<<"Enter hours: ";
    	cin>>hours;
    	cout<<"Enter rate: ";
    	cin>>hr_rate;
    
    	/*	Call function*/
    	gross = find_gross(hours,hr_rate);
    
    	cout<<"\nGross hours worked: "<<"\t"
    		<<gross<<endl;
    
    	/*	Call function, output return*/
    	cout<<"\nNet pay is: "<<"\t\t"
    		<<find_net(gross)<<endl;
    
    	return 0;
    }
    
    /*	Function definition*/
    /*	Function finds gross pay based on user
    	given values*/
    double find_gross(double &worked, double &rate)
    {
    	double gross_pay;
    
    	gross_pay = worked * rate;
    
    	return gross_pay;
    }
    
    /*	Function Declaration*/
    /*	Function finds net pay based on return
    	of find_gross function by making
    	deductions*/
    double find_net(double &grosspay)
    {
    	double net_is,
    		   percent_loss;
    
    	percent_loss = grosspay * FED_TAX;
    	grosspay -= percent_loss;
    	percent_loss = grosspay * S_TAX;
    	grosspay -= percent_loss;
    	grosspay -= HOSP;
    	grosspay -= UNION;
    
    	net_is = grosspay;
    
    	return net_is;
    }

  2. #2
    lurker
    Guest
    I compiled your code, and you're not using the net_pay variable
    in 'main'. I also changed your find net function a little. This may or may not be better, just a thought

    Code:
    double find_net(double &grosspay)
    {
    	double net_is,
    		   percent_loss;
    
    percent_loss = (grosspay * FED_TAX) + (grosspay * S_TAX);
    
    net_is = grosspay - (percent_loss + HOSP + UNION);
    
    return net_is;
    }
    I didn't test it to see if it works, but my basic thoughts are, fewer lines (although I usually like more but simpler lines), and this way 'grosspay' isn't changed. Just some ideas.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    You could rewrite find_gross as an inline function.
    Mr. C: Author and Instructor

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Personally, I'd go with clarity before deciding to make it an IOCCC entry for the sake of a few lines.
    What fun is that?
    Code:
    /*
      NatPine.cpp:
    
        Creation (2002,12,20) Steven Billington
        Edit (2002,12,21) JSW
    
        Program finds netpay for employees.
    */
    #include <iostream>
    
    double find_gross(double worked, double rate);
    double find_net(double grosspay);
    
    // Gross dependent deductions (percentage)
    const double FED_TAX = 0.18;
    const double S_TAX   = 0.045;
    // Gross independent deductions
    const double HOSP    = 26.65;
    const double UNION   = 7.85;
    
    int main()
    {
      double hours, hr_rate, gross;
      
      std::cout<<"Enter hours worked and hourly rate: ";
      std::cin>> hours >> hr_rate;
    
      gross = find_gross(hours, hr_rate);
      
      std::cout<<"\nGross pay:\t"<< gross;
      std::cout<<"\nNet pay:  \t"<< find_net(gross) <<std::endl;
    }
    
    double find_gross(double worked, double rate)
    {
      return worked * rate;
    }
    
    double find_net(double grosspay)
    {
      return grosspay - ((grosspay * FED_TAX) + (grosspay * S_TAX) + HOSP + UNION);
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    thnx guys, that helps alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Public opinion reports (PIPA)
    By novacain in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 02-04-2005, 10:06 AM
  2. A matter of opinion
    By pepperblue255 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2002, 09:51 PM
  3. Freedom of opinion
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-10-2002, 06:06 AM
  4. opinion about books
    By clement in forum C Programming
    Replies: 7
    Last Post: 09-24-2001, 04:18 PM

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