the science museum has asked you to write a c++ application that children can use to find the total number of hours and years slept during their lifetime. assume they slept at an average of 8 hour per night. the user should enter the name, birthday, year and current year. to calculate the number of hours slept. 365 day per year, 30 day per month and 8 hours of sleep per day. the program should show how many years, month, weeks, days, hours, minutes, seconds the child slept in this lifetime. note use classes, function, and control structures to achieve this task. the user will also determine the number of children the process
#include <iostream>
using namespace std;
class ChildSleep
{
private:
string name;
int day, month, year, curYear;
public:
ChildSleep(string name, int day, int month, int year, int curYear):
name(name), day(day), month(month), year(year), curYear(curYear) {}
void totalSleep() {
int days = ((30*(12-month) + 30-day + (curYear-year-1)*365)*8)/24;
int years = days/365;
int months = days/30;
int weeks = days/7;
int hours = (30*(12-month) + 30-day + (curYear-year-1)*365)*8;
int minutes = hours*60;
int seconds = minutes*60;
cout << name << " slept " <<
years << " years, " <<
months << " months, " <<
weeks << " weeks, " <<
days << " days, " <<
hours << " hours, " <<
minutes << " minutes, " <<
seconds << " seconds.\n\n";
}
};
int main() {
int num;
cout << "Input the number of children: ";
cin >> num;
while (num--)
{
string name;
int day, month, year, curYear;
cout << "Input your name: ";
cin >> name;
cout << "Input a birthday (1-30): ";
cin >> day;
cout << "Input a birth month (1-12): ";
cin >> month;
cout << "Input a birth year: ";
cin >> year;
cout << "Input a current year: ";
cin >> curYear;
ChildSleep child(name, day, month, year, curYear);
child.totalSleep();
}
return 0;
}
Example:
Input the number of children: 2
Input your name: Azimjon
Input a birthday (1-30): 7
Input a birth month (1-12): 3
Input a birth year: 2002
Input a current year: 2021
Azimjon slept 6 years, 76 months, 326 weeks, 2287 days, 54904 hours, 3294240 minutes, 197654400 seconds.
Input your name: Sheroz
Input a birthday (1-30): 5
Input a birth month (1-12): 12
Input a birth year: 1998
Input a current year: 2021
Sheroz slept 7 years, 89 months, 383 weeks, 2685 days, 64440 hours, 3866400 minutes, 231984000 seconds.
Comments
Leave a comment