I have some C++ code:
#include <bjarne/std_lib_facilities.h>
struct Date {
int month;
int day;
int year;
};
Date get_date();
Date get_birth_date();
int days_in_month (int month);
bool is_valid_date (const Date& date);
bool is_before (const Date& date1, const Date& date2);
int main()
{
cout << "Welcome to Age Calculator!\n";
Date current;
current = get_date();
cout << "Would you like to see how old you are (y/n)?";
char answer, slash;
cin >> answer;
Date birthday;
if(answer == 'y'){
birthday = get_birth_date();
bool valid = is_valid_date (birthday);
bool before = is_before (current,birthday);
while(!valid && !before){
cout << "Invalid birth date? Please re-enter: ";
cin >> birthday.month >> slash >> birthday.day >> slash >> birthday.year;
valid = is_valid_date (birthday);
before = is_before (current,birthday);
}
cout << "Your birthday is: " << birthday.month << "/" << birthday.day << "/" << birthday.year << "\n";
}
else
cout << "You are so chicken! \n";
}
Date get_date()
{
cout << "Please enter today's date (mm/dd/yyyy): ";
Date today;
char slash;
cin >> today.month >> slash >> today.day >> slash >> today.year;
bool valid = is_valid_date (today);
while(!valid){
cout << "Invalid date? Please re-enter: ";
cin >> today.month >> slash >> today.day >> slash >> today.year;
valid = is_valid_date (today);
}
cout << "Date entered was: " << today.month << "/" << today.day << "/" << today.year << "\n";
return today;
}
Date get_birth_date()
{
cout << "Please enter your birth date (mm/dd/yyyy): ";
Date birth;
char slash;
cin >> birth.month >> slash >> birth.day >> slash >> birth.year;
return birth;
}
int days_in_month (int month)
{
int month31[7] = {1,3,5,7,8,10,12};
for(int i = 0; i < 7; i++){
if(month == month31[i])
return 31;
}
int month30[4] = {4,6,9,11};
for(int i = 0; i < 4; i++){
if(month == month30[i])
return 30;
}
if(month == 2)
return 28;
}
bool is_valid_date (const Date& date)
{
int months[12] = {1,2,3,4,5,6,7,8,9,10,11,12};
int days = 0;
for(int i = 0; i < 12; i++){
if(date.month == months[i]){
days = days_in_month (date.month);
if(date.day <= days && date.day > 1){
return true;
}
}
return false;
}
bool is_before (const Date& date1, const Date& date2)
{
cout << date1.day << " " << date2.day;
if(date2.year < date1.year){
return true;
}
else if(date2.year == date1.year)
{
cout << "-";
if(date2.month < date1.month)
return true;
else if(date2.month == date1.month){
if(date2.day <= date1.day)
return true;
}
else
return false;
}
return false;
}
I know that the is_valid_date function works, but when I'm testing a birthday that comes after the current day entered, for some reason, it passes the is_before test and never goes to the while loop asking the user to enter a valid birthday. Any suggestions would be greatly appreciated! Thanks in advance!
Edit: The specific inputs that I'm testing are: for today's date, I enter 05/06/2015 and for a birthday, I enter 05/07/2015. It then prints the birthday, which means it skips the while loop in int main(), which it shouldn't do, since the birthday comes after the current date.
Aucun commentaire:
Enregistrer un commentaire