Problem with my C++ code. The code should display for midnight 00:00:00 but inst
ID: 3604916 • Letter: P
Question
Problem with my C++ code. The code should display for midnight 00:00:00 but instead it displays 0:00:00 I don't know how to fix it. Thanks.
Steps for the assignment:
https://docs.google.com/document/d/1RNP9ATq7MKM47oiI0Lnrua0r7lxZvJBz3FiQffjSS4U/edit?usp=sharing
Code:
https://drive.google.com/file/d/0B8jPGepnXF2cWjd3TG5IZTJWMzA/view?usp=sharing
noon is 12:00:00 midnight is 0:00:00 lunch time is 11:30:06 supper time is 18:45:00 bed time is 23:59:59 From lunch time to bed time is 44999 seconds. From supper time to noon is -24300 seconds From midnight to bed time is 86399 seconds supper time is after noon lunch time is not after supper time bed time is before midnight Press any key to continue . . .Explanation / Answer
#include <iostream>
using std::cout;
using std::endl;
//struct definition
struct tod {
int hour; //the hour, 0-23
int minute; //the minute, 0-59
int second; //the second, 0-59
char descr[32]; //the description of the time of day
};
//function prototype(s)
void coutTod(const tod&);
void coutTod(int, const tod*);
int diffTod(const tod&, const tod&);
int isAfter(const tod&, const tod&); // true if 1st is after 2nd
int isBefore(const tod&, const tod&); // true if 1st is before 2nd
int main() {
tod theTime[] = {
{ 12,0,0,"noon" },
{ 0,0,0,"midnight" },
{ 12,30,0,"lunch time" },
{ 18,35,0,"supper time" },
{ 22,0,0,"bed time" }
};
coutTod(5, theTime);
cout << "From " << theTime[0].descr
<< " to " << theTime[3].descr
<< " is " << diffTod(theTime[0], theTime[3])
<< " seconds." << endl << endl;
cout << "From " << theTime[4].descr
<< " to " << theTime[2].descr
<< " is " << diffTod(theTime[4], theTime[2])
<< " seconds." << endl << endl;
cout << "From " << theTime[2].descr
<< " to " << theTime[3].descr
<< " is " << diffTod(theTime[2], theTime[3])
<< " seconds." << endl << endl;
if (isAfter(theTime[0], theTime[4]))
cout << theTime[4].descr << " is after " << theTime[0].descr << endl << endl;
else
cout << theTime[4].descr << " is not after " << theTime[0].descr << endl << endl;
if (isAfter(theTime[2], theTime[3]))
cout << theTime[2].descr << " is after " << theTime[3].descr << endl << endl;
else
cout << theTime[2].descr << " is not after " << theTime[3].descr << endl << endl;
if (isBefore(theTime[4], theTime[1]))
cout << theTime[4].descr << " is before " << theTime[1].descr << endl << endl;
else
cout << theTime[4].descr << " is not before " << theTime[1].descr << endl << endl;
system("pause");
}
void coutTod(const tod& t) {
cout << t.descr << " is ";
//you need to print starting zero if hours less than 10
if(t.hour<10) cout << '0';
cout << t.hour << ':';
if (t.minute < 10) cout << '0';
cout << t.minute << ':';
if (t.second < 10) cout << '0';
cout << t.second << endl << endl;
}
void coutTod(int n, const tod* t) {
for (int i = 0; i < n; i++) {
coutTod(t[i]);
}
}
int diffTod(const tod& time1, const tod& time2)
{
int diff, diffHr, diffMin, diffSec = 0;
diffHr = time2.hour - time1.hour;
diffMin = time2.minute - time1.minute;
diffSec = time2.second - time1.second;
diff = (diffHr * 60 * 60) + (diffMin * 60) + (diffSec);
return diff;
}
//true if 1st is after 2nd
int isAfter(const tod& time1, const tod& time2)
{
if (diffTod(time2, time1) >= 0)
return 1;
else
return 0;
}
//true if 1st is before 2nd
int isBefore(const tod& time1, const tod& time2) {
if (diffTod(time1, time2) < 0)
return 1;
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.