• 0

[C++] Help with my code


Question

Hi guys, I've been working on this for a few hours and I'm getting really exhausted and annoyed. My goal is have the user insert the time. I don't know how to manipulate the time.h functions yet so I'm having the time inserted manually. From there I have the time shown to the user then have it incremented by 1 hour. Then output another line with only 1 minute incremented.

From there I just set the timezone in my code and output that via a variable as you can see, then just tell the user the current time zone is +8. I also re-output the time.

I am having trouble accessing the private members of my Time class in my main function. As far as I know I've down everything the way it should be done but its just not working for me.

Any help would really be accepted. Also I know my coding is sloppy and most likely incorrent but I'm learning its its only my 4th week.

So any input on coding styles is a bonus ;)

thx

#include "stdafx.h"
#include <iostream></P> <P>using namespace std;</P> <P>class Time
{
private:
 int hour;
 int minute;
 int zero;
public:
 void setTime(int,int,int);
 void returnZero();
 int returnTime();
 void printTime();
 void incrementHour();
 void incrementMinute();
};
class extTime: public Time
 {
 int time_zone;
 public:
  void setTimezone(int);
  int getTimezone();
  void printTimezone();
 };
void Time::setTime(int hr, int min, int z)
 {
  hour = hr;
  minute = min;
  zero = z;
 }
int Time::returnTime()
 {
 return hour;
 return minute;
 return zero;
 }
void Time::printTime()
 {
  do
   {
   cout << "Hour: ";
   cin >> hour;
   }
  while(hour > 12);
  do
   {
   cout << "Minute: ";
   cin >> minute;
   }
  while(minute > 59);
  cout << hour << ":" << &Time::returnZero << minute << endl << endl;
 }
void Time::incrementHour()
 {
  cout << "Incremented by 1 hour" << endl;
  if(hour == 12)
   {
   hour = 0;
   hour++;
   cout << hour;
   }
  else
   {
   hour++;
   cout << hour;
   }
 }
void Time::incrementMinute()
 {
  cout << "Incremented by 1 minute" << endl;
  if(minute == 59 && hour == 12)
   {
   hour = 12;
   minute = 00;
   cout << hour << ":" << minute << endl << endl;
   }
  else
   if(minute < 59)
   {
   minute++;
   cout << minute;
   }
 }
void Time::returnZero()
 {
 if(minute > 10)
  cout << "";
 else
  {
  cout << 0;
  }
 }
void extTime::setTimezone(int ext)
 {
 time_zone = ext;
 ext = 8;
 }
int extTime::getTimezone()
 {
 return time_zone;
 }
void extTime::printTimezone()
 {
 cout << "The time was stated as " << &Time::printTime << " and..." << endl;
 cout << "The current time zone is +" << &extTime::getTimezone << "GMT" << endl;
 }
int _tmain(int argc, _TCHAR* argv[])
{
 system("Title Week 3 Lab");
 Time time;
 extTime time_zone;
 cout << "Enter the time in HH:MM format **12H format**" << endl;
 time.printTime();
 cout << time.incrementHour << ":" << time.minute << endl << endl;
 cout << time.hour << ":" << time.incrementMinute;
 time_zone.printTimezone();
 system("pause");
 return 0;
}

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I can't really give you a full reply right now as I'm a bit busy but the problem is that incrementHour and incrementMinute are functions, not members. Also they're both void functions so you can't display anything from them.

You could however do this instead:

	time.incrementHour();
	time.incrementMinute();

I formatted your code a bit too to make it easier to read too. Always make sure you indent your code :)

#include <iostream>

using namespace std;

class Time
{
	private:
		int hour;
		int minute;
		int zero;
	public:
		void setTime(int,int,int);
		void returnZero();
		int returnTime();
		void printTime();
		void incrementHour();
		void incrementMinute();
};

class extTime: public Time
{
	int time_zone;
	public:
		void setTimezone(int);
		int getTimezone();
		void printTimezone();
};

void Time::setTime(int hr, int min, int z)
{
	hour = hr;
	minute = min;
	zero = z;
}

int Time::returnTime()
{
		// Should possibly rename this function to returnHour() ?
	return hour;
}

void Time::printTime()
{
	do
	{
		cout << "Hour: ";
		cin >> hour;
   } while(hour > 12);

	do
	{
		cout << "Minute: ";
		cin >> minute;
	} while(minute > 59);

	cout << hour << ":" << &Time::returnZero << minute << endl << endl;
}

void Time::incrementHour()
{
	cout << "Incremented by 1 hour" << endl;

	if(hour == 12)
	{
		hour = 0;
		hour++;
		cout << hour;
	}
	else
	{
	   hour++;
	   cout << hour;
	}
}

void Time::incrementMinute()
{
	cout << "Incremented by 1 minute" << endl;
	if(minute == 59 && hour == 12)
	{
		hour = 12;
		minute = 00;
		cout << hour << ":" << minute << endl << endl;
	}
	else if(minute < 59)
	{
		minute++;
		cout << minute;
   }
}

void Time::returnZero()
{
	if(minute > 10)
	{
		cout << "";
	}
	else
	{
		cout << "0";
	}
}

void extTime::setTimezone(int ext)
{
	time_zone = ext;
	ext = 8;
}

int extTime::getTimezone()
{
	return time_zone;
}

void extTime::printTimezone()
{
	cout << "The time was stated as " << &Time::printTime << " and..." << endl;
	cout << "The current time zone is +" << &extTime::getTimezone << "GMT" << endl;
}

int main()
{
	system("Title Week 3 Lab");
	Time time;
	extTime time_zone;

	cout << "Enter the time in HH:MM format **12H format**" << endl;

	time.printTime();
	time.incrementHour();
	time.incrementMinute();

	time_zone.printTimezone();

	system("pause");
	return 0;
}

It won't do what you think as you can only return one value from the function. You'll need separate functions for each one. I.e returnHour() returnMinute(), returnZero().

Hope this helps!

ViZioN

P.S Regarding the time.h functions you should have a look here: http://www.cplusplus.com/reference/clibrary/ctime/ It describes the functions and gives some handy example code :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.