Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clock using a class?

Status
Not open for further replies.

goncrazybbaksoon

Programmer
Jan 30, 2003
6
US
I need to make a working clock using a class that increments from 12:00:00 am to 11:59:59 pm. I thought I had the right idea in my code, but I'm stuck in some kind of loop that repeatedly prints the same 24 numbers(which I can't see what the connection is) over and over again. I'm not sure what the problem is, or where to go from here. Help!
 
show us some code, please? The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
//Clock

#include <iostream.h>
#include <stdlib.h>

class time //new data type
{
public:
void setmin(int newmin);
void sethour(int newhour);
void setsec(int newsec);
int getmin();
int getsec();
int gethour();
char am();
char pm();
void nexttime();

private:
int hour; //1-12 hours
int min; //00-59
int sec; //00-59
};
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void main()
{
time t;
int iseccounter=0;
int imincounter=0;
int ihourcounter=0;

for(ihourcounter = 00; ihourcounter <=12; ihourcounter++)
{
for(imincounter = 00; imincounter <= 59; imincounter++)
{
for(iseccounter = 00; iseccounter <= 59; iseccounter++)
{
cout<<t.getmin();
cout<<t.gethour();
cout<<t.getsec()<<endl;
}
}
}



system(&quot;PAUSE&quot;);
return 0;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void time::setmin(int newmin)
{
min = newmin; //min from Main is sent into (int newmin)
//newmin is set to min
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void time::sethour(int newhour)
{
hour = newhour; //hour from Main is sent into (int newhour)
//newhour is set to hour
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void time::setsec(int newsec)
{
sec = newsec ; //sec from Main is sent into (int newsec)
//newsec is set to sec
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
int time::gethour()
{
return hour;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
int time::getmin()
{
return min;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
int time::getsec()
{
return sec;
}
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void time::nexttime()
{
hour++;
}
 
The class could do with a redesign but to try and keep it as close to the original so you can see what's going on better - you didn't increment the secs,mins,hours in the loop:


[tt]
#include <iostream>

using namespace std; //introduces namespace std


class myTime
{
public:
myTime() { hour=min=sec=0; }
inline void setmin(int newmin) { min=newmin; };
inline void sethour(int newhour) { hour = newhour; }
inline void setsec(int newsec) { sec = newsec; }
inline int getmin() const { return min; }
inline int getsec() const { return sec; }
inline int gethour() const { return hour; }
// char am();
// char pm();
inline void nexttime() { hour++; }
private:
int hour;
int min;
int sec;
};



int main()
{
myTime t;
for(auto int ihourcounter=1;ihourcounter<=12;ihourcounter++)
{
for(auto int imincounter=0;imincounter<=59;imincounter++)
{
for(auto int iseccounter=0;iseccounter<=59;iseccounter++)
{
t.setsec(iseccounter);
t.setmin(imincounter);
t.sethour(ihourcounter);
cout<<t.gethour()<<&quot;:&quot;;
cout<<t.getmin()<<&quot;:&quot;;
cout<<t.getsec()<<endl;
}
}
}
return 0;
}[/tt]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top