Help with C++ homework!

I'm in desperate need of help with a homework assignment. I have to write a program that will take the user's birthday and offer calculation options:
days user has been alive
days until next birthday
days until user is 87 years old

I've started writing the preliminary code, but I'm a little lost with all of the functions and how to implement them. Criticism, advice, or instructions are all GREATLY appreciated.

The code is given below: (I know I'm missing a few things as this is just a rough draft)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Homework 4
//Days of Our Lives
//Given a birthdate how many days has the user been alive, how many
// days until their next birthday, how many days until 87 

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

enum MONTHS {JAN=1, FEB, MAR, APR, MAY, JUUN, JUL, AUG, SEPT, NOV, DEC};

int Current_Date(int current_month, int current_day, int current_year);
int Days_Alive(int month, int day, int year, int current_month, int current_day, int current_year,
                   int birth_year, int birth_month, int birth_day);
bool isLeap(int year);
int daysTilNextBday( int birth_day, int birth_month, int current_day, int current_month, int current_year, int year);
int daysInMonth(int month, int year);
int get_Birthday(int birth_month, int birth_day, int birth_year);
int daysInYear(int year);
int thisYear(int current_year);
int thisDay(int current_day);
int thisMonth(int current_month);
int bDay(int birth_day);
int bMonth(int birth_month);
int bYear(int birth_year);

char menu_choice;
char MainMenu(char shape_choice);
char Table_of_Contents();

int main()
{
    int month, day, year, current_month, current_day, current_year, birth_day, birth_month, birth_year, end_month, end_day, end_year;
    char exit;
    
    cout << "Hello! Welcome to The Days of Our Lives Age Calculator!" << endl << endl;
    cout << "Using your birthday, this program can perform several calculations." << endl << endl << endl;
    cout << "Please choose from the following menu: " << endl << endl;
    cout << "To calculate how many days you have been alive, enter 'D'." << endl << endl;
    cout << "To calculate how many days until your next birthday, enter 'N'." << endl << endl;
    cout << "To calculate how many days until you reach four score and seven years (87 years old), enter 'F'." << endl << endl;
    cout << "To quit this program, enter 'Q'." << endl << endl;
    cin << menu_choice;
    
    return (menu_choice);
    
    do
    {
          exit = MainMenu(Table_of_Contents());
    }
    while((exit!= 'Q')&&(exit!='q'));
    system("PAUSE");
    return EXIT_SUCCESS;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

char MainMenu(char menu_choice)
{
     
     switch (menu_choice)
     {
           case 'D':
           case 'd':
                cout << "You chose to calculate how many days you've been alive." << int Days_Alive(int month, int day, int year, int current_month, int current_day, int current_year,
                   int birth_month, int birth_year, int birth_day);
                cout << endl << endl;
                break;
                        
           case 'N':
           case 'n':
                cout << "You chose to calculate how many days until your next birthday." << int daysTilNextBday(int birth_day, int birth_month, int birth_year, int current_day, int current_month, int current_year, int year);
                cout << endl << endl;
                break;
                        
           case 'F':
           case 'f':
                cout << "You chose to calculate how many days until you are four score and seven years old (87)." << AreaRectangle(get_length(), get_width());
                cout << endl << endl;
                break;

int get_Birthday(int birth_month, int birth_day, int birth_year){
    
    cout << "Please enter your Birthday. Use (1-12) for the months."  << endl;
    int bMonth(int birth_month);
    cout << "month: " << endl;
    int bDay(int birth_day);
    cout << "day: " << endl;   
    int bYear(int birth_year);
    cout << "year: " << endl;
    cout << "Your Birthday is: "<<birth_month<< " /"<<birth_day;
    cout << " / " << birth_year << endl << endl;
}

int Current_Date(int& current_month, int& current_day, int& current_year){    
    cout << "Please enter today's date." << endl;
    int thisMonth(int current_month);
    int thisDay(int current_day);
    int thisYear(int current_year);
    cout << "The Current Date is: "<< current_month << " /"<< current_day;
    cout << " / " << current_year << endl << endl;
}
bool divisibleBy( int number, int divisor){
     return number % divisor == 0;     
}

bool isLeap(int year) {
   bool leap(false);
   if (divisibleBy( year, 4) ){
         if(divisibleBy( year, 100)){
           if (divisibleBy( year, 400))
             leap = true;   
           else
             leap = false;
         }    
         else
           leap = true;       
   }
   else
         leap = false;         
       
       
       return leap;  
}    
int daysInFebruary(int year){
    int result(28);
    if ( isLeap(year))
      result = 29;
      
    return result;
}
/*int Days_in_Month(int month, int year)
{
    if(9==month || 4==month || 6==month || 11==month)
    {
                return 30;
    }
    else if (2==month)
    {
         if(is_Leap())
         return 29;
         else
         return 28;
    }
    else
    {
        return 31;
    }
}*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
int daysInMonth( int month, int year){
    int result(0);
    
    if (month == 9 || month == 4 || month == 6 || month == 11){
       result = 30;
    }
    else if ( month == 2){
        result = daysInFebruary(year); 
    }
    else {
        result = 31; 
    }
    
    return result;
}    

int currentDayNumber( int month, int day, int year){
    int result(0);
    
    // compute days of months passed:
    if ( month > 1)
       result = 31;
    if (month > 2)
       result = result + daysInFebruary(year);
    if (month > 3)
       result = result + 31;
    if (month > 4)
       result = result + daysInMonth(4, year);
    if (month > 5)
       result = result + 31;
    if (month > 6)
       result = result + 30;
    if (month > 7)
       result = result + 31;
    if (month > 8)
       result = result + 31;
    if (month > 9)
       result = result + 30;
    if (month > 10)
       result = result + 31;
    if (month > 11)
       result = result + 30;
       
    //add to result current day:
    result = result + day;
    
    return result;  
}

int daysInYear(int year){
   int result(365); 
   
   if (isLeap(year) == true){
      result = 366;
   }
   return result; 
}
int thisYear(int current_year){
    int result(0);
    cout<< "What year is it currently? " << endl;
    cin >> current_year;
    return result = current_year;
}
int thisDay(int current_day){
    int result(0);
    cout<< "What day number is it today? " << endl;
    cin >> current_day;
    return result= current_day;
}
int thisMonth(int current_month){
    int result(0);
    cout << "What month is it currently? " << endl;
    cin >> current_month;
    return result = current_month;
}
int bYear(int birth_year){
    int result(0);
    cout << "What year were you born in? " <<endl;
    cin >> birth_year;
    return result = birth_year;
}
int bMonth(int birth_month){
    int result(0);
    cout << "What month were you born in? " <<endl;
    cin >> birth_month;
    return result = birth_month;
}
int bDay(int birth_day){
    int result(0);
    cout << "What day were you born on? " <<endl;
    cin >> birth_day;
    return result = birth_day;
}
    

int Days_Alive(int current_month, int current_day, int current_year,
                   int birth_month, int birth_year, int birth_day)
{
    int result(0);
    cout << "Please enter the current month, the current day, and the current year, all separated by spaces only." << endl; 
    cout << "Example: Jan 1, 2013 = 1 1 2013" << endl << endl;
    cin >> current_month >> current_day << current_year;
    cout << "Please enter your birthdate, using the same format." << endl << endl;
    cin >> birth_month >> birth_day >> birth_year;
    
    result = (current_year - birth_year)*daysInYear + (current_month - birth_month)*daysInMonth + (current_day - birth_day)
    return cout << "You have been alive " << result << "days!";
}

int daysTilNextBday(int birth_day, int birth_month, int birth_year, int current_day, int current_month, int current_year, int year)
{
    int result(0);
    cout << "Please enter the current month, the current day, and the current year, all separated by spaces only." << endl; 
    cout << "Example: Jan 1, 2013 = 1 1 2013" << endl << endl;
    cin >> current_month >> current_day << current_year;
    cout << "Please enter your birthdate, using the same format." << endl << endl;
    cin >> birth_month >> birth_day >> birth_year;
  
    result = daysInYear(year) - get_Birthday(birth_month, birth_day, birth_year);
    return result;                    
}

int FourScoreSevenYears (int birth_month, int birth_day, int birth_year, int year)
{
    int result(0);
    cout << "Please enter the current month, the current day, and the current year, all separated by spaces only." << endl; 
    cout << "Example: Jan 1, 2013 = 1 1 2013" << endl << endl;
    cin >> current_month >> current_day << current_year;
    cout << "Please enter your birthdate, using the same format." << endl << endl;
    cin >> birth_month >> birth_day >> birth_year;
    
    result = daysInYear(year) - Days_Alive(int current_month, int current_day, int current_year,
                   int birth_month, int birth_year, int birth_day)
    return result;
in main()

return (menu_choice);

will always return and exit your program

basically your whole menu is F'd in the A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//in main

int day, month, year;

while(exit != 'q' || exit != 'Q')
{
  std::cout << "Enter the day you were born(dd): ";
  std::cin >> day;
  std::cout << "\nEnter the month you were born(mm): ";
  std::cin >> month;
  std::cout << "\nEnter the year you were born(yyyy): ";
  std::cin >> year;
  std::cout << '\n';

  //Continue to do stuff here

//Now you have some of your necessary parameters that you need to pass to your functions
}

return 0;



You always need these 3 numbers, to get the current year, if you are using windows look into the tm struct, you can populate it with one function call and then it is as easy as tm->day, tm->year etc...
Last edited on
The function IsLeapYear can be written like this:

1
2
3
4
5
6
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
     std::cout << year << " is a leap year\n";
}
else{
     std::cout << year << " is not a leap year" << std::endl;
}


You could also do with a parallel array that has the days in month. Or have a struct which holds the month name & days in month.

Michaela Elise is right - your menu selection is far to complicated, the same might be said for the rest of your code as well. 270 LOC is to much for this assignment - one could probably do it about 100 as a rough guess. Here is an outline of a menu:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <ctype.h>  //for the toupper function

bool Quit = false;
char Option = 'D';

while(!Quit){
    ShowMenu();  //this function has cout's that display the menu options
    cin >> Option;
    Option = toupper(Option); //avoids testing vars twice

     switch (Option) {

          case 'D' :
                 //call this function
                 break;
         
          //other cases here

          case 'Q' : //user wants to quit
               Quit = true;  //this way execution continues after the while loop
               break;         //or call the exit function instead to end program

          default :
              std::cout << "Invalid Option Try again"
              break;

     }
}


You also need to make use of functions to get input once, so you don't have repetitive code.

EDIT: What you need is one function that calculates the number of days between 2 dates.

Hope all goes well 8+D
Last edited on
I had a look at this earlier, and it's a bit of a mess. Try implementing TheIdeasMan's struct, for example:


1
2
3
4
5
6
7
struct Date{

	int month,year,day;

};



Last edited on
THANKS for all of the help, I'm actually VERY new to programming entirely. And I'm incredibly lost sometimes. I know it's a mess.

The structure of the menu looks great!

Could someone give me an idea about how to structure the functions though? (For all 3 options: days alive, days til birthday, days til 87). And how do I implement them in the code?

An example on how to calculate the days between two dates would be awesome!

@ toomanystars, what is struct? I'm guessing it's a type, but I haven't seen that before.



Topic archived. No new replies allowed.