Having problems creating pointers to Time

Hello people...why can't I get this program to run...I'm about to lose it...I tried everything...I think?!?...Can someone please help me.....



#include <iostream> // Time.h file

using namespace std;

class Time
{
public:
void set(int h, int m, int s);
void print ();
void increment();
int get_hour();
int get_mins();
int get_secs();
bool Equal(Time t);
bool LessThan(Time t);
bool GreaterThan(Time t);

private:
int hour;
int mins;
int secs;
};

#include "time.h" // Time.cpp file

void Time::set(int h, int m, int s)
{
hour = h;
mins = m;
secs = s;
}
void Time::increment()
{
secs ++;
if (secs == 60)
{ mins ++;
secs = 0;
if ( mins == 60)
{ hour ++;
mins = 0;
}
if ( hour == 24)
hour = 0;
}
}

void Time::print()
{
if (hour < 10) cout << '0';
cout << hour << ":";
if (mins < 10) cout << '0';
cout << mins << ":";
if (secs < 10) cout << '0';
cout << secs << endl;

}
int Time::get_hour()
{
return hour;
}
int Time::get_mins()
{
return mins;
}
int Time::get_secs()
{
return secs;
}

bool Time::Equal(Time t)
{
if ( hour == t.hour && mins == t.mins && secs == t.secs)
return true;
else
return false;
}

bool Time::LessThan(Time t)
{
if(hour < t.hour || hour == t.hour && mins < t.mins || hour == t.hour && mins == t.mins && secs < t.secs)
return true;
else
return false;
}

bool Time::GreaterThan(Time t)
{
if(hour > t.hour || hour == t.hour && mins > t.mins || hour == t.hour && mins == t.mins && secs > t.secs)
return true;
else
return false;
}

#include "time.h" // Main.cpp file

int main()
{
Time begin;
Time diff;
Time end;
Time check_in;


int h, m, s;
begin.set (12,0, 0);
end.set (18, 0, 0);

cout << " enter hour, min, sec\n ";
cin >>h>>m>>s;
check_in.set (h, m, s);

cout << " the time you checked in ";
check_in.print ();

if (check_in.Equal(begin))
cout << "You are right on time. Lets go to lunch !! ";
else if(check_in.LessThan(begin))
{
while(!begin.Equal(check_in))
{
begin.increment();
diff.increment();
h = diff.get_hour();
m = diff.get_mins();
s = diff.get_secs();

cout << " Good Morning you are early. You have to wait "<<h<<":"<<m<<":"<<s<< " for lunch !! ";
}

else if(check_in.GreaterThan(begin))
{
while(!begin.Equal(end.set))
{
begin.increment();
diff.increment();
h = diff.get_hour();
m = diff.get_mins();
s = diff.get_secs();

cout << "Good Afternoon you are late. You have to wait "<<h<<":"<<m<<":"<<s<< " for dinner !! ";
}
return 0;
}
}





Last edited on
Well I compiled this with gcc 4.0.1 and got the following errors:
t.cpp: In function ‘int main()’:
t.cpp:121: error: statement cannot resolve address of overloaded function
t.cpp:122: error: statement cannot resolve address of overloaded function
t.cpp:130: error: expected `}' before ‘else’
t.cpp:130: error: expected `}' before ‘else’
t.cpp: At global scope:
t.cpp:130: error: expected unqualified-id before ‘else’
t.cpp:131: error: expected unqualified-id before ‘{’ token
t.cpp:144: error: expected declaration before ‘}’ token


So let's check out those lines, in order. 121 is this
begin.increment; Well it's pretty obvious that increment is a method and not a member so you just forgot the parentheses: begin.increment();

This is the same problem with line 122.

Line 130 has an else statement, but those only work when they follow an if statement, and the one in line 130 has no if with which it corresponds. I believe you meant else if ( instead of else ( both in line 130 and in line 117 and possibly other places that I didn't catch.

good luck!
Hey Thanks man...I tried it your way and I still ended up with compiler errors...here is what it says:

------ Build started: Project: Time, Configuration: Debug Win32 ------
Compiling...
Main.cpp
..\Main.cpp(37) : error C2181: illegal else without matching if
..\Main.cpp(39) : error C3867: 'Time::set': function call missing argument list; use '&Time::set' to create a pointer to member
..\Main.cpp(39) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Build log was saved at "file://c:\Documents and Settings\Darren\Desktop\Time\Debug\BuildLog.htm"
Time - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1.) You have an else with no if before it
2.) You probably forgot some more () after someTime.set()
this is the time header
#include <iostream>

using namespace std;

class Time
{
public:
void set(int h, int m, int s);
void print ();
void increment();
int get_hour();
int get_mins();
int get_secs();
bool Equal(Time t);
bool LessThan(Time t);
bool GreaterThan(Time t);

private:
int hour;
int mins;
int secs;
};

time.cpp
#include "time.h" // Time.cpp file

void Time::set(int h, int m, int s)
{
hour = h;
mins = m;
secs = s;
}
void Time::increment()
{
secs ++;
if (secs == 60)
{
cout<<"\nsecond is "<<secs;
mins ++;
secs = 0;
}
if ( mins == 60)
{
hour ++;
cout<<" min is "<<mins;
mins = 0;
}
if ( hour == 24)
cout<<" hour is "<<hour;
hour = 0;

}

void Time::print()
{
if (hour < 10) cout << '0';
cout << hour << ":";
if (mins < 10) cout << '0';
cout << mins << ":";
if (secs < 10) cout << '0';
cout << secs << endl;

}
int Time::get_hour()
{
return hour;
}
int Time::get_mins()
{
return mins;
}
int Time::get_secs()
{
return secs;
}

bool Time::Equal(Time t)
{
if ( hour == t.hour && mins == t.mins && secs == t.secs)
return true;
else
return false;
}

bool Time::LessThan(Time t)
{
if(hour < t.hour || hour == t.hour && mins < t.mins || hour == t.hour && mins == t.mins && secs < t.secs)
return true;
else
return false;
}

bool Time::GreaterThan(Time t)
{
if(hour > t.hour || hour == t.hour && mins > t.mins || hour == t.hour && mins == t.mins && secs > t.secs)
return true;
else
return false;
}

main.cpp
#include "time.h"

int main()
{
Time begin;
Time diff;
Time end;
Time check_in;


int h, m, s;
begin.set (12,0, 0);
end.set (18, 0, 0);

cout << " enter hour, min, sec\n ";
cin >>h>>m>>s;
check_in.set (h, m, s);

cout << " the time you checked in ";
check_in.print ();

if (check_in.Equal(begin))
cout << "You are right on time. Lets go to lunch !! ";
else if(check_in.LessThan(begin))
{

while(!begin.Equal(check_in))
{
diff.increment();
/*-------------------------*/
}
}
h = diff.get_hour();
m = diff.get_mins();
s = diff.get_secs();
cout<<"time is "<<h<<":"<<m<<":"<<s<<endl;
return 0;
}
i know its not complete but i kind of narrow down the problem. the peoblam is that in main.cpp when the while loop starts, there is no increment of second. im dont quite remember how the prof. did it class. i tried few ways but it dont workout because secs is defined in privet in the time class. i think if we could get the secs to inc while the loop is on it will work out fine and the rest can be completed without any problem.
hey thanks man...i got everything to work except getting the seconds to convert to minutes / hours..i believe it has something to do with the diff.get...i'll let you know how it turns out....
i gave it a though here is what i came up with, while in the loop when the variable secs in the increment increases we need another variable for second to increase as well so that it can get the difference bet. two time. of corse there other simpler ways but i huss it wouldnt hurt to know more ways right. the stright was to do this is int h,m,s from the int main and put condition in the while loop such as:
if (check_in.Equal(begin))
cout << "You are right on time. Lets go to lunch !! ";
h = 0;
m = 0;
s = 0;
else if(check_in.LessThan(begin))
{

while(!begin.Equal(check_in))
{
check_in.increment(); sorry about writing diff.increment();
s++;
if(s==60)
{
m+++
s = 0;
}
if(m==60)
....
....
if(h==24)
....
}

than print the time out in any format u wish. if anyone else has a different idea please post it.
Topic archived. No new replies allowed.