### 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)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152`` ``````//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 #include #include 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: "<
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134`` ``````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? " <> birth_year; return result = birth_year; } int bMonth(int birth_month){ int result(0); cout << "What month were you born in? " <> birth_month; return result = birth_month; } int bDay(int birth_day){ int result(0); cout << "What day were you born on? " <> 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

 ``1234567891011121314151617181920`` ``````//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:

 ``123456`` ``````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:

 ``12345678910111213141516171819202122232425262728`` ``````#include //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:

 ``1234567`` ``````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.