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

Dani AI

Generated

has the right idea, but a few concrete problems cause wrong counts in Cases B and D and elsewhere: the command-line check accidentally uses assignment, the calendar math is done piecemeal (so Feb 29 and year/century leap rules are mishandled), and the biorhythm values are printed before they’re computed. ’s suggestion to split responsibilities is the best single improvement — separate date arithmetic (days-between and days-in-month), leap-year logic, and the small utilities that compute cycles.

A reliable approach is to convert any y/m/d into a single “days-since-epoch” integer and subtract. That removes ad-hoc month-switching and leap-day fudge factors. The snippet below shows a compact, correct leap-year test, a days-in-month helper, and a robust days-from-civil routine (Howard Hinnant’s algorithm) suitable for days-between calculations:

static inline bool is_leap(int y) {
    return (y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0));
}

int days_in_month(int m, int y) {
    static const int mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
    return (m == 2) ? mdays[1] + (is_leap(y) ? 1 : 0) : mdays[m-1];
}

/* days_from_civil converts y,m,d to an absolute day count; subtract two values to get days between. */
int days_from_civil(int y, unsigned m, unsigned d) noexcept {
    y -= m <= 2;
    const int era = (y >= 0 ? y : y-399) / 400;
    const unsigned yoe = static_cast<unsigned>(y - era * 400);
    const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d - 1;
    const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy;
    return era * 146097 + static_cast<int>(doe) - 719468; // days since 1970-01-01
}

int days_between(int y1,int m1,int d1,int y2,int m2,int d2) {
    return days_from_civil(y2,m2,d2) - days_from_civil(y1,m1,d1);
}

Use days_between(birth, end) for totalDaysAlive (no extra +1 unless your spec counts the birth day). Compute the last birthday by comparing month/day to the end date; then days_since_last_bday = days_between(last_bday, end_date). Fix the command-line test to use ==, call physical(..), emotional(..), intellectual(..) after totalDaysAlive is set, and add tests: same date -> 0, next day -> 1, cross-century leap checks (1900 not leap, 2000 leap). For background on the leap rule see Leap year and for the algorithm details see Howard Hinnant’s date algorithms: date algorithms.

/* 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]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.