Time Zones
Jan 28, 2011 at 4:42am UTC
I have having a problem figuring out the last part of my code. Everything seems to be working great, but then at the end when I ask the user to enter a time zone to be calculated instead of returning the new time, it looks to me like it is returning the memory address of that time(i.e. -858993461:-858993460) Can someone shoe me where I am going wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
#include <iostream>
#include <string>
using namespace std;
class Time
{
protected : //they will be known in 'ExtTime'
int hr, min; //declare hour & min
string zone; //declare time zone
public :
Time() {} //calling function
void setHour(); //sets hours
void setMinute(); //sets mins
int getHour(){return hr;};
int getMinute(){return min;};
void addHour();
void addMinute();
void printTime();
};
//end class Time
class ExtTime : public Time
{
public :
ExtTime(int , int ){};
void timeZone(int , int );
void printTime();
private :
string zone;
};
//end inherited class ExTime from class Time
void Time::setHour()
{
cout << "Enter the hour: " ;
cin >> hr;
}
void Time::setMinute()
{
cout << "Enter the minutes: " ;
cin >> min;
}
void Time::addHour()
{
hr++;
if (hr == 24)
hr = 0;
}
void Time::addMinute()
{
min++;
if (min == 60)
{
hr++;
min = 0;
if (hr == 24)
hr = 0;
}
}
void Time::printTime()
{
cout << "In Eastern Standard Time the time is: " ;
if (hr > 24)
cout << "0" ;
cout << hr << ":" ;
if (min > 59)
cout << "0" ;
cout << min;
}
void ExtTime::timeZone(int , int )
{
cout << "Enter time zone (EST, PST, MST, CST): " ;
cin >> zone;
if (zone == "PST" || zone == "pst" )
cout << "The time is: " << (hr-3) << ":" << getMinute() << endl;
if (zone == "EST" || zone == "est" )
cout << "The time is: " << hr << ":" << getMinute() << endl;
if (zone == "CST" || zone == "cst" )
cout << "The time is: " << (hr-1) << ":" << getMinute() << endl;
if (zone == "MST" || zone =="mst" )
cout << "The time is: " << (hr-2) << ":" << getMinute() << endl;
}
_______________________________________________________________________________
#include <iostream>
#include <string>
#include "timeZone.h"
using namespace std;
void main()
{
Time myTime;
myTime.setHour();
myTime.setMinute();
int h = myTime.getHour();
int m = myTime.getMinute();
cout << "The hour is " << h << " and the minute is " << m << endl;
myTime.printTime();
cout << endl;
myTime.addHour();
cout << "After adding 1 hour\n" ;
myTime.printTime();
cout << endl;
myTime.addMinute();
cout << "After adding 1 minute\n" ;
myTime.printTime();
cout << endl;
ExtTime newTime(h,m);
newTime.timeZone(h, m);
system("pause" );
}
Jan 28, 2011 at 1:17pm UTC
line 128 does not initialize hr and min of your object (ExtTime constructor does nothing). So you get random numbers. Btw. timeZone doesn't use its parameters either
Jan 28, 2011 at 1:41pm UTC
What coder said.
For the constructor, do this:
Change
Time() {} //calling function
to
Time(int h, int m) {hr = h; min =m} //calling function
and
to
ExtTime(int h, int m):Time(h,m){} //There is not supposed to be a ";" here
Regarding the other timeZone: Do NOT mix class functions with input. It's bad style. Instead, use the parameters you pass.
Last edited on Jan 28, 2011 at 1:45pm UTC
Jan 29, 2011 at 10:05pm UTC
Thanks a lot for both your help. I made those changes and a few other to clean it up and it is running perfectly!!!! Once again thanks :-)
Topic archived. No new replies allowed.