Determine Gross wage using class

This is a discussion on Determine Gross wage using class within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that computes gross by using a class and three functions. When I try ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Determine Gross wage using class

    I am trying to make a program that computes gross by using a class and three functions. When I try to run it, it gives me this error I think something is wrong with my set function and my int main function, any help would be appreciated.

    Code:
    #include <iostream>
    using namespace std;
    
    double payRate;
    int hours;
    
    class Employee
    {
     public:
     void set(double payRate, int hours);
     double pay();
     void display();
    
     private:
     int hours;
     double payRate;
     double Gross;
    };
    
    int main()
    {
     Employee wage;
     cout << "What is the pay rate:";
     cin >> payRate;
     cout << "How many hours worked:";
     cin >> hours;
     wage.set(payRate, hours);
     wage.display();
    }
    
    void Employee::set(double payRate, int hours)
    {
     cin >> payRate;
     cin >> hours;
    }
    
    double Employee::pay()
    {
     return Gross= payRate* hours;
    }
    
    void Employee::display()
    {
     cout << "The Gross wage is: " << Gross;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    >> When I try to run it, it gives me this error

    What error? And why are you reading the input twice (once from main and then from set())?

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Code:
    #include <iostream>
    using namespace std;
    
    double payRate;
    int hours;
    
    class Employee
    {
     public:
     void set(double payRate, int hours);
     double pay();
     void display();
    
     private:
     int hours;
     double payRate;
     double Gross;
    };
    
    int main()
    {
     Employee wage;
     cout << "What is the pay rate:";
     cin >> payRate;
     cout << "How many hours worked:";
     cin >> hours;
     wage.set(payRate, hours);
     wage.display();
    }
    
    void Employee::set(double payRate, int hours)
    {
     cin >> payRate;
     cin >> hours;
    }
    
    double Employee::pay()
    {
     return Gross= payRate* hours;
    }
    
    void Employee::display()
    {
     cout << "The Gross wage is: " << Gross;
    }
    Why do you read in "payRate" and "hours" in main and again in the "set" function? You don't need to read those values in main; just use the set function.
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    not to mention you can't just use them without initializing them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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