SHWOO 25 Junior Poster in Training

I have overloaded the << & >> operators and I though I could access private data members of the class if I declared the functions friends of the class. I am getting four errors in the two overloaded functions that are trying to access the private members firstName and lastName. Anyone have any ideas as to what I am doing wrong?

#ifndef H_personType
#define H_personType

#include <string>
 
using namespace std;

class personType
{
    friend ostream& operator<< (ostream&, const personType &);
    friend istream& operator>> (istream&, personType &);
public:
    void print() const;
       //Function to output the first name and last name
       //in the form firstName lastName.
  
    void setName(string first, string last);
       //Function to set firstName and lastName according 
       //to the parameters.
       //Postcondition: firstName = first; lastName = last

    string getFirstName() const;
       //Function to return the first name.
       //Postcondition: The value of the firstName is returned.

    string getLastName() const;
       //Function to return the last name.
       //Postcondition: The value of the lastName is returned.

    personType(string first = "", string last = "");
       //constructor
       //Sets firstName and lastName according to the parameters.
       //The default values of the parameters are empty strings.
       //Postcondition: firstName = first; lastName = last  

 private:
    string firstName; //variable to store the first name
    string lastName;  //variable to store the last name
};

#endif
#include <iostream>
#include <string>
#include "personType.h"

using namespace std;

ostream& operator<< (ostream& osObject, const personType& name)
{
    osObject << name.firstName << name.lastName;

    return osObject;
}

istream& operator>> (istream& isObject, personType& name)
{
    isObject >> name.firstName >> …
SHWOO 25 Junior Poster in Training

I cannot figure out why I am getting these two errors

error C2504: 'arrayListType' : base class undefined
error C2143: syntax error : missing ',' before '<'

// Jon Wayman
// class that is derived from arrayListType


template <class elemType>
class unorderedArrayListType: public arrayListType<elemType>
{
public:
    void insertAt(int location, const elemType& inserItem);
    void insertEnd(const elemType& insertItem);
    void replaceAt(int location, const elemType& repItem);
    int seqSearch(const elemType& searchItem) const;
    void remove(const elemType& removeItem);

    unorderedArrayListType(int size = 100);
        //constructor 

};

template <class elemType>
void unorderedArrayListType<elemType>::insertEnd(const elemType &insertItem)
{
    if (length >= maxSize)    //the list is full
        cout << "Cannot insert in a full list." << endl;
    else 
    {
        list[length] = insertItem; //insert the item at the end
        length++;    //increment the length
    }
} //end insert End

template <class elemType>
int unorderedArrayListType<elemType>::seqSearch(const elemType& searchItem) const
{
    int loc;
    bool found = false;

    for (loc = 0; loc < length; loc++)
        if (list[loc] == searchItem)
        {
            found = true;
            break;
        }

    if (found)
        return loc;
    else 
        return -1;
} // end Seqsearch

template <class elemType>
void unorderedArrayListType<elemType>::remove(const elemType &removeItem)
{
    int loc;

    if (length == 0)
        cout << "Cannot delete from an empty list." << endl;
    else
    {
        loc = seqSearch(removeItem);
         
        if (loc != -1)
            removeAt(loc);
        else
            cout << "The item to be deleted is not in the list." << endl;
    }
} //end remove

template <class elemType>
void unorderedArrayListType<elemType>::replaceAt(int location, const elemType &repItem)
{
    if (location < 0 || location >= length)
        cout << "The location of the item to be …
SHWOO 25 Junior Poster in Training

So I went back and looked at my first algorithm and I had in the if statement (list >
and it should have been (list[j] >

I like your algorithm because it is a little simpler and more clear. Thanks again for the help.

Reply to ya later.

SHWOO 25 Junior Poster in Training

C++ Programming: Program Design Including Data Structures 3rd ed. by D.S. Malik

I am going to copy the algorithm again maybe I screwed it up.

void bubbleSort(elemType list[], int length)
{
       for (int i = 1; i < length; i++)
       {
              for (int j = 0; j < length - i; j++)
              {
                     if (list[j] > list[j + 1])
                     {
                             elemType temp = list[j];
                             list[j] = list[j + 1];
                             list[j + 1] = temp;
                     }
              }
       }
}
SHWOO 25 Junior Poster in Training

Thanks for the help. I just deleted the function with the two parameters. I got the code for that bubble sort algorithm straight out of the book so I am surprised it was flawed, but you were right cause when I compiled I get an error then I ran it your way and it worked great. Thanks again.

SHWOO 25 Junior Poster in Training

The assignment is to write a C++ function using "pointer notation" that will write out the elements of an array of int in reverse order.

The problem I'm having is with the sortAscend function and what the parameters should be to accept an array. I'm getting an error cause I'm passing an object and the function expects an int pointer.

class arrayList
{
public:
    void print() const;
        //Function to output the elements of the list
        //Postcondition: Elements of the list are output
        //                 on the standard output device.

    void sortAscend(int *list, int length);
        //Function to sort the array in Ascending order
        //Postcondition: Elements of the list are sorted 
        //                 in Ascending order

    void insertEnd(int insert);
        //Function to insert elements at the end of list
        //Postcondition: list[length] = insert; length++;

    arrayList(int size = 10);
        //constructor
        //Creates an array of the size specified by the
        //parameter size. The default size is 10.
        //Postcondition: The list points to the array,
        //                 length = 0, and maxSize = size;

    ~arrayList();
        //destructor
        //Deallocate the memory occupied by the array.

private:
    int *list;        //array to hold the list elements
    int length;        //variable to store the length of the list
    int maxSize;    //variable to store the maximum size of the list
};
#include "arrayList.h"

using namespace std;

void arrayList::print() const
{
    for (int i = 0; i < length; i++)
        cout << list[i] << " ";
    cout << endl;
} //end print function

void arrayList::sortAscend(int *list, int length)
{
    for(int i = 0; i …
SHWOO 25 Junior Poster in Training

I am working on a struct. The two requirements are for the user to be able to input an entry and then view it. I have two questions, when entering the data shouldn't I be able to enter spaces when entering the address i.e. 654 smith st and store that as address1? and why am i getting a memory addres for my print function?
it displays

,
-654649840

#include <string>
#include <iostream>

using namespace std;

struct playerType
{
    string first;
    string last;
    string address1;
    string address2;
    string city;
    string state;
    string zip;
    int jersey;
}; //end struct

void readIn(playerType player)
{
    cin >> player.first >> player.last;
    cin    >> player.address1 >> player.address2;
    cin    >> player.city >> player.state;
    cin    >> player.zip >> player.jersey;
}

void printPlayer(playerType player)
{
    cout << endl << endl << player.first << " " << player.last 
        << endl << player.address1 << endl << player.address2
        << endl << player.city << ", " << player.state
        << " " << player.zip << endl << player.jersey << endl;
}


int main()
{
    playerType player;

    cout << "Enter all of the player's information in order shown\n"
        "pressing Enter after each entry. First name, last name,\n"
        "address line 1, address line 2, city, state, zip, and jersey number: ";

    readIn(player);
    printPlayer();

    
} //end main
SHWOO 25 Junior Poster in Training

Those three loops have been fixed and I have instantiated two objects with different values for hours in the array yet, both calls to biWeeklySalary with two different objects show the same -9....e+061 value

SHWOO 25 Junior Poster in Training

I fixed the loop which stepped of the array and am still getting -9.25596e+061 for all calculation of biWeeklySalary

SHWOO 25 Junior Poster in Training

Can anyone figure out why I get -9.032654e something from my biWeeklySalary function?

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include "HireDate.h"

#include <string>
using std::string;

class Employee
{
public:
    Employee();
    Employee( const string &, const string &, const string &, const HireDate & );
    ~Employee();

    void setFirstName( const string & );
    string getFirstName() const; 

    void setLastName( const string & );
    string getLastName() const;

    void setSocialSecurityNumber( const string & );
    string getSocialSecurityNumber() const;

    void print() const;

private:
    string firstName;
    string lastName;
    string socialSecurityNumber;
    const HireDate hireDate;
};

#endif
#include <iostream>
using std::cout;
using std::endl;

#include "Employee.h"
#include "HireDate.h"
#include "MailCarrier.h"

//constructor
Employee::Employee()
{

}

//Constructor 
Employee::Employee(const string &first, const string &last, const string &ssn, const HireDate &dateOfHire )
    : firstName( first ), lastName( last ), socialSecurityNumber( ssn ), hireDate( dateOfHire )
{

}

Employee::~Employee()       //destructor
{

}

//function to set Employee's first name
void Employee::setFirstName( const string &first )
{
    firstName = first;
}

//function to get Employee's first name
string Employee::getFirstName() const
{
    return firstName;
}

//function to set Employee's last name
void Employee::setLastName( const string &last )
{
    lastName = last;
}

//function to get Employee's last name
string Employee::getLastName() const
{
    return lastName;
}

// function to set Employee's Social Security Number
void Employee::setSocialSecurityNumber( const string &ssn )
{
    socialSecurityNumber = ssn;
}

//function to get Employee's Social Security Number
string Employee::getSocialSecurityNumber() const
{
    return socialSecurityNumber;
}

//function to print Employee object
void Employee::print() const
{
    cout << "Employee: " << getFirstName() << ' ' << getLastName()
        << …
SHWOO 25 Junior Poster in Training

Hi, I am a newbie so am not really sure but I have a suggestion.

1. Return an array from get getHoursFromUser()
2. Take a new array, say new_array and initialize it using the returned value from getHoursFromUser().
3. Pass that new_array in your function that calculates the salary.

If that is wrong can you comment on that please. Thank you .

Well someone suggested that I don't need to pass any arguments to the function because the array already has the values that are needed from the getHoursFromUser function. So I changed the biWeeklySalary function to receive no arguments but, the algorithm or something is messed up somewhere because the output of the function with 80 hours total is 1 and it should come out to 1561.60

SHWOO 25 Junior Poster in Training

I appreciate the help guys but I can't seem to figure it out. here is what I have changed.

// prototype
double biWeeklySalary( double [], int ); 

// definition
double MailCarrier::biWeeklySalary( double hours[], int size )

private:

double hours[ 12 ];

bob.biWeeklySalary( hours, 12 ) returns an error where hours is undeclared

SHWOO 25 Junior Poster in Training

and I had bob.biWeeklySalary( hours )
also tried bob.biWeeklySalary ( hours[] )
bob.biWeeklySalary( *hours )

None worked

double MailCarrier::biWeeklySalary( double hours[] )
{
    double pay = 0;

    for (int k = 0; k <= 11; k++)
    {
    if ( hours[ k ] > 10)
        pay = ((payRate * 2 * ( hours[ k ] - 10)) + (payRate * 1.5 * 2) + (payRate * 8));
    else if ( hours[ k ] > 8)
        pay = ((payRate * 1.5 * (*hours[ k ] - 8)) + ( payRate * 8));
    else if ( hours[ k ] >= 0)
        pay = payRate * 8;
    else cout << "Hours entered incorrectly, Please try again!\n" << endl;
    totalPay += pay;
    }
    
    return totalPay;
}
SHWOO 25 Junior Poster in Training

I have instantiated an object, bob, and have an array with hours for each day bob has worked. When I try to call bob.biWeeklySalary( hours[] ) it doesn't work. I'm not really sure how to use the array here. The compiler just says that hours is an undeclared identifier.

Your help is very appreciated

#include <iostream>
using std::cout;
using std::endl;


#include "MailCarrier.h"
#include "HireDate.h"
#include "Employee.h"

int main()
{
    HireDate bobHire( 4, 8, 2004 );

    MailCarrier bob( "Bob", "Johnson", "534-44-9551", bobHire, 19.52 );

    bob.getHoursFromUser();

    bob.biWeeklySalary(  );

    bob.Employee::print();
}
#ifndef MAILCARRIER_H
#define MAILCARRIER_H

#include "Employee.h"
#include "HireDate.h"

#include <iostream>
using std::cout;
using std::endl;

class MailCarrier : public Employee
{
public:
    MailCarrier();
    MailCarrier( const string &, const string &, const string &, const HireDate &, double );

    void setPayRate( double );
    double getPayRate() const;

    void getHoursFromUser();

    double biWeeklySalary( double );

private:
    double payRate;
    double hours[12];
    double totalPay;

};
#endif
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "MailCarrier.h"

//Constructor
MailCarrier::MailCarrier()
{
    for (int i = 0; i <= 12; i++)
        hours[i] = 0;
}

//Constructor
MailCarrier::MailCarrier( const string &first, const string &last, const string &ssn,
                        const HireDate &, double pay )
    //explicitly call base-class constructor
    : Employee( first, last, ssn, HireDate( ) )
{
    setPayRate( pay ); // validate and store payRate
}

// function to set payRate
void MailCarrier::setPayRate( double pay )
{
    payRate = ( pay > 0.0 && pay < 35.0 ) ? pay : 17.17;
}

double MailCarrier::getPayRate() const
{
    return payRate;
} …
SHWOO 25 Junior Poster in Training

I am getting this error in the second constructor for Class MailCarrier.

mailcarrier.cpp(11) : error C2512: 'Employee' : no appropriate default constructor available
I'm trying to incorporate composition and inheritance into my program.
:idea:Any help would be appreciated:idea:

//MailCarrier.h


#ifndef MAILCARRIER_H
#define MAILCARRIER_H

#include "Employee.h"

#include <iostream>
using std::cout;
using std::endl;

class MailCarrier : public Employee
{
public:
    MailCarrier();
    MailCarrier( const string &, const string &, const string &, const HireDate &, double );

    void setPayRate( double );
    double getPayRate() const;

    void getHoursFromUser();

    double biWeeklySalary( double );

private:
    double payRate;
    double hours[12];
    double totalPay;
};
#endif
//MailCarrier.cpp
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "MailCarrier.h"

//Constructor
MailCarrier::MailCarrier()
{
    for (int i = 0; i <= 12; i++)
        hours[i] = 0;
}

//Constructor
MailCarrier::MailCarrier( const string &first, const string &last, const string &ssn,
                        const HireDate &, double pay )
    //explicitly call base-class constructor
    : Employee( first, last, ssn, HireDate  )
{
    setPayRate( pay ); // validate and store payRate
}

// function to set payRate
void MailCarrier::setPayRate( double pay )
{
    payRate = ( pay > 0.0 && pay < 35.0 ) ? pay : 17.17;
}

double MailCarrier::getPayRate() const
{
    return payRate;
}

//Function to get hours from user
void MailCarrier::getHoursFromUser()
{
    double dailyHours;

    for (int j = 0; j <= 11; j++)
    {
        cout << "Enter hours for day " << j+1 << ": ";
        cin >> hours[ j ];
    }
}//End function
//Employee.h


#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include "HireDate.h"

#include …
SHWOO 25 Junior Poster in Training

You obviously didn't look hard enough.

These are basic duration examples.

I need help using my mm/dd/yyyy, which is three int variables and getting a length of employment with the company so I can calculate the amount of vacation and sick leave.

The ctime library uses seconds since jan 1, 1970 and mine is three int so I don't know how I would do it.

SHWOO 25 Junior Poster in Training

I am writing a program to calculate my paycheck. I am including in this program two functions to calculate sickleave and vacation leave.

I have a hireDate data member in the format of mm/dd/yyyy, how would I figure out length of employment to calculate aquired vacation and sick leave?

I have not used the ctime std lib and couldn't find much help on the net.

Heres what I have so far for my program

#ifndef HIREDATE_H
#define HIREDATE_H

class HireDate 
{
public:
   HireDate( int = 1, int = 1, int = 1900 ); // default constructor
   void print() const; // print date in month/day/year format
   ~HireDate(); // provided to confirm destruction order
private:
   int month; // 1-12 (January-December)
   int day; // 1-31 based on month
   int year; // any year

   // utility function to check if day is proper for month and year
   int checkDay( int ) const; 
}; // end class Date

#endif
#include <iostream>
using std::cout;
using std::endl;

#include "HireDate.h" // include Date class definition

// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
HireDate::HireDate( int mn, int dy, int yr )
{
   if ( mn > 0 && mn <= 12 ) // validate the month
      month = mn;
   else 
   {                     
      month = 1; // invalid month set to 1
      cout << "Invalid month (" << mn << ") set to 1.\n";
   } // end else

   year = yr; // could validate yr …
SHWOO 25 Junior Poster in Training

Shouldn't your second for loop be like this?

I am a novice programmer and I didn't think of that. Is it better or more common to do it that way?

I just skipped the zeroeth subscript so the first workday would be 1

SHWOO 25 Junior Poster in Training

I'm trying to create the code for a paycheck program where I am paid double time for anything over 10 hours, time and a half for 8-10 hours and straight time for 0-8 hours. For some reason the pay isn't accumulating correctly. Any help would be appreciated.

#include <iostream>
using std::cout;
using std::cin;

#include <iomanip>
using std::endl;


int main()
{
    double Array[13];
    double pay = 0;
    double payRate = 19.52;

    for (int i = 0; i <= 12; i++)
        Array[i] = 0;

    for (int j = 1; j <= 12; j++)
    {
        cout << "Enter hours for day " << j << endl;
        cin >> Array[j];
    }

    for (int k = 1; k <= 12; k++)
    {
    if (Array[ k ] > 10)
        pay = ((payRate * 2 * (Array[ k ] - 10)) + (payRate * 1.5 * 2) + (payRate * 8));
    else if (Array[ k ] > 8)
        pay = ((payRate * 1.5 * (Array[ k ] - 8)) + ( payRate * 8));
    else if (Array[ k ] >= 0)
        pay = payRate * 8;
    else cout << "Hours entered incorrectly, Please try again!\n" << endl;
    pay += pay;
    }
    cout << pay;
    return 0;
}
SHWOO 25 Junior Poster in Training

Salem was right it was the obscure code but I copied from my textbook and just tried to morph it into my program. I guess I don't really understand what the previous code was trying to do, any ideas?

in the book it has a funciton defined as

void CommissionEmployee::setGrossSales( double sales )
{
      grossSales = ( sales < 0.0 ) ? 0.0 : sales;
}
SHWOO 25 Junior Poster in Training

I have instantiated an object with arguments( string, float, float)

The two floating numbers are initialized using the constructor but when I display my object using my print function instead of getting the two floating numbers I entered, I get 0.00 for each.

#ifndef Publication_H
#define Publication_H

#include <string>
using std::string;

class Publication
{
public:
    Publication( const string &, float = 0.00 );

    void setBookTitle( const string & ); //function to set book's title
    string getBookTitle() const; // function to retrieve book's title

    void setBookPrice( const float  ); // function to set book's price
    float getBookPrice() const; // function to retrieve book's price

    void print() const;

private:
    string bookTitle;
    float bookPrice;
};

#endif
#include <iostream>
using std::cout;
using std::endl;

#include "Publication.h"

Publication::Publication( const string &title, float price)
: bookTitle( title )
{
    setBookPrice( price );
}

void Publication::setBookTitle(const string &title )
{
    bookTitle = title;
}

string Publication::getBookTitle() const
{
    return bookTitle;
}

void Publication::setBookPrice( const float price )
{
    bookPrice = ( price > 0.00 ) ? 0.00 : price;
}

float Publication::getBookPrice() const
{
    return bookPrice;
}

void Publication::print() const
{
    cout << "Wayman Publishing\n Books in Circulation:"
        << "\nTitle: " << getBookTitle() << "\nPrice: "<< "$" << getBookPrice() << endl;
}

Notice here I instantiate Tape audio( "Lord of the Rings", 19.95, 246.50 ); but the 19.95 and 246.50 display 0.00

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;

#include <iomanip>
using std::setprecision;

#include "book.h"
#include "tape.h"

int main()
{
    Tape …
SHWOO 25 Junior Poster in Training

So if I am to overload this operator two different ways is that going to have two different definitions?

somephonenumber.operator<<( cout ); or as

somePhoneNumber << cout

SHWOO 25 Junior Poster in Training

I am learning overloaded operators, I successfully overloaded the <<and >> operators as friend functions creating cout << object and cin >> object. For the next problem I am required to overload the istream operator two ways. I must invoke it as
somephonenumber.operator<<( cout ); or as
somePhoneNumber << cout; I am a little confused on the exact code.

Visual Studio indicates 2 Errors

phonenumber.cpp(25) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::ostream' (or there is no acceptable conversion)

phonenumber.h(17): could be 'std::string PhoneNumber::operator <<(const PhoneNumber)'
while trying to match the argument list '(PhoneNumber, std::ostream)'Fig11_05.cpp

fig11_05.cpp(13) : error C2511: 'std::string PhoneNumber::operator <<(const PhoneNumber &)' : overloaded member function not found in 'PhoneNumber'

phonenumber.h(14) : see declaration of 'PhoneNumber'

// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber.
#include <iomanip>
using std::setw;

#include "PhoneNumber.h"


string PhoneNumber::operator<<( const PhoneNumber &right)
{
output << "(" << number.areaCode << ") "
    << number.exchange << "-" << number.line;
        return output; // enables cout << a << b << c;
} // end function operator<<


istream &operator>>( istream &input, PhoneNumber &number )
{
   input.ignore(); // skip (
   input >> setw( 3 ) >> number.areaCode; // input area code
   input.ignore( 2 ); // skip ) and space
   input >> setw( 3 ) >> number.exchange; // input exchange
   input.ignore(); // skip dash (-)
   input >> setw( 4 ) >> number.line; // input line
   return input; // enables …