Hi guys. I am hoping for a little help with this. I am pretty new to programming and just can't seem to get things right yet. Here is my code. It returns like 40 errors. Way messed up eh?

#include <iostream>
#include <string>

using namespace std;

class Date
{
public:
	Date();	//default constructor
	Date(int month, int day, int year);	//constructor with parameters
	void setMonth(int);
	void setDay(int);
	void setYear(int);
	int getMonth();
	int getDay();
	int getYear();
	void printDate();
private:
	int month;
	int day;
	int year;
};

class Time
{
public:
	Time(); //constructor
	Time(int, int);	//constructor with parameters
	void setHour(int);
	void setMinute(int);
	int getHour();
	int getMinute();
	void printTime();
private:
	int hr;
	int min;
};

class Event
{
public:
	Event(); //constructor
	Event(string, int, int, int, int, int);	//constructor with parameters
	string getName();
	void printName();
	void printDate();
	void printTime();
private:
	string EventName;
	Time EventTime;
	Date EventDate;
};

Date::Date()
{
	month = 03;
	day = 17;
	year = 2011;
}

Date::Date(int mm, int dd, int yyyy)
{
	month = mm;
	day = dd;
	year = yyyy;
}

void Date::setMonth(int mm)
{
	if(03 <= mm && mm <= 12)
		mm = month;
	else
		month = 03;
}

void Date::setDay(int dd)
{
	if(17 <= dd && dd <= 31)
		dd = day;
	else
		day = 17;
}

void Date::setYear(int yyyy)
{
	if(0 <= yyyy && yyyy <= 2020)
		yyyy = year;
	else 
		year = 2011;
}

int	Date::getMonth()
{
	return month;
}

int Date::getDay()
{
	return day;
}

int Date::getYear()
{
	return year;
}

int Date::printDate()
{
	cout << getMonth() << "/" << getDay() << "/" << getYear();
};

Time::Time() //default constructor sets the hour and minute
{
	hour = 0;
	minute = 0;
}

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

void Time::setHour (int hr)
{
	if(0 <= hr && hr < 24)
		hr = hour;
	else
		hour = 0;
}

void Time::setMinute(int min)
{
	if(0 <= min && min < 60)
		min = minute;
	else
		minute = 0;
}

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

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

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

Event::Event()
{

}

Event::Event(string eName, int eHour, int eMinute, int eMonth, int eDay, int eYear)
{
	eventTime.setHour(eHour);
	eventTime.setMinute(eMinute);
	eventDate.setMonth(eMonth);
	eventDate.setDay(eDay);
	eventDate.setYear(eYear);
}

string Event::getName()
{
	return eventName;
}

void Event::printName()
{
	cout << eventName;
}

void Event::printDate()
{
	cout << eventDate.getMonth() << "/" << eventDate.getDay() << "/" << eventDate.getYear();
}

void Event::printTime()
{
	cout << eventTime.getHour() << ":" << eventTime.getMinute();
}

int main()
{
	Event eventOne ("Saint Patricks Day", 06, 00, 03, 17, 2011);
	Event eventTwo ("The Fourth of July", 08, 00, 07, 04, 2011);

	cout << eventOne.printName() << " occurs on " << eventOne.printDate() << "at" << eventOne.printTime() << "." << endl;
	cout << eventTwo.printName() << " occurs on " << eventTwo.printDate() << "at" << eventTwo.printTime() << "." << endl;
	cout << endl;

	return 0;
}

Any help on this would be greatly appreciated.

Hi guys. I am hoping for a little help with this. I am pretty new to programming and just can't seem to get things right yet. Here is my code. It returns like 40 errors. Way messed up eh?

#include <iostream>
#include <string>

using namespace std;

class Date
{
public:
	Date();	//default constructor
	Date(int month, int day, int year);	//constructor with parameters
	void setMonth(int);
	void setDay(int);
	void setYear(int);
	int getMonth();
	int getDay();
	int getYear();
	void printDate();
private:
	int month;
	int day;
	int year;
};

class Time
{
public:
	Time(); //constructor
	Time(int, int);	//constructor with parameters
	void setHour(int);
	void setMinute(int);
	int getHour();
	int getMinute();
	void printTime();
private:
	int hr;
	int min;
};

class Event
{
public:
	Event(); //constructor
	Event(string, int, int, int, int, int);	//constructor with parameters
	string getName();
	void printName();
	void printDate();
	void printTime();
private:
	string EventName;
	Time EventTime;
	Date EventDate;
};

Date::Date()
{
	month = 03;
	day = 17;
	year = 2011;
}

Date::Date(int mm, int dd, int yyyy)
{
	month = mm;
	day = dd;
	year = yyyy;
}

void Date::setMonth(int mm)
{
	if(03 <= mm && mm <= 12)
		mm = month;
	else
		month = 03;
}

void Date::setDay(int dd)
{
	if(17 <= dd && dd <= 31)
		dd = day;
	else
		day = 17;
}

void Date::setYear(int yyyy)
{
	if(0 <= yyyy && yyyy <= 2020)
		yyyy = year;
	else 
		year = 2011;
}

int	Date::getMonth()
{
	return month;
}

int Date::getDay()
{
	return day;
}

int Date::getYear()
{
	return year;
}

int Date::printDate()
{
	cout << getMonth() << "/" << getDay() << "/" << getYear();
};

Time::Time() //default constructor sets the hour and minute
{
	hour = 0;
	minute = 0;
}

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

void Time::setHour (int hr)
{
	if(0 <= hr && hr < 24)
		hr = hour;
	else
		hour = 0;
}

void Time::setMinute(int min)
{
	if(0 <= min && min < 60)
		min = minute;
	else
		minute = 0;
}

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

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

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

Event::Event()
{

}

Event::Event(string eName, int eHour, int eMinute, int eMonth, int eDay, int eYear)
{
	eventTime.setHour(eHour);
	eventTime.setMinute(eMinute);
	eventDate.setMonth(eMonth);
	eventDate.setDay(eDay);
	eventDate.setYear(eYear);
}

string Event::getName()
{
	return eventName;
}

void Event::printName()
{
	cout << eventName;
}

void Event::printDate()
{
	cout << eventDate.getMonth() << "/" << eventDate.getDay() << "/" << eventDate.getYear();
}

void Event::printTime()
{
	cout << eventTime.getHour() << ":" << eventTime.getMinute();
}

int main()
{
	Event eventOne ("Saint Patricks Day", 06, 00, 03, 17, 2011);
	Event eventTwo ("The Fourth of July", 08, 00, 07, 04, 2011);

	cout << eventOne.printName() << " occurs on " << eventOne.printDate() << "at" << eventOne.printTime() << "." << endl;
	cout << eventTwo.printName() << " occurs on " << eventTwo.printDate() << "at" << eventTwo.printTime() << "." << endl;
	cout << endl;

	return 0;
}

Any help on this would be greatly appreciated.

Hello, please refer to a specific part of your code that you don't understand and need help..

Ok the first and most important problem is simple that you have gone about this task in the wrong way. Start with the assumption that the compiler is your friend not your enemy, it is there to help, but if you write your whole program and then try to compile it you will have a very very hard time figuring out what you have done wrong.

So what you should have done it written the smallest compilable program that you can write and compiled, then fix the problems and then add a little more (basically in chunks of approximately one error per compile -- that changes with experience and later you can separate the compiler errors, you can write more, but initially that means no more than one method at a time).

So let us look at your code and figure out some of the types of error you are making.

First off, is that you seem to have no care for the capitalization of variables double avar; is different from double Avar; .
You consistently use random first letter capitalization.

Second you do this:

class A
{ 
   int hr;
   A(int);
};

// THIS IS WRONG:
A::A(int hr)    // THIS mask the class variable
{
   hour=hr;     // hour doesn't even exist anywhere
}

You should have written

A::A(int hour)
{
   hr=hour;
}

Word of caution, you write 06 and that is an octal number, i.e. 08 is illegal because there is no 8 in octal numbers: Thus 08 in the fourth of july is wrong.
Write just 8.

So the first thing is to debug you code: Make a copy of this file, and then strip almost every thing you of it and then compile. I would start with this:

The following code does not compile, but the compiler errors make a lot more sense.

// THIS CODE DOES NOT COMPILE:

#include <iostream>
#include <string>

using namespace std;

class Time
{

public:

  Time(); //constructor
  Time(int, int);	//constructor with parameters

  void printTime();

private:
  int hr;
  int min;
};


Time::Time() //default constructor sets the hour and minute
{
  hr = 0;
  min = 0;
}

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

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

int main() {
  Time A(5,6);
  A.printTime();
  return 0;
}

When you have that working, then move on and add some of the other methods of Time, and then a simplified Date class, and then the Event class.

Edited 6 Years Ago by StuXYZ: n/a

Comments
If only our Professors would walk us through this like you did, you wouldn't have people like me bothering you! (LOL)

hi
in class date you had declared

void printDate();

in the definition you wrote

int Date::printDate()

this should be

void Date::printDate()

also
in your constructor

Time::Time() //default constructor sets the hour and minute
{
	hour = 0;
	minute = 0;
}

which should be

Time::Time() //default constructor sets the hour and minute
{
	hr = 0;
	min = 0;
}

and this

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

should be something like

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

the same problem here

void Time::setHour (int hr)
{
	if(0 <= hr && hr < 24)
		hr = hour;
	else
		hour = 0;
}

can be changed to

void Time::setHour (int hour)
{
	if(0 <= hour && hour < 24)
		hr = hour;
	else
		hr = 0;
}

also here

void Time::setMinute(int minute)
{
	if(0 <= minute && minute < 60)
		min = minute;
	else
		min = 0;
}

and here

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

should be

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

here

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

must be

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

you have a problem here

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

should be

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

this function

Event::Event(string eName, int eHour, int eMinute, int eMonth, int eDay, int eYear)
{
	eventTime.setHour(eHour);
	eventTime.setMinute(eMinute);
	eventDate.setMonth(eMonth);
	eventDate.setDay(eDay);
	eventDate.setYear(eYear);
}

should be

Event::Event(string eName, int eHour, int eMinute, int eMonth, int eDay, int eYear)
{
	EventName =eName;
	EventTime.setHour(eHour);
	EventTime.setMinute(eMinute);
	EventDate.setDay(eDay);
	EventDate.setMonth(eMonth);
	EventDate.setYear(eYear);
	

}

this function

string Event::getName()
{
	return eventName;
}

should be

string Event::getName()
{
	return EventName;
}

and this one

void Event::printName()
{
	cout << eventName;
}

should be

void Event::printName()
{
	cout << EventName;
}

this

void Event::printDate()
{
	cout << eventDate.getMonth() << "/" << eventDate.getDay() << "/" << eventDate.getYear();
}

must be

void Event::printDate()
{
	cout << EventDate.getMonth() << "/" << EventDate.getDay() << "/" << EventDate.getYear();
}

and this

void Event::printTime()
{
	cout << eventTime.getHour() << ":" << eventTime.getMinute();
}

must be

void Event::printTime()
{
	cout << EventTime.getHour() << ":" << EventTime.getMinute();
}

and finally

int main()
{
	Event eventOne ("Saint Patricks Day", 06, 00, 03, 17, 2011);
	Event eventTwo ("The Fourth of July", 08, 00, 07, 04, 2011);
 
	cout << eventOne.printName() << " occurs on " << eventOne.printDate() << "at" << eventOne.printTime() << "." << endl;
	cout << eventTwo.printName() << " occurs on " << eventTwo.printDate() << "at" << eventTwo.printTime() << "." << endl;
	cout << endl;
 
	return 0;
}

must be

int main()
{
	Event eventOne ("Saint Patricks Day", 06, 00, 03, 17, 2011);
	Event eventTwo ("The Fourth of July", 05, 00, 07, 04, 2011);
 
	 eventOne.printName();
	cout<< " occurs on " ;
	eventOne.printDate();
	cout<< "at" ;
	 eventOne.printTime() ;
		 cout<< "." << endl;
	 eventTwo.printName();
	 cout << " occurs on ";
	 eventTwo.printDate() ;
	 cout<< "at" ;
	  eventTwo.printTime();
	  cout<< "." << endl;
	cout << endl;
 
	return 0;
}

NOW:
1. Be careful when you declare variables , when you use them make sure you have written them correctly .
2. You can't use identifier before declaring it.
3. You can't put void function call in cout statement .
$ Please try to go through your errors this will help you fix them

Comments
Same as I told the other person who inputted a lot of info for me, If our professors would walk us through these like you did, we would probably learn a lot more. Thank you.

Thank you to all who have replied to my issue. I got this one fixed but stay tuned, I am sure there will be a lot more.

This question has already been answered. Start a new discussion instead.