0

This is the code I've written for a homework assignment - however in the footer for Cases B & D I have no idea what I should have for coding to get the correct count - any help/comments is greatly appreciated!

/* as long as actual birthday has occurred life is fine
however if birthday has not occured.. wrong date */
//progam which calculates birthDate, todaysDate, leapYears and then
//determines how many days the user has been alive

#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

void getTodaysDate (int &month, int &day, int &year);
int daysAlive (int month, int day, int year,
              int end_month, int end_day, int end_year);
int leap_years (int month, int day, int year, int end_month, int end_day,
                int end_year);
void intellectual (int totalDaysAlive, int &intelCycle);
void emotional (int totalDaysAlive, int &emotCycle);
void physical (int totalDaysAlive, int &physCycle);




//start main

int main(int argc, char *argv[])
{
int month, day, year; 
int currentAge, leap_years, daysSinceLastBday;
char answer = 'y';
int end_month;
int end_day;
int end_year;
int totalDaysAlive;
int emotCycle, physCycle, intelCycle;



// determine end date

  if (argc == 1)
  {
    getTodaysDate(end_month, end_day, end_year);
  }
  else if (argc = 4)
  {
    end_month = atoi(argv[1]);
    end_day = atoi(argv[2]);
    end_year = atoi(argv[3]);
  }
  else
  {
     cout << "Enter ending month."<< endl;
     cin >> end_month;
     cout << "Enter ending day." << endl;
     cin >> end_day;
     cout << "Enter ending year." << endl;
     cin >> end_year;
     cout << "You entered: "<<end_month <<"/" << end_day << "/" << end_year
          << endl;
 }//end of end date   

  //prime pump - begin while loop
  while (answer == 'y')
  {
  //ask for birth year
  cout << "Please enter your birth year (YYYY) ";
  cin >> year;

  while (year < 1901)
  {
  cout << "Year needs to be past 1901 "<<endl;
  cout << "Please enter your birth year (YYYY) ";
  cin >> year;
  }
  //ask for birth month

  cout << "Please enter your birth month ";
  cin >> month;
  while(month >12 || month < 1)
  {
  cout << "You've entered an invalid month "<<endl;
  cout << "Please enter your birth month ";
  cin >> month;
  }
  //ask for birth day

  cout<<"Please enter your birth day ";
  cin >> day;
  while(day > 31 || day < 1)
  {
  cout << "You've entered an invalid day "<<endl;
  cout << "Please enter your birth day " << endl;
  cin >> day;
  }
 // cout stmt and the 6 values

   totalDaysAlive = daysAlive(month, day, year, end_month, end_day, end_year);      

   cout << "You have been alive for " << totalDaysAlive << " days." << endl;
   // biorythms part
   cout << "Biorhytms Stats:" << endl;
   cout << "Physical Cycle: " << physCycle << " (go for a run!)" << endl;
   cout << "Emotional Cycle: " << emotCycle << " (lock yourself in your room!)"
        << endl;
   cout << "Intellectual Cycle: " << intelCycle
        << "(Why did I take this class?)"
        << endl;


   cout << "Do you wish to continue? (y/n) ";
   cin >> answer;
   }// end of while user wishes to continue




}// end of main


// footer - start functions

void getTodaysDate (int &month, int &day, int &year)
{
   struct tm *myTime;
   time_t x;
   time(&x);
   myTime = localtime(&x);
   year = myTime->tm_year+1900;
   day = myTime->tm_mday;
   month = myTime->tm_mon+1;
}  //end of getTodaysDate

// calculate current age
int daysAlive (int month, int day, int year,
              int end_month, int end_day, int end_year)
{
    int retval;
    int currentAge;   //calculate current age
    int leapDays; // calculate leap year
    int daysSinceLastBday; //calculate days since last birthday
    int temp_month;
    int i; 
    int intelCycle, emotCycle, physCycle;


        if (month < end_month) //case A - had bd this year
         {
        currentAge = (end_year - year);
        cout<<"Your current age is: "<<currentAge<<endl;
         }
        else
          if (month > end_month) //case B - not had bd this year
          {
          currentAge = (end_year - year) - 1; 
          cout<<"Your current age is: "<<currentAge<<endl;
          }     
        else    
          if (day < end_day) //case C - same month no bd yet
          {
          currentAge = (end_year - year);   
          cout<<"Your current age is: "<<currentAge<<endl;  
          }
        else
          if (day > end_day) //case D - same month & had bd
          {
           currentAge = (end_year - year) -1;
           cout<<"Your current age is: "<<currentAge<<endl;
          }
        else
         {
          currentAge = (end_year - year); //case E - birthday
          cout << "Happy Birthday!" << endl;
         }


     leapDays = leap_years (month, day, year, end_month, end_day, end_year);
     cout << "The number of leap days you've been alive is: " 
          << leapDays << endl;

      //determine the number of days since last birthday  


        if(month < end_month)
      {
      //A add days from day to end of month - had bd this year
      switch (month)
      {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
              daysSinceLastBday = 31 - day;
              break;

          case 2:
              daysSinceLastBday = 28 - day;
              break;

          case 4:
          case 6:
          case 9:
          case 11:
              daysSinceLastBday = 30 - day;
              break;

          default:
          break;
     } 
 }       
       else 
       {
       if(month > end_month)

      //B not had bd this year - still need to figure out how to use same as D
      switch (month)
      {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
              daysSinceLastBday = 31 - end_day;
              break;

          case 2:
              daysSinceLastBday = 28 - end_day;
              break;

          case 4:
          case 6:
          case 9:
          case 11:
              daysSinceLastBday = 30 - end_day;
              break;

          default:
          break;
  }       
       else
        if(day < end_day)
        //end_day minus day

      //C same month no birthday figure out how to use
      switch (month)
      {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
              daysSinceLastBday = 31 - day;
              break;

          case 2:
              daysSinceLastBday = 28 - day;
              break;

          case 4:
          case 6:
          case 9:
          case 11:
              daysSinceLastBday = 30 - day;
              break;

          default:
          break;
     }    
        else if(day > end_day)

      //D same month had bd figure out how to use - same as B
      // day minus end_day
      switch (month)
      {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
              daysSinceLastBday = 31 - end_day;
              break;

          case 2:
              daysSinceLastBday = 28 - end_day;
              break;

          case 4:
          case 6:
          case 9:
          case 11:
              daysSinceLastBday = 30 - end_day;
              break;

          default:
          break;
   }      
       else 

      //E - birthday

        {
          daysSinceLastBday = 0;
        }  



      // outside switch but still in if

      temp_month = month + 1;
      for (i = temp_month; i < end_month; i++)
      {
          switch (i)
          { 
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
              daysSinceLastBday += 31;
              break;

          case 2:
              daysSinceLastBday += 28;
              break;

          case 4:
          case 6:
          case 9:
          case 11:
              daysSinceLastBday += 30;

          default:
          break;
          }//end of switch stmt     
      } // end of for loop  

       daysSinceLastBday += end_day;
    }// end of if 







      retval = currentAge * 365 + leapDays + 1 + daysSinceLastBday;
      return (retval);
  }// end of days alive




   int leap_years (int month, int day, int year, int end_month, int end_day, 
       int end_year)
    {
       int retval;
         retval = 0;
         int yearstep;
         yearstep = year;
         while (yearstep <= end_year)
         {
             if ((yearstep % 4) == 0)
             {
                 retval++; //counts # of leap years
             }
             yearstep++;
         } 
          //case A 

  if ((month > 3) && (year %4) == 0)
  {
       retval--;
    }   


  if ((end_year % 4)== 0)
  {
    if (end_month == 1)
    retval--;

      if ((end_month == 2) && (end_day <= 28))
      {
          retval--;
      }
  }  
         return (retval); 
 } // end of leap years

 //biorhythms

 void intellectual (int totalDaysAlive, int &intelCycle)
 {
     intelCycle = (totalDaysAlive % 33);
 }

 void emotional (int totalDaysAlive, int &emotCycle)
 {
     emotCycle = (totalDaysAlive % 28);  
 }

 void physical (int totalDaysAlive, int &physCycle)  
 {
     physCycle = (totalDaysAlive % 23);
 } //end biorythms

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by VernonDozier
0
/* as long as actual birthday has occurred life is fine
however if birthday has not occured.. wrong date */
//progam which calculates birthDate, todaysDate, leapYears and then
//determines how many days the user has been alive

Life is not fine when birthday has already occurred. If you enter 1/1/1980 as the birthdate, you've been alive for the same number of days as when you enter 3/1/1980. You have some flaws in the program logic and you are doing far too much in the daysAlive function in my opinion. Calculating daysSinceLastBDay is a good candidate for having its own function. Split it into separate functions. It appears to me that daysSinceLastBDay is never more than 31, so you aren't calculating months since last birthday, then converting it to days. You were close on the code tags, but not quite:

[code=cplusplus] // paste code here

[/code]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.