Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
can anyone please help me with this c++ program?
Write a program that calculates the number of hours of your life that you have spent sleeping. Assume that you sleep 8 hours each night. To simplify the problem, assume that there are always 30 days in each month, and 365 days in each year. The program output should look similar to:
Enter you birthdate (using numbers for months)
Month:9
Day:27
Year:1984
Enter today's date (using numbers of months)
Month:10
Day:26
Year:2002
You have been alive for 6599 days.
You have slept 52792 hours.
3 Answers
- 1 decade agoFavorite Answer
Variables assumed:
int birthYear, birthMonth, birthday, thisYear, thisMonth, today, hours
Not actual code: (Just logic)
hours = (thisYear - birthYear) *365;
if (birthMonth < thisMonth) {
hours += (thisMonth - birthMonth) * 30; }
else {
hours -= (birthMonth - thisMonth) * 30;
if (birthDay > today) {
hours += (birthday - today); }
else {
hours -= 30 - birthday + today; )
hours *= 8;
- 1 decade ago
The Calendar function is hardcoded with the dates you supplied, though it can easily to adapted to accept the six parameters thru input. I'm certain you can handle that. Its calculations are perfect, near as I can tell.
[code]
#include <iostream>
//Calender function
void calendar(int month, int day, int year,
int pre_month, int pre_day, int pre_year)
{
cout<<"Birthdate: "<<month<<" "<<day<<" "<<year<<std::endl;
cout<<std::endl;
int forward_year=year;
int day_count=0;
//set calendar in motion
while (forward_year<pre_year+1)
{
day++;
day_count++;
if(day==30 && month<12)
{
day=0;
month=month+1;
}
else if(day==30 && month==12)
{
month=1;
forward_year=forward_year+1;
day_count+=5;
day=0;
}
else if(forward_year==pre_year &&
month==pre_month && day==pre_day)
{
break;
}
}
int sleep;
sleep=day_count*24/3;
cout<<day_count<<" is the number of days we've lived"<<std::endl;
cout<<std::endl;
cout<<sleep<<" is the number of hours we've slept"<<std::endl;
cout<<std::endl;
cout<<"Present Date: "<<month<<" "<<day<<" "
<<forward_year<<std::endl;
cout<<std::endl;
}
//End Calendar function
int main()
{
// Our hardcoded dates inside our function
calendar(9, 27, 1984, 10, 26, 2002);
// end function call
cin.get();
}
[/code]
- Lie RyanLv 61 decade ago
#include <iostream>
using namespace std;
int main ()
{
int birthmonth; int birthday; int birthyear;
int nowmonth; int nowday; int nowyear;
cout << "Enter your birthdate" << endl;
cout << "Month:";
cin >> birthmonth;
cout << endl;
cout << "Day:";
cin >> birthday;
cout << endl;
cout << "Year:";
cin >> birthyear;
cout << endl;
cout << "Enter today's date" << endl;
cout << "Month:";
cin >> nowmonth;
cout << endl;
cout << "Day:";
cin >> nowday;
cout << endl;
cout << "Year:";
cin >> nowyear;
cout << endl;
AliveTime(birthmonth, birthday, birthyear, nowmonth, nowday, nowyear);
//cout << "You have slept " << (8/24)*AliveTime*24<< "hours.";
cout << "You have slept " << AliveTime*8 << "hours.";
}
int AliveTime(int birthmonth, int birthday, int birthyear, int nowmonth, int nowday, int nowyear) {
int year; int month; int day;
year = (nowyear - (birthyear - 1)) * 365;
month = (nowmonth - 1) * 30 + (12-birthmonth)*30;
day = 30-birthday + nowday;
return year + month + day;
}
Source(s): not tested Edit: OK, for the meantime, ignore the code, the date calculation is plainly incorrect.