Ok, I'm going post all of my code, and hopefully someone can tell me where my error is at. I'm guessing I need to do a cin.ignore() somewhere, but I'm not sure where. I've tried it in a couple of places, but nothing has fixed the problem completely.
#include <string>
#include <iostream>
using namespace std ;
class Person
{
public:
Person ( ) ;
Person ( string theName ) ;
Person ( const Person & theObject ) ;
string getName ( ) const ;
Person operator =( const Person & rtSide ) ;
friend istream & operator >>( istream & is , Person & p ) ;
friend ostream & operator <<( ostream & os , const Person & p ) ;
private:
string name ;
} ;
#include "person.h"
Person::Person ( ) : name ( "John Doe" )
{ }
Person::Person ( string theName ) : name ( theName )
{ }
Person::Person ( const Person & theObject )
{
name = theObject.name ;
}
Person Person::operator =( const Person & rtSide )
{
//useless here
if ( name == rtSide.name )
return *this ;
name = rtSide.name ;
return *this ;
}
istream & operator >>( istream & is , Person & p )
{
cout << "Name: " << flush ;
getline ( is , p.name ) ;
return is ;
}
ostream & operator <<( ostream & os , const Person & p )
{
os << "Name: " << p.name << endl ;
return os ;
}
string Person::getName ( ) const
{
return name ;
}
#include <string>
#include <iostream>
using namespace std ;
class Vehicle
{
public:
Vehicle ( ) ;
Vehicle ( string newManufacturer , int newCylinders , Person newOwner ) ;
Vehicle ( const Vehicle & theObject ) ;
Vehicle operator =( const Vehicle & rtSide ) ;
friend istream & operator >>( istream & is , Vehicle & v ) ;
friend ostream & operator <<( ostream & os , const Vehicle & v ) ;
protected:
string manufacturer ;
int cylinders ;
Person owner ;
} ;
class Truck : public Vehicle
{
public:
Truck ( ) ;
Truck( string manf , int cyl , Person newO , double newLoad, int newTowCap, int maxPas, bool kingCab ) ;
Truck operator =( const Truck & rtSide ) ;
friend istream & operator >>( istream & is , Truck & t ) ;
friend ostream & operator <<( ostream & os , const Truck & t ) ;
void setOwner ( ) ;
private:
double loadCap ;
int towingCap ;
int maxPassengers ;
bool kingCab ;
} ;
#include "person.h"
#include "vehicle.h"
Vehicle::Vehicle ( ) : manufacturer ( "No manufacturer set" ) , cylinders ( 0 ) , owner ( Person ( ) )
{ }
Vehicle::Vehicle ( string newManufacturer , int newCylinders , Person newOwner ) : manufacturer ( newManufacturer ) , cylinders ( newCylinders ) , owner ( newOwner )
{ }
Vehicle::Vehicle ( const Vehicle & theObject )
{
manufacturer = theObject.manufacturer ;
cylinders = theObject.cylinders ;
owner = theObject.owner ;
}
Vehicle Vehicle::operator =( const Vehicle & rtSide )
{
//check for self assignment not necessary without the use of pointers or arrays.
manufacturer = rtSide.manufacturer ;
cylinders = rtSide.cylinders ;
owner = rtSide.owner ;
return *this ;
}
istream & operator >>( istream & is , Vehicle & v )
{
cout << "Manufacturer: " << flush ;
is >> v.manufacturer ;
cout << "Number of cylinders: " << flush ;
is >> v.cylinders ;
cout << "Owner: " << flush ;
is >> v.owner ;
return is ;
}
ostream & operator <<( ostream & os , const Vehicle & v )
{
os << "Manufacturer: " << v.manufacturer << endl ;
os << "Number of cylinders: " << v.cylinders << endl ;
os << "Owner: " << v.owner << endl ;
return os ;
}
Truck::Truck ( ) : Vehicle ( ) , loadCap ( 0.0 ) , towingCap ( 0 ) , maxPassengers ( 0 ) , kingCab ( false )
{ }
Truck::Truck( string manf , int cyl , Person newO , double newLoad, int newTowCap, int maxPas, bool kingCab ) : Vehicle ( manf , cyl , newO ) , loadCap ( newLoad ) , towingCap ( newTowCap ) , maxPassengers ( maxPas ) , kingCab ( kingCab )
{ }
Truck Truck::operator = ( const Truck & rtSide )
{
//check for self assignment not necessary without the use of pointers or arrays.
Vehicle::operator = ( rtSide ) ;
loadCap = rtSide.loadCap ;
towingCap = rtSide.towingCap ;
maxPassengers = rtSide.maxPassengers ;
kingCab = rtSide.kingCab ;
return *this ;
}
istream & operator >>( istream & is , Truck & t )
{
if ( t.manufacturer == "No manufacturer set" )
{
cout << "Manufacturer: " << flush ;
is >> t.manufacturer ;
is.ignore ( ) ;
cout << "Number of cylinders: " << flush ;
cout << flush ;
cout << "Owner: " << flush ;
is >> t.owner ;
}
char xCab ;
cout << "Load Capacity (tons): " << flush ;
is >> t.loadCap ;
cout << "Towing Capacity (lbs): " << flush ;
is >> t.towingCap ;
cout << "Maximum Passengers: " << flush ;
is >> t.maxPassengers ;
cout << "King Cab? (y/n): " << flush ;
is >> xCab ;
if ( xCab == 'y' || xCab == 'Y' )
t.kingCab = true ;
else
t.kingCab = false ;
return is ;
}
ostream & operator <<( ostream & os , const Truck & t )
{
os << "Owner: " << t.owner.getName ( ) << endl ;
os << "Manufacturer: " << t.manufacturer << endl ;
os << "Number of cylinders: " << t.cylinders << endl ;
os << "Load Capacity (tons): " << t.loadCap << endl ;
os << "Towing Capacity (lbs): " << t.towingCap << endl ;
os << "Maximum Passengers: " << t.maxPassengers << endl ;
os << "King Cab: " ;
if ( t.kingCab == true )
os << "Yes" << endl ;
else
os << "No" << endl ;
return os ;
}
void Truck::setOwner ( )
{
cout << "New Owner: " << flush ;
cin >> owner ;
cout << "Owner changed to " << owner << ".\n" ;
}
#include "person.h"
#include "vehicle.h"
int main ( )
{
Person caleb ;
Person todd ( "Todd Jones" ) ;
cin >> caleb ;
Truck silverado ;
Truck titan ( "Nissan" , 8 , caleb , 2.3 , 7200 , 6 , true ) ;
cin >> silverado ;
cout << endl ;
cout << silverado ;
cout << titan ;
Truck prototype ( silverado ) ;
prototype.setOwner ( ) ;
cout << prototype ;
return 0 ;
}
Sample output:
Name: Caleb
Manufacturer: Chevy
Number of cylinders: Owner: Name:
Another sample (without the cin.ignore() at line 179):
Name: Caleb
Manufacturer: Chevy
Number of cylinders: Owner: Name: Load Capacity (tons):
:(