Thread: Input showing as default values

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    33

    Input showing as default values

    Ok, I wanted to try and avoid posting this whole solution, but I don't see any other to address the problems I'm having. So I apologize in advance, this is going to be a long post.

    Same solution that I was having compiler errors on yesterday. We're working with inheritance and the program is supposed to be to create and display information for default Employee objects, Hourly Employee objects and Salaried Employee objects. Everything compiles as is, but I'm running into two issues that I can't figure out.

    1) In both the hourly and salaried objects, the wage, hours, and management level attributes all displaying their default values. I'm almost positive that this has something to do with the str to int and str to double conversions that I'm using (currently have atoi and atof in the file, but I've also tried stringstream, but still had to same problem). Any thoughts as to what I'm missing?

    2) the assignment calls for the Benefit benefit member in Employee.h to be protected. However this makes the member unaccessible when I try to use it in the EmployeeMain.cpp in order to set the input for the Benefit class members. I had to make the Benefit benefit member public again to get it to compile. How would I set the input for the Benefit class members while the Benefit benefit member is protected?

    Thanks in advance for any help.

    Solution files follow:

    EmployeeMain.cpp
    Code:
    #include "Hourly.h"
    #include "Salaried.h"
    
    void DisplayApplicationInformation();
    void DisplayDivider(string);
    string GetInput(string);
    void TerminateApplication();
    int Employee::numEmployees = 0;
    
    int main()
    {
        DisplayApplicationInformation();
        string input = "";
        
        DisplayDivider("      EMPLOYEE #1      ");
        cout << endl;
        
        Benefit benefit1("Atena", 250000, 45);
        Employee emp1("John","Jones",'M', 3, 100000, benefit1);
    
        emp1.Employee::displayEmployee();
    
        DisplayDivider("Number of Employees");
        cout << "Number of employees: " << Employee::getnumEmployees() << endl;
        cout << endl;
    
        DisplayDivider("      EMPLOYEE #2      ");
        cout << endl;
    
        Salaried emp2;
    
        input = GetInput("First Name");
        emp2.setFirstName(input);
        input = GetInput("Last Name");
        emp2.setLastName(input);
        input = GetInput("Gender");
        emp2.setGender(input.at(0));    //.at() converts str to char
        input = GetInput("Dependent");
        emp2.setDependent(input);        //overloaded method, str to int.
        input = GetInput("Annual Salary");
        emp2.setAnnualSalary(input);    //overloaded method, str to double
        input = GetInput("Health Insurance Company");
        emp2.benefit.setHealthInsurance(input);
        input = GetInput("Amount of Life Insurance");
        emp2.benefit.setLifeInsurance(atof(input.c_str()));
        input = GetInput("Vacation Time");
        emp2.benefit.setVacation(atoi(input.c_str()));
        input = GetInput("Management Level, 0-3");
        emp2.setManagementLevel(atoi(input.c_str()));
        cout << endl;
        
        emp2.Salaried::displayEmployee();
    
        DisplayDivider("Number of Employees");
        cout << "Number of employees: " << Employee::getnumEmployees() << endl;
        cout << endl;
    
        DisplayDivider("      EMPLOYEE #3      ");
        cout << endl;
     
        Hourly emp3;
    
        input = GetInput("First Name");
        emp3.setFirstName(input);
        input = GetInput("Last Name");
        emp3.setLastName(input);
        input = GetInput("Gender");
        emp3.setGender(input.at(0));   
        input = GetInput("Dependent");
        emp3.setDependent(input);        
        input = GetInput("Employment Status? - FT, PT, TEMP");
        emp3.setCatagory(input);
        input = GetInput("Hourly Wage");
        emp3.setWage(atof(input.c_str()));
        input = GetInput("Number of Hours Worked");
        emp3.setHours(atof(input.c_str()));
        input = GetInput("Health Insurance Company");
        emp3.benefit.setHealthInsurance(input);
        input = GetInput("Amount of Life Insurance");
        emp3.benefit.setLifeInsurance(atof(input.c_str()));
        input = GetInput("Vacation Time");
        emp3.benefit.setVacation(atoi(input.c_str()));
        cout << endl;
        
        emp3.Hourly::displayEmployee();
    
        DisplayDivider("Number of Employees");
        cout << "Number of employees: " << Employee::getnumEmployees() << endl;
        cout << endl;
    
        TerminateApplication();
        system("pause");
        return 0;
    }
    
    void DisplayApplicationInformation()
    {
        cout << endl;
        cout << "Welcome to the Employee Class Program" << endl;
        cout << "CIS247C, Week5 Lab" << endl;
        cout << "Developer: John Worley" << endl;
        cout << endl;
    }
    
    void DisplayDivider(string outputTitle)
    {
        cout << setw(12) << outputTitle << setw(12) << endl;
        cout << "-----------------------" << endl;
    }
    
    string GetInput(string input) //general input function; takes input as string
    {
        cout << "Enter " << input << ": ";   // no endl, in order to allow input on same line
        getline(cin, input);
        return input;
    }
    
    void TerminateApplication() // terminate program message
    {
        cout << "Thank you for using the Employee class program" << endl;
        cout << "Program terminating" << endl;
        cout << "Good-Bye" << endl;
        cout << endl;
    }
    Benefit.h

    Code:
    # pragma once
    #include <iostream>
    #include <string>  
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    class Benefit
    {
    private:
        string HealthInsurance;
        double LifeInsurance;
        int Vacation;
        
    public:
        Benefit();
        Benefit(string health, double life, int vac);
        void displayBenefits();
        string getHealthInsurance();
        void setHealthInsurance(string health);
        double getLifeInsurance();
        void setLifeInsurance(double life);
        int getVacation();
        void setVacation(int vac);
    };
    Benefit.cpp
    Code:
    #include "Benefit.h"
    
    static const double Min_Life = 0;
    static const double Max_Life = 1000000;
    static const int Min_Vacation = 0;
    static const int Max_Vacation = 365;
    
    Benefit::Benefit()
    {
        string HealthInsurance = "NG";  //NG stands for not given
        double LifeInsurance = 0;
        int Vacation = 0;
    }
    
    Benefit::Benefit(string health, double life, int vac)
    {
        HealthInsurance = health;
        LifeInsurance = life;
        Vacation = vac;
    }
    
    void Benefit::displayBenefits()
    {
        cout << endl;
        cout << "Benefits Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Health Insurance Company: " << setw(5) << HealthInsurance << endl;
        cout << "Amount of Life Insurance: $" << setw(5) << LifeInsurance << endl;
        cout << "Vacation Time: " << setw(13) << Vacation << " days" << endl;
        cout << endl;
    }
    
    string Benefit::getHealthInsurance()
    {
        return HealthInsurance;
    }
    
    void Benefit::setHealthInsurance(string health)
    {
        HealthInsurance = health;
    }
    
    double Benefit::getLifeInsurance()
    {
        return LifeInsurance;
    }
    
    void Benefit::setLifeInsurance(double life)
    {
        if(life >= Min_Life && life <= Max_Life)
        {
            LifeInsurance = life;
        }
        else if(life < Min_Life)
        {
            LifeInsurance = Min_Life;
        }
        else
        {
            LifeInsurance = Max_Life;
        }
    }
    
    int Benefit::getVacation()
    {
        return Vacation;
    }
    
    void Benefit::setVacation(int vac)
    {
        if(vac >= Min_Vacation && vac <= Max_Vacation)
        {
            Vacation = vac;
        }
        else if(vac < Min_Vacation)
        {
            Vacation = Min_Vacation;
        }
        else
        {
            Vacation = Max_Vacation;
        }
    }
    Employee.h
    Code:
    #pragma once
    #include "Benefit.h"
    
    class Employee
    {
    protected:
        string FirstName, LastName;
        char Gender;
        int Dependent;
        double AnnualSalary;
        
    
    private:
        static int numEmployees;
        
    public:
        Benefit benefit;
        Employee();
        Employee(string fName, string lName, char gen, int depend, double salary, Benefit ben);
        double calculatePay();
        void displayEmployee();
        string getFirstName();
        void setFirstName(string fName);
        string getLastName();
        void setLastName(string lName);
        char getGender();
        void setGender(char gen);
        int getDependent();
        void setDependent(int depend);
        void setDependent(string depend);
        double getAnnualSalary();
        void setAnnualSalary(double salary);
        void setAnnualSalary(string salary);
        static int getnumEmployees();
        Benefit getBenefit();
        void setBenefit(Benefit ben);
    };
    Employee.cpp
    Code:
    #include "Employee.h"
    
    static const int Max_Depend = 10;
    static const int Min_Depend = 0;
    static const double Max_Salary = 250000;
    static const double Min_Salary = 20000;
    static const char Default_Gender = 'U';
    
    Employee::Employee()
    {
        FirstName = "not given";
        LastName = "not given";
        Gender = 'U';
        Dependent = 0;
        AnnualSalary = 20000;
        benefit = Benefit();
        numEmployees++;
    }
    
    Employee::Employee(string fName, string lName, char gen, int depend, double salary, Benefit ben)
    {
        
        FirstName = fName;
        LastName = lName;
        Gender = gen;
        Dependent = depend;
        AnnualSalary = salary;
        benefit = ben;
        numEmployees++;
    }
    
    double Employee::calculatePay()
    {
        return AnnualSalary/52;
    }
    
    void Employee::displayEmployee()
    {
        cout << endl;
        cout << "Employee Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
        cout << "Gender: " << setw(12) << Gender << endl;
        cout << "Dependents: " << setw(8) << Dependent << endl;
        cout << showpoint << fixed << setprecision(2);
        cout << "Annual Salary: " << setw(5) << "$" << AnnualSalary << endl;
        cout << "Weekly Salary: " << setw(5) << "$" << calculatePay() << endl;
        benefit.displayBenefits();
        cout << endl;
    }
    
    string Employee::getFirstName()
    {
        return FirstName;
    }
    
    void Employee::setFirstName(string fName)
    {
        FirstName = fName;
    }
    
    string Employee::getLastName()
    {
        return LastName;
    }
    
    void Employee::setLastName(string lName)
    {
        LastName = lName;
    }
    
    char Employee::getGender()
    {
        return Gender;
    }
    
    void Employee::setGender(char gen)
    {
        switch(gen)
        {
        case 'f':case 'F': case 'm': case 'M':
            Gender = gen;
            break;
        default:
            Gender = Default_Gender;
        }
    }
    
    int Employee::getDependent()
    {
        return Dependent;
    }
    
    void Employee::setDependent(int depend)
    {
         if(depend >= Min_Depend && depend <= Max_Depend)
         {
             Dependent = depend;
         }
         else if(depend < Min_Depend)
         {
             Dependent = Min_Depend;
         }
         else
         {
             Dependent = Max_Depend;
         }
    }
    
    void Employee::setDependent(string input) //overloaded method set as string
    {
        Dependent = atoi(input.c_str());      //str to int 
    }
    
    double Employee::getAnnualSalary()
    {
        return AnnualSalary;
    }
    
    void Employee::setAnnualSalary(double salary)
    { 
        if(salary >= Min_Salary && salary <= Max_Salary)
         {
             AnnualSalary = salary;
         }
         else if(salary < Min_Salary)
         {
             AnnualSalary = Min_Salary;
         }
         else
         {
             AnnualSalary = Max_Salary;
         }
    }
    
    void Employee::setAnnualSalary(string input)
    {
        AnnualSalary = atof(input.c_str());  //str to double
    }
    
    int Employee::getnumEmployees()
    {
        return numEmployees;
    }
    
    Benefit Employee::getBenefit()
    {
        return benefit;
    }
    
    void Employee::setBenefit(Benefit ben)
    {
        benefit = ben;
    }
    Salaried.h
    Code:
    #include "Employee.h"
    
    class Salaried:public Employee
    {
    private:
        int ManagementLevel;
    public:
        Salaried();
        Salaried(string fName, string lName, char gen, int depend, double salary, Benefit ben, int manLevel);
        Salaried(double salary, int manLevel);
        int getManagementLevel();
        void setManagementLevel(int manLevel);
        double CalculatePay();
        void displayEmployee();
    };
    Salaried.cpp
    Code:
    #include "Salaried.h"
    
    static const int Min_Management = 0;
    static const int Max_Management = 3;
    static const double Bonus_Percent = 10;
    
    Salaried::Salaried():Employee()
    {
        int manLevel = 0;
    }
    
    Salaried::Salaried(string fName, string lName, char gen, int depend, double salary, Benefit ben, int manLevel):Employee(fName,lName,gen,depend,salary,ben)
    {
    }
    
    Salaried::Salaried(double salary, int manLevel):Employee()
    {
        setAnnualSalary(salary);
    }
    
    double Salaried::CalculatePay()
    {
        double Bonus_Percent = ManagementLevel * 0.10;
        return (AnnualSalary + Bonus_Percent) / 52;
    }
    
    void Salaried::displayEmployee()
    {
        cout << endl;
        cout << "Employee Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
        cout << "Gender: " << setw(12) << Gender << endl;
        cout << "Dependents: " << setw(8) << Dependent << endl;
        cout << "Salaried Employee" << endl;
        cout << "Management Level: " << setw(2) << Salaried::getManagementLevel() << endl;
        cout << showpoint << fixed << setprecision(2);
        cout << "Annual Salary: " << setw(5) << "$" << AnnualSalary << endl;
        cout << "Weekly Salary: " << setw(5) << "$" << Salaried::CalculatePay() << endl;
        cout << endl;
        benefit.displayBenefits();
        cout << endl;
    }
    
    int Salaried::getManagementLevel()
    {
        return ManagementLevel;
    }
    
    void Salaried::setManagementLevel(int manLevel)
    {
        if(ManagementLevel >= Min_Management && ManagementLevel <= Max_Management)
        {
            ManagementLevel = manLevel;
        }
        else
            if(ManagementLevel < Min_Management)
            {
                ManagementLevel = Min_Management;
            }
            else
            {
                ManagementLevel = Max_Management;
            }
    }
    Hourly.h
    Code:
    #include "Employee.h"
    
    class Hourly:public Employee
    {
    private:
        double Wage;
        double Hours;
        string Catagory;
    public:
        Hourly();
        Hourly(double hwage, double hrs, string catagory);
        Hourly(string fname, string lname, char gen, int dep, double hwage, double hrs, Benefit ben, string catagory);
        double CalculatePay();
        void displayEmployee();
        double getWage();
        void setWage(double hwage);
        double getHours();
        void setHours(double hrs);
        string getCatagory();
        void setCatagory(string catagory);
        double getAnnualSalary();
        void setAnnualSalary(double salary);
    };
    Hourly.cpp
    Code:
    #include "Hourly.h"
    
    const double MIN_WAGE = 10;
    const double MAX_WAGE = 75;
    const double MIN_HOURS = 0;
    const double MAX_HOURS = 50;
    
    Hourly::Hourly():Employee()
    {
        double Wage = 0; 
        double Hours = 0;
        string Catagory = "";
    }
    
    Hourly::Hourly(double hwage, double hrs, string catagory):Employee()
    {
    }
    
    Hourly::Hourly(string fName, string lName, char gen, int dep, double hwage, double hrs, Benefit ben, string catagory):Employee()
    {
        Wage = hwage;
        Hours = hrs;
        Catagory = catagory;
    }
    
    double Hourly::CalculatePay()
    {
        return Wage * Hours;
    }
    
    void Hourly::displayEmployee()
    {
        cout << endl;
        cout << "Employee Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Employee Name: " << setw(8) << FirstName << " " << LastName << endl;
        cout << "Gender: " << setw(12) << Gender << endl;
        cout << "Dependents: " << setw(8) << Dependent << endl;
        cout << showpoint << fixed << setprecision(2);
        cout << "Hourly Employee" << endl;
        cout << "Employment Status: " << Hourly::getCatagory() << endl;
        cout << "Wage: " << setw(14) << "$" << Wage << " per hour " << endl;
        cout << "Hours: " << setw(16) << Hours << endl;
        cout << "Annual Salary: " << setw(5) << "$" << Hourly::getAnnualSalary() << endl;
        cout << "Weekly Salary: " << setw(5) << "$" << Hourly::CalculatePay() << endl;
        cout << endl;
        cout << "Benefits Information" << endl;
        cout << "----------------------------------" << endl;
        cout << "Health Insurance Company: " << setw(5) << benefit.getHealthInsurance() << endl;
        cout << "Amount of Life Insurance: $" << setw(5) << benefit.getLifeInsurance() << endl;
        cout << "Vacation Time: " << setw(13) << benefit.getVacation() << " days" << endl;
        cout << endl;
        
        cout << endl;
    }
    
    double Hourly::getWage()
    {
        return Wage;
    }
    
    void Hourly::setWage(double hwage)
    {
        if(Wage >= MIN_WAGE && Wage <= MAX_WAGE)
        {
            Wage = hwage;
        }
        else
            if(Wage < MIN_WAGE)
            {
                Wage = MIN_WAGE;
            }
            else
            {
                Wage = MAX_WAGE;
            }
    }
    
    double Hourly::getHours()
    {
        return Hours;
    }
    
    void Hourly::setHours(double hrs)
    {
        if(Hours >= MIN_HOURS && Hours <= MAX_HOURS)
        {
            Hours = hrs;
        }
        else  
            if(Hours < MIN_HOURS)
            {
                Hours = MIN_HOURS;
            }
            else
            {
                Hours = MAX_HOURS;
            }
    
    }
    
    string Hourly::getCatagory()
    {
        return Catagory;
    }
    
    void Hourly::setCatagory(string catagory)
    {
        Catagory = catagory;
    }
    
    double Hourly::getAnnualSalary()
    {
        return AnnualSalary;
    }
    
    void Hourly::setAnnualSalary(double salary)
    {
        AnnualSalary = Hourly::CalculatePay() * 50;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by worl4125
    1) In both the hourly and salaried objects, the wage, hours, and management level attributes all displaying their default values. I'm almost positive that this has something to do with the str to int and str to double conversions that I'm using (currently have atoi and atof in the file, but I've also tried stringstream, but still had to same problem). Any thoughts as to what I'm missing?
    Use a debugger and trace why the values aren't changing.

    If you want us to examine the code, then post the smallest and simplest compilable program that demonstrates the problem. All you need is a single example where a particular variable should not have its default value, yet still has it. You don't need "both the hourly and salaried objects, the wage, hours, and management level attributes". Just one will do.

    Quote Originally Posted by worl4125
    2) the assignment calls for the Benefit benefit member in Employee.h to be protected. However this makes the member unaccessible when I try to use it in the EmployeeMain.cpp in order to set the input for the Benefit class members. I had to make the Benefit benefit member public again to get it to compile. How would I set the input for the Benefit class members while the Benefit benefit member is protected?
    One way is to provide getter/setter functions to set the Benefit member of the Employee. I notice that this is already in the Employee class interface.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Here's a problem:
    Code:
    Hourly::Hourly():Employee()
    {
        double Wage = 0; 
        double Hours = 0;
        string Catagory = "";
    }
    You need to remove "double" and "string" from here since you don't want to create new local variables but instead want to set the member variables.

    Also, you don't need ":Employee()" here since the default behavior will be to call that default constructor.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Use a debugger and trace why the values aren't changing.

    If you want us to examine the code, then post the smallest and simplest compilable program that demonstrates the problem. All you need is a single example where a particular variable should not have its default value, yet still has it. You don't need "both the hourly and salaried objects, the wage, hours, and management level attributes". Just one will do.


    One way is to provide getter/setter functions to set the Benefit member of the Employee. I notice that this is already in the Employee class interface.
    Again, my apologizes for including the whole file like that. It was late and I was frustrated and couldn't think of where the problem might be. I thought a new set of eyes would see what I wasn't. I'll refrain from it in the future.

    I completely forgot about including the Benefit getter/setter. Once I implemented those, the access problem cleared up. The wage, hours, management level members required a minor change in the setter functions. All told, the program is up and running as expected this morning.

    Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. default values for Struct elements
    By udaraliyanage in forum C Programming
    Replies: 2
    Last Post: 04-09-2011, 06:17 AM
  2. default values in struct
    By taurus in forum C Programming
    Replies: 1
    Last Post: 10-04-2009, 05:43 AM
  3. Default values of socket options
    By goofy in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-23-2008, 10:20 AM
  4. variable default values
    By dasmann in forum C++ Programming
    Replies: 12
    Last Post: 09-26-2007, 02:21 AM
  5. Default values in function prototypes
    By wdicks in forum C Programming
    Replies: 13
    Last Post: 10-10-2001, 01:06 AM