i am new in programming . i have a clock code. but want to modify it. when user press "0" it increment in time by 5min and when press "1" increment time by 10 min


_______________

//The complete program listing of the program that defines 
//and uses the class clockType

#include <iostream>
using namespace std;

class clockType
{
public:
    void setTime(int, int, int);
    void getTime(int&, int&, int&) const;
    void printTime() const;
    void incrementSeconds();
    void incrementMinutes();
    void incrementHours();
    bool equalTime(const clockType&) const;

private:
    int hr;
    int min;
    int sec;
};


int main()
{
    clockType myClock;
    clockType yourClock;  

    int hours;
    int minutes;
    int seconds;

       //set the time of myClock
    myClock.setTime(5, 4, 30);                          //Line 1

    cout << "Line 2: myClock: ";                        //Line 2
    myClock.printTime();    //print the time of myClock //Line 3
    cout << endl;                                       //Line 4

    cout << "Line 5: yourClock: ";                      //Line 5
    yourClock.printTime(); //print the time of yourClock  Line 6
    cout << endl;                                       //Line 7

       //set the time of yourClock
    yourClock.setTime(5, 45, 16);                       //Line 8

    cout << "Line 9: After setting, yourClock: ";       //Line 9
    yourClock.printTime(); //print the time of yourClock  Line 10
    cout << endl;                                       //Line 11

       //compare myClock and yourClock
    if (myClock.equalTime(yourClock))                   //Line 12
        cout << "Line 13: Both times are equal."
             << endl;                                   //Line 13
    else                                                //Line 14
        cout << "Line 15: The two times are not equal."
             << endl;                                   //Line 15

    cout << "Line 16: Enter the hours, minutes, and "
         << "seconds: ";                                //Line 16
    cin >> hours >> minutes >> seconds;                 //Line 17
    cout << endl;                                       //Line 18

       //set the time of myClock using the value of the
       //variables hours, minutes, and seconds
    myClock.setTime(hours, minutes, seconds);           //Line 19

    cout << "Line 20: New myClock: ";                   //Line 20
    myClock.printTime();    //print the time of myClock //Line 21
    cout << endl;                                       //Line 22

       //increment the time of myClock by one second
    myClock.incrementSeconds();                         //Line 23

    cout << "Line 24: After incrementing myClock by " 
         << "one second, myClock: ";                    //Line 24
    myClock.printTime();    //print the time of myClock //Line 25
    cout << endl;                                       //Line 26

      //retrieve the hours, minutes, and seconds of the 
      //object myClock
    myClock.getTime(hours, minutes, seconds);           //Line 27

      //output the value of hours, minutes, and seconds
    cout << "Line 28: hours = " << hours 
         << ", minutes = " << minutes 
         << ", seconds = " << seconds << endl;          //Line 28

    return 0;
}//end main


void clockType::setTime(int hours, int minutes, int seconds)
{
	if (0 <= hours && hours < 24)
	    hr = hours;
	else 
	    hr = 0;

	if (0 <= minutes && minutes < 60)
	    min = minutes;
	else 
	    min = 0;

	if (0 <= seconds && seconds < 60)
	    sec = seconds;
	else 
	    sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

void clockType::incrementHours()
{
	hr++;
	if(hr > 23)
 	   hr = 0;
}

void clockType::incrementMinutes()
{
	min++;
	if (min > 59)
	{
	    min = 0;
	    incrementHours();
	}
}

void clockType::incrementSeconds()
{
	sec++;

	if (sec > 59)
	{
	    sec = 0;
	    incrementMinutes();
	}
}

void clockType::printTime() const
{
	if (hr < 10)
	    cout << "0";
	cout << hr << ":";

	if (min < 10)
	    cout << "0";
	cout << min << ":";

	if (sec < 10)
	   cout << "0";
	cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
	return (hr == otherClock.hr 
		    && min == otherClock.min 
		    && sec == otherClock.sec);
}

Recommended Answers

All 3 Replies

Please use the code-tags.

And I don't quite understand your question. What do you mean with 'it runs clock 5 minutes'?

when user press "0" it increment in time by 5min and when press "1" increment time by 10 min

You could use a while loop.

char ch = ' ';
bool RepeatWhile = 1;
while(RepeatWhile) //Repeat while loop until something else then 0 or 1 is entered
{
  cin >> ch;
  switch(ch)
  {
    case '0':
      myClock.incrementMinutes(5);
      break;
    case '1':
      myClock.incrementMinutes(10);
      break;
    default:
      RepeatWhile = 0;
      break;
  }
}

Also change your current functions:

void clockType::incrementMinutes(int AMin)
{
  min = min + AMin;
  while (min > 59) //in case that min > 119
  {
    min = min - 60;
    incrementHours(1); //After you changed the function increment hours, otherwise use incrementHours();
  }
}

thanks for the help.but i didnt get ur point. where i have to do these changes.i have tried doing the cahnges but nothing can u please do these changes.thanks a lot for the help

The following code should work:

//The complete program listing of the program that defines
//and uses the class clockType
#include <windows.h>
#include <iostream>
using namespace std;

class clockType
{
public:
void setTime(int, int, int);
void getTime(int&, int&, int&) const;
void printTime() const;
void incrementSeconds(int);
void incrementMinutes(int);
void incrementHours(int);
bool equalTime(const clockType&) const;

private:
int hr;
int min;
int sec;
};


int main()
{
clockType myClock;
clockType yourClock;

int hours;
int minutes;
int seconds;

//set the time of myClock
myClock.setTime(5, 4, 30); //Line 1

cout << "Line 2: myClock: "; //Line 2
myClock.printTime(); //print the time of myClock //Line 3
cout << endl; //Line 4

cout << "Line 5: yourClock: "; //Line 5
yourClock.printTime(); //print the time of yourClock Line 6
cout << endl; //Line 7

//set the time of yourClock
yourClock.setTime(5, 45, 16); //Line 8

cout << "Line 9: After setting, yourClock: "; //Line 9
yourClock.printTime(); //print the time of yourClock Line 10
cout << endl; //Line 11

//compare myClock and yourClock
if (myClock.equalTime(yourClock)) //Line 12
cout << "Line 13: Both times are equal."
<< endl; //Line 13
else //Line 14
cout << "Line 15: The two times are not equal."
<< endl; //Line 15

cout << "Line 16: Enter the hours, minutes, and "
<< "seconds: "; //Line 16
cin >> hours >> minutes >> seconds; //Line 17
cout << endl; //Line 18

//set the time of myClock using the value of the
//variables hours, minutes, and seconds
myClock.setTime(hours, minutes, seconds); //Line 19

cout << "Line 20: New myClock: "; //Line 20
myClock.printTime(); //print the time of myClock //Line 21
cout << endl; //Line 22

//increment the time of myClock by one second
myClock.incrementSeconds(1); //Line 23

cout << "Line 24: After incrementing myClock by "
<< "one second, myClock: "; //Line 24
myClock.printTime(); //print the time of myClock //Line 25
cout << endl; //Line 26

//retrieve the hours, minutes, and seconds of the
//object myClock
myClock.getTime(hours, minutes, seconds); //Line 27

//output the value of hours, minutes, and seconds
cout << "Line 28: hours = " << hours
<< ", minutes = " << minutes
<< ", seconds = " << seconds << endl; //Line 28

char ch = ' ';
bool RepeatWhile = 1;
while(RepeatWhile) //Repeat while loop until something else then 0 or 1 is entered
{
  cin >> ch;
  switch(ch)
  {
    case '0':
      myClock.incrementMinutes(5);
      break;
    case '1':
      myClock.incrementMinutes(10);
      break;
    default:
      RepeatWhile = 0;
      break;
  }
  myClock.printTime(); // show the time
  cout << endl; // looks better on output
}

system("pause");

return 0;
}//end main


void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}

void clockType::incrementHours(int AHour)
{
  hr = hr + AHour;
  hr = hr % 24; // in case we get more than 24 hours, it sets it to somewhere between 0:00 and 23:59
}

void clockType::incrementMinutes(int AMin)
{
  min = min + AMin;
  if(min/60 > 0) // pointless to call when we don't need to increase the hours
  {
	incrementHours(min/60);
  }
  min = min % 60;
}

void clockType::incrementSeconds(int ASec)
{
  sec = sec + ASec;
  if(sec/60 > 0) // pointless to call when we don't need to increase the minutes
  {
	incrementMinutes(sec/60);
  }
  sec = sec % 60;
}

void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";

if (min < 10)
cout << "0";
cout << min << ":";

if (sec < 10)
cout << "0";
cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}

I changed it a bit. Instead of while loops I used / and %, which is more efficient. If you don't know I used them, then see it as a little homework. It isn't that hard to figure out ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.