Need help with comparing and incrementing values
Sep 4, 2016 at 8:34pm UTC
-Create two clockType objects: cellPhoneClock and myComputerClock.
-Prompt the user to enter the times for both clocks.
-Display both times with appropriate documentation.
-If the clocks do not have equal times, set myComputerClock to the cellPhoneClock time.
-Set myComputerClock so that it is 1 hour, 5 minutes, 23 seconds faster than the cellPhoneClock.
-Display both times again, with appropriate documentation.
I am needing help starting with the step where it says that if the clocks arent equal to set them equal to each other.
testclockType.cpp file
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
#include <iostream>
#include<string>
#include "clockType.h"
using namespace std;
int main()
{
//Variables for cell phone clock
int phrs;
int pmins;
int psecs;
//Variables for computer clock
int chrs;
int cmins;
int csecs;
cout << " Welcome! \n" ;
cout << " Set your clock \n" ;
clockType cellPhoneClock;
clockType myComputerClock;
cout << " Enter the time for your cell phone clock: \n" ;
cout << " Hours: " ;
cin >> phrs;
cout << " Minutes: " ;
cin >> pmins;
cout << " Seconds: " ;
cin >> psecs;
cellPhoneClock.setTime(phrs,pmins,psecs);
cout << endl << endl;
cout << " Enter the time for your computer clock: \n" ;
cout << " Hours: " ;
cin >> chrs;
cout << " Minutes: " ;
cin >> cmins;
cout << " Seconds: " ;
cin >> csecs;
myComputerClock.setTime(chrs,cmins,csecs);
cout << endl << endl;
cout << " Cellphone : " ;
cellPhoneClock.printTime();
cout << endl << endl;
cout << " Computer : " ;
myComputerClock.printTime();
cout << endl << endl;
if (phrs != chrs && pmins != cmins && psecs != csecs)
{
myComputerClock.equalTime(cellPhoneClock);
cout << "The original time was : \n" ;
cout << "Cell Phone : " << cellPhoneClock.printTime();
cout << endl << endl;
cout << "Computer : " << myComputerClock.printTime();
cout << endl << endl;
cout << "The time now is : \n" ;
cout << "Cellphone : " <<
}
else
{
myComputerClock.incrementHours();
myComputerClock.incrementMinutes();
myComputerClock.incrementSeconds();
}
return 0;
}
clockType.h file
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
#ifndef CLOCKTYPE_H
#define CLOCKTYPE_H
using namespace std;
class clockType
{
public :
void setTime (int hours, int minutes, int seconds);
void getTime (int & hours, int & minutes, int & seconds) const ;
void printTime() const ;
void incrementHours();
void incrementMinutes();
void incrementSeconds();
bool equalTime(const clockType& otherClock) const ;
clockType(int hours, int minutes, int seconds);
clockType();
private :
int hr; //stores the hours
int min; //stores the minutes
int sec; //stores the seconds
};
#endif
testclockTypeImp.cpp file
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
#include<iostream>
#include "clockType.h"
using namespace std;
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::printTime() const
{
if (hr < 10)
cout << "0" ;
cout << hr << ":" ;
if (min < 10)
cout << "0" ;
cout << min << ":" ;
if (sec < 10)
cout << "0" ;
cout << sec;
}
void clockType::incrementHours()
{
hr++;
if (hr > 23)
hr=0;
}
void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours(); //incrementHours
}
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes(); //incrementMinutes
}
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
//Constructor without parameters
clockType::clockType()
{
hr = 0;
min = 0;
sec = 0;
}
//Constructor with parameters
clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = hours;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds <60)
sec = seconds;
else
sec = 0;
}
Topic archived. No new replies allowed.