12 Replies - 829 Views - Last Post: 23 November 2009 - 12:41 AM Rate Topic: -----

#1 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

C++ Help Needed

Posted 22 November 2009 - 09:51 PM

Need some help with this program


// Week3.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class Date
{
public:
	Date();
	Date(int m, int d, int y);
private:
	int month;
	int day;
	int year;

};


class Time
{
public:
	Time(int hour, int minute);
	Time();
	void setHour(int);
	void setMinute(int);
	int getHour();
	int getMinute();
	void incrHour(int);
	void incrMinute(int);
	void printTime();
	
private:
	int hr, min;
};

class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData();
private:
	string eventName;
	Time eventTime;
	Date eventDate;
	
};

Date::Date()
{
	
}

Event::Event()
{

}

Event::Event(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay): eventTime(eventHour, eventMinute), eventDate(eventDay, eventMonth, eventYear)
{
	eventName = eventName2;
	
}



void Event::printEventData() 
{
	cout << endl << eventName << " Happens on " << eventDate; 
	cout << " at " << eventTime << endl;
	
}


Date::Date(int m, int d, int y)
{
	year = y;
	month = m;
	day = d;

}

class extTime : public Time
{
public:
	extTime();
	extTime(int hr, int min, string tz);
	string getTimezone();
	void setTimezone(string);
	void printTime();
private:
	string tz;
};

Time::Time() 
{ 
	hr = min = 0; 
}

Time::Time(int hour, int minute) 
{ 
	hr = hour; 
	min = minute; 
}

void Time::setHour(int hour) 
{ 
	hr = hour; 
}

void Time::setMinute(int minute) 
{ 
	min = minute; 
}

int Time::getHour() 
{ 
	return hr; 
}

int Time::getMinute() 
{ 
	return min; 
}

void Time::incrHour(int addHr) 
{ 
	// Add addHr to hr.  We perform %24 in case the new value is > 23.
	hr = (hr + addHr)%24;

	// If addHr is negative, there is a chance that tne new value for hr will be negative.
	// If so, we want to turn it back into a valid positive value.
	if (hr < 0) hr += 24; 
}

void Time::incrMinute(int addMin) 
{ 
	// Add addMin to min.  We perform %60 in case the new value is > 59.
	min = (min + addMin)%60; 

	// If addMin is negative, there is a chance that the new value for min will be negative.
	// If so, we wan to turn it back into a valid positive value.
	if (min < 0) min += 60;
}

void Time::printTime() { cout << getHour() << ":" << getMinute(); }

extTime::extTime() { tz = "EST"; }

extTime::extTime(int hr, int min, string timezone)
	: Time(hr, min)
{
	tz = timezone;
}

void extTime::setTimezone(string timezone) { tz = timezone; }

string extTime::getTimezone() { return tz; }

void extTime::printTime()
{
	cout << getHour() << ":" << getMinute() << " " << getTimezone();
}


int main()
{
	Event ev("New Year", 1,0,2009,1,1);
	Event userEvent;
	userEvent.printEventData();


	system("PAUSE");

	return 0;
}




Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Date' (or there is no acceptable conversion) c:\users\john\documents\visual studio 2005\projects\ilabwk4\finkell_john_wk4\finkell_john_wk4.cpp 75
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Time' (or there is no acceptable conversion) c:\users\john\documents\visual studio 2005\projects\ilabwk4\finkell_john_wk4\finkell_john_wk4.cpp 76

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Help Needed

#2 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: C++ Help Needed

Posted 22 November 2009 - 10:05 PM

That error is telling you that cout has no overloaded operator that accepts an object of type 'Date'. You'll need to cast it, or use accesor functions like:

eventDate.getDay();


Hope I helped :D
Was This Post Helpful? 1
  • +
  • -

#3 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: C++ Help Needed

Posted 22 November 2009 - 10:11 PM

this prob wont solve your problem but it might be a good idea to set up a constructor to initialize your 'date' vars so that they won't be junk or null when an instance of the class is created
Was This Post Helpful? 0
  • +
  • -

#4 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 22 November 2009 - 10:20 PM

View Postxtreampb, on 22 Nov, 2009 - 09:11 PM, said:

this prob wont solve your problem but it might be a good idea to set up a constructor to initialize your 'date' vars so that they won't be junk or null when an instance of the class is created


you are correct the information is not passing right and junk is being passed.

the output is to be

New Year's Day Happens on 01/01/2009 at 00:01

so there is more problems than just the errors i was getting
Was This Post Helpful? 0
  • +
  • -

#5 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 22 November 2009 - 10:31 PM

View Postjfinkell, on 22 Nov, 2009 - 09:20 PM, said:

View Postxtreampb, on 22 Nov, 2009 - 09:11 PM, said:

this prob wont solve your problem but it might be a good idea to set up a constructor to initialize your 'date' vars so that they won't be junk or null when an instance of the class is created


you are correct the information is not passing right and junk is being passed.

the output is to be

New Year's Day Happens on 01/01/2009 at 00:01

so there is more problems than just the errors i was getting



Ok removed the junk from last weeks program and just using what is needed for this weeks.


// Week4.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class Date
{
public:
	Date();
	Date(int m, int d, int y);
//private: // removed due to error 
	int month;
	int day;
	int year;

};


class Time
{
public:
	Time(int hour, int minute);
	Time();
	
	
//private: // removed due to error 
	int hr, min;
};

class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData();
//private: // removed due to error 
	string eventName;
	Time eventTime;
	Date eventDate;
	
};

Date::Date()
{
	
}

Event::Event()
{

}

Event::Event(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay): eventTime(eventHour, eventMinute), eventDate(eventDay, eventMonth, eventYear)
{
	eventName = eventName2;
	
}



void Event::printEventData() 
{
	cout << endl << Event::eventName << " Happens on " << eventDate.day; 
	cout << " at " << eventTime.hr << endl;
	
}


Date::Date(int m, int d, int y)
{
	year = y;
	month = m;
	day = d;

}



Time::Time() 
{ 
	hr = min = 0; 
}

Time::Time(int hour, int minute) 
{ 
	hr = hour; 
	min = minute; 
}





int main()
{
	Event ev("New Year", 1,0,2009,1,1);
	Event userEvent;
	userEvent.printEventData();
	


	system("PAUSE");

	return 0;
}




Informaion is not passing right so that is wrong but not sure why.
Was This Post Helpful? 0
  • +
  • -

#6 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: C++ Help Needed

Posted 22 November 2009 - 10:37 PM

well your constructors aren't doing anything
date::date()
{
this->month=1;
this->day=1;
this->year=0;
}



now with the constructor that you have, do the basic same thing but use the vars that are being passed to it.
Was This Post Helpful? 1
  • +
  • -

#7 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 22 November 2009 - 10:37 PM

View Postjfinkell, on 22 Nov, 2009 - 09:31 PM, said:

View Postjfinkell, on 22 Nov, 2009 - 09:20 PM, said:

View Postxtreampb, on 22 Nov, 2009 - 09:11 PM, said:

this prob wont solve your problem but it might be a good idea to set up a constructor to initialize your 'date' vars so that they won't be junk or null when an instance of the class is created


you are correct the information is not passing right and junk is being passed.

the output is to be

New Year's Day Happens on 01/01/2009 at 00:01

so there is more problems than just the errors i was getting



Ok removed the junk from last weeks program and just using what is needed for this weeks.


// Week4.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class Date
{
public:
	Date();
	Date(int m, int d, int y);
//private: // removed due to error 
	int month;
	int day;
	int year;

};


class Time
{
public:
	Time(int hour, int minute);
	Time();
	
	
//private: // removed due to error 
	int hr, min;
};

class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData();
//private: // removed due to error 
	string eventName;
	Time eventTime;
	Date eventDate;
	
};

Date::Date()
{
	
}

Event::Event()
{

}

Event::Event(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay): eventTime(eventHour, eventMinute), eventDate(eventDay, eventMonth, eventYear)
{
	eventName = eventName2;
	
}



void Event::printEventData() 
{
	cout << endl << Event::eventName << " Happens on " << eventDate.day; 
	cout << " at " << eventTime.hr << endl;
	
}


Date::Date(int m, int d, int y)
{
	year = y;
	month = m;
	day = d;

}



Time::Time() 
{ 
	hr = min = 0; 
}

Time::Time(int hour, int minute) 
{ 
	hr = hour; 
	min = minute; 
}





int main()
{
	Event ev("New Year", 1,0,2009,1,1);
	Event userEvent;
	userEvent.printEventData();
	


	system("PAUSE");

	return 0;
}




Informaion is not passing right so that is wrong but not sure why.


Changed printEventData to


void Event::printEventData() 
{
	cout << endl << eventName << " Happens on " << eventDate.month 
		<< eventDate.day << eventDate.year <<
		" at " << eventTime.hr << eventTime.min << endl;
	
}


Was This Post Helpful? 0
  • +
  • -

#8 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 22 November 2009 - 10:52 PM

Ok the constructors are working however the informaion is still not passing right. I know it has to be something simple that I am missing.


// Week4.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class Date
{
public:
	Date();
	Date(int m, int d, int y);
//private: // removed due to error 
	int month;
	int day;
	int year;

};


class Time
{
public:
	Time(int hour, int minute);
	Time();
	
	
//private: // removed due to error 
	int hr, min;
};

class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData();
//private: // removed due to error 
	string eventName;
	Time eventTime;
	Date eventDate;
	
};

Date::Date()
{
	month = 01;
	day = 01;
	year = 2009;

}

Event::Event()
{

}

Event::Event(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay): eventTime(eventHour, eventMinute), eventDate(eventDay, eventMonth, eventYear)
{
	eventName = eventName2;
	
}



void Event::printEventData() 
{
	cout << endl << eventName << " Happens on " << eventDate.month << "/" 
		<< eventDate.day << "/" << eventDate.year <<
		" at " << eventTime.hr << "0:" << eventTime.min << "0" << endl;
	
}


Date::Date(int m, int d, int y)
{
	year = y;
	month = m;
	day = d;

}



Time::Time() 
{ 
	hr = 0;
	min = 01; 
}

Time::Time(int hour, int minute) 
{ 
	hr = hour; 
	min = minute; 
}





int main()
{
	Event ev("New Year", 1,0,2009,1,1);
	Event userEvent;
	userEvent.printEventData();
	


	system("PAUSE");

	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#9 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: C++ Help Needed

Posted 22 November 2009 - 11:00 PM

you are displaying userEvent and all the constructors aren't set up for the event Make sure that each var in ALL you classes are being initialized in you constructors.
check your event constructor again, there may be more.
Was This Post Helpful? 0
  • +
  • -

#10 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 22 November 2009 - 11:08 PM

View Postxtreampb, on 22 Nov, 2009 - 10:00 PM, said:

you are displaying userEvent and all the constructors aren't set up for the event Make sure that each var in ALL you classes are being initialized in you constructors.
check your event constructor again, there may be more.


Ya I have almost got it right. I am passing some right but not all still working on it I really thank everyone for the help I have gotten it has pointed me in the right direction. changed the following code

Class Event


class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData(string,int,int,int,int,int);
//private: // removed due to error 
	string eventName;
	Time eventTime;
	Date eventDate;
	
};





printEventData


void Event::printEventData(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay) 
{
	cout << endl << eventName2 << " Happens on " << eventDate.month << "/" 
		<< eventDate.day << "/" << eventDate.year <<
		" at " << eventTime.hr << "0:" << eventTime.min << "0" << endl;
	
}




This is the main


int main()
{
	Event userEvent;
	userEvent.printEventData("New Year",1,0,2009,1,1);
	


	system("PAUSE");

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 xtreampb  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 337
  • Joined: 20-June 07

Re: C++ Help Needed

Posted 22 November 2009 - 11:20 PM

in your event::Display your code says
void Event::printEventData(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay) 
{
	cout << endl << eventName2 << " Happens on " << eventDate.month << "/" 
		<< eventDate.day << "/" << eventDate.year <<
		" at " << eventTime.hr << "0:" << eventTime.min << "0" << endl;
	
}



instead of for example eventDat.day i would reccomend this->day

this-> is a pointer. it refers to which ever instance of 'event' called the function. in programming you can't be explicit. this-> allows you to be more dynamic and flexable. see what you get when you change them all in your event class. now just do that in your class, not in you main code. and if you have any more processing code like what you have in your event::display that calls functions and those functions uses class vars, change to this->

This post has been edited by xtreampb: 22 November 2009 - 11:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 23 November 2009 - 12:01 AM

View Postxtreampb, on 22 Nov, 2009 - 10:20 PM, said:

in your event::Display your code says
void Event::printEventData(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay) 
{
	cout << endl << eventName2 << " Happens on " << eventDate.month << "/" 
		<< eventDate.day << "/" << eventDate.year <<
		" at " << eventTime.hr << "0:" << eventTime.min << "0" << endl;
	
}



instead of for example eventDat.day i would reccomend this->day

this-> is a pointer. it refers to which ever instance of 'event' called the function. in programming you can't be explicit. this-> allows you to be more dynamic and flexable. see what you get when you change them all in your event class. now just do that in your class, not in you main code. and if you have any more processing code like what you have in your event::display that calls functions and those functions uses class vars, change to this->



When i try to change this in the Event::printEventData i get an error again of

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Date' (or there is no acceptable conversion) c:\users\john\documents\visual studio 2005\projects\ilabwk4\finkell_john_wk4\finkell_john_wk4.cpp 78


void Event::printEventData(string eventName2, int eventMonth, 
						   int eventDay, int eventYear, int eventHour, 
						   int eventMinute)
{
	cout << endl << eventName2 << " Happens on " << eventMonth << "/" 
		<< eventDay << "/" << eventYear <<
		" at " << eventHour << ":"<< eventMinute << endl;

	cout << endl << this->eventName << " Happens on " << this->eventDate << "/" 
		<< " at " << this->eventTime << endl;
	
}




[quote name='jfinkell' date='22 Nov, 2009 - 10:59 PM' post='843055']
[quote name='xtreampb' post='843035' date='22 Nov, 2009 - 10:20 PM']
in your event::Display your code says

Whole code now looks like this.


// Week4.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class Date
{
public:
	Date();
	Date(int m, int d, int y);
//private: // removed due to error 
	int month;
	int day;
	int year;

};


class Time
{
public:
	Time(int hour, int minute);
	Time();
	
	
//private: // removed due to error 
	int hr, min;
};

class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData(string,int,int,int,int,int);
//private: // removed due to error 
	string eventName;
	Time eventTime;
	Date eventDate;
	
};

Date::Date()
{
	this->month = 01;
	this->day = 01;
	this->year = 2009;

}

Event::Event()
{

}

Event::Event(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay): eventTime(eventHour, eventMinute), eventDate(eventDay, eventMonth, eventYear)
{
	this->eventName = eventName2;
	
}



void Event::printEventData(string eventName2, int eventMonth, 
						   int eventDay, int eventYear, int eventHour, 
						   int eventMinute)
{
	cout << endl << eventName2 << " Happens on " << eventMonth << "/" 
		<< eventDay << "/" << eventYear <<
		" at " << eventHour << ":"<< eventMinute << endl;

// added this to check output should be pullling in data from event. 
	cout << endl << this->eventName << " Happens on " << this->eventDate << "/" 
		<< " at " << this->eventTime << endl;
	
}


Date::Date(int m, int d, int y)
{
	this->year = y;
	this->month = m;
	this->day = d;

}



Time::Time() 
{ 
	this->hr = 0;
	this->min = 1; 
}

Time::Time(int hour, int minute) 
{ 
	this->hr = hour; 
	this->min = minute; 
}





int main()
{
	Event ev("New Year", 1,0,2009,1,1);
	Event userEvent;
	userEvent.printEventData("New Year's ",1,0,2009,0,01);
	userEvent.printEventData("Valentine's Day ",2,14,2010,12,15);
	cout << endl;
	


	system("PAUSE");

	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#13 jfinkell  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 01-November 09

Re: C++ Help Needed

Posted 23 November 2009 - 12:41 AM

[quote name='jfinkell' date='22 Nov, 2009 - 11:01 PM' post='843055']
[quote name='xtreampb' post='843035' date='22 Nov, 2009 - 10:20 PM']
in your event::Display your code says

Ok Not sure just why it will not work the way it needs to so can someone point out where I am mucking it up.

it should take in the data from the Event ev and pass it to the print however it prints out just what the constructor has. not what it needs to.

Now the latter works right but by passed the Event


// Week4.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;



class Date
{
public:
	Date();
	Date(int m, int d, int y);
//private: // removed due to error 
	int month;
	int day;
	int year;

};


class Time
{
public:
	Time(int hour, int minute);
	Time();
	
	
//private: // removed due to error 
	int hr, min;
};

class Event
{
public:
	Event();
	Event(string eventName2, int eventHour, int eventMinute, int eventYear, int eventMonth, int eventDay);
	void printEventData(string,int,int,int,int,int);
	void prEventData();
//private: // removed due to error 
	string eventName;
	Time eventTime;
	Date eventDate;
	
};

Date::Date()
{
	this->month = 01;
	this->day = 01;
	this->year = 2009;

}

Event::Event()
{

}

Event::Event(string eventName2, int eventHour, int eventMinute, 
					int eventYear, int eventMonth, int eventDay): eventTime(eventHour, eventMinute), eventDate(eventDay, eventMonth, eventYear)
{
	this->eventName = eventName2;
	
}


// this print is being used to print out from the print command hard code. in main

void Event::printEventData(string eventName2, int eventMonth, 
						   int eventDay, int eventYear, int eventHour, 
						   int eventMinute)
{
	cout << endl << eventName2 << " Happens on " << eventMonth << "/" 
		<< eventDay << "/" << eventYear <<
		" at " << eventHour << ":"<< eventMinute << endl;

	
}

// this print chould be pulling in data from the Event ev in main However no informaion is passing
void Event::prEventData()
{
	cout << endl << this->eventName << " Happens on " << this->eventDate.month << "/" 
		<< this->eventDate.day << "/" << this->eventDate.year <<
		" at " << this->eventTime.hr << ":"<< this->eventTime.min << endl;
}


Date::Date(int m, int d, int y)
{
	this->year = y;
	this->month = m;
	this->day = d;

}



Time::Time() 
{ 
	this->hr = 0;
	this->min = 1; 
}

Time::Time(int hour, int minute) 
{ 
	this->hr = hour; 
	this->min = minute; 
}





int main()
{
	Event ev("New Year", 1,0,2009,1,1);
	Event prEvent;
	// should be passing data from Event ev to the prEventData

	prEvent.prEventData();

	Event userEvent;
	
	// this prints out right however, this is not the way that it needs to work needs to 
	// pass the information to Event then pass to the print 

	userEvent.printEventData("New Year's ",1,0,2009,0,01);
	userEvent.printEventData("Valentine's Day ",2,14,2010,12,15);
	cout << endl;
	


	system("PAUSE");

	return 0;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1