How to write a program

This is a discussion on How to write a program within the C++ Programming forums, part of the General Programming Boards category; Well I have this 2nd year course for C++ where I have to write programs depending on the explanation and ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    How to write a program

    Well I have this 2nd year course for C++ where I have to write programs depending on the explanation and objectives of what the program should do. But i am having trouble to write them...

    I was wondering if anyone would tell me how to start of a program that you will write and all you know is what the program must do.

    say I need to write a program where there are 2 classes and a main funtion. One Employee class and one staff class..

    Code:
    class Employee
    {
    	private:
    		char *		name;		// Employee's name
    		double		hourlyRate;	// Hourly pay rate
    		double		hours;		// Hours worked this week
    
    		double		taxRate ();
    
    	public:
    		Employee ();
    		~Employee ();
    
    		void		setName (char * _name);
    		void		setHourlyRate (double _hourlyRate);
    		void		setHours (double _hours);
    		void		printPayroll ();
    };
    Code:
    class Staff
    {
    	private:
    		int		totalEmployees;
    		Employee	employees[MAX_EMPLOYEES];
    
    	public:
    		Staff ();
    		~Staff ();
    
    		void		addEmployee (char * _name,
    					double _hourlyRate, double _hours);
    		int		getTotalEmployees ();
    		void		printPayroll ();
    };
    Now I need to write the definition of each member function
    this is what I got for employee

    Code:
    # include <iostream>
    # include "Employee.h"
    using namespace std;
    
    Employee::Employee() {
        name = new char [200];
        hourlyRate = 0;
        hours = 0;
    }
    
    Employee::~Employee() {
        delete[] name;
    }
    
    double Employee::taxRate () {
        int anual_Income = hourlyRate * 40 * 52;
    
        if (anual_Income < 20000) { return 15;}
    
        else if (anual_Income < 30000) return 0.20;
    
        else if (anual_Income < 40000) return 0.25;
    
        else if (anual_Income < 50000) return 0.30;
    
        else if (anual_Income < 60000) return 0.35;
    
        else if (anual_Income < 70000) return 0.40;
    
        else return 0.45;
    }
    
    char* Employee::setName (char *_name) {
    
       name = _name;
    }
    
    double Employee::setHourlyRate (double _hourlyRate) {
    
       hourlyRate = _hourlyRate;
    }
    
    double Employee::setHours (double _hours) {
    
       hours = _hours;
    }
    
    void Employee::printPayroll () {
        double overTime = 0, hours40 = hours;
            if (hours > 40) {
                overTime == hours - 40;
                hours40 = 40;}
    
        double weeklyPay = hours40 * hourlyRate + overtime * hourlyRate * 1.5;
        double tax = taxRate() * weeklyPay;
    
        cout << *name << ": " << weeklyPay << " - " <<   tax << " = " << weeklyPay - tax;
    
    }
    What am i doing wrong?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And how is what your program does different from what you (or someone else) expects?

    Code:
    if (anual_Income < 20000) { return 15;}
    
    else if (anual_Income < 30000) return 0.20;
    Aside from annual being misspelled, it seems very inconsistant to have 15 as the tax value for up to 20000, and then 0.20 up to 30000. Either it should be 0.15, or it should be 20, I expect - I didn't look at ALL your code to figure out which is the right variant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by Tashfique
    I was wondering if anyone would tell me how to start of a program that you will write and all you know is what the program must do.
    That's generally how a lot of programming is. You are given a requirement to have a program accomplish objective X and then you work towards that goal. Without knowing what your overall goal is I can't say much... you do have some syntax/logical errors as described below.

    Are you limited to using char pointers in this program? I'd seriously reconsider this if it is not the case, better to use a std::string container instead. Otherwise...
    Code:
    char* Employee::setName (char *_name) {
    
        name = _name;
    }
    I doubt this is what you want to do here. You've just caused a memory leak, you've lost the original pointer to the memory allocated in the constructor and reset the pointer to a new location. I'm guessing a call to the strcpy function is what you need here and _name should probably be declared const.

    Also,
    Code:
    if (hours > 40) {
        overTime == hours - 40;
        hours40 = 40;}
    The above looks wrong... specifically the use of the equality operator (==) instead of the assignment operator (=).

    This is a nit, but functions that do not change any data within the class should be declared const. For example, since the printPayroll function does not modify any of the class data members (name,hourlyRate,hours) it should be declared as such:
    Code:
    class Employee
    {
        ...
        void printPayroll () const;
    };

    Code:
    cout << *name << ": " << weeklyPay << " - " <<   tax << " = " << weeklyPay - tax;
    Your use of *name above is incorrect. This will just print the first letter of the person's name and not the whole thing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    It really helps if you sit down with some pen and paper and write everything down. Start planning.
    To achieve X, I must do Y, etc. Then connect them. Figure out what classes can represent this and that, then start planning the layout of classes, etc.
    Good planning from the start will save you headaches later!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Are you limited to using char pointers in this program? I'd seriously reconsider this if it is not the case, better to use a std::string container instead.
    Yes, he really should.
    And that's not the only "mistake" with char arrays in the snippet posted.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  2. how could i write this program: cat < apa | wc | wc > bepa
    By strugglingman in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 04:40 PM
  3. Is there another way to write this program?
    By agentxx04 in forum C Programming
    Replies: 1
    Last Post: 11-23-2004, 08:28 PM
  4. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM

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