Question

Write a C++ program that determines the user's age after the   user enters both the current...

Write a C++ program that determines the user's age after the  

user enters both the current date and hisher birthdate as 3  

integers:                yyyy mm dd                           

                                                                

Define a class which is named DateType and will be used to    

declare 2 objects: "birthday" and "today". The class will have

a function "output" which will display the date in conventional

form (like it is above next to Date Assigned: Month dd, yyyy

and it will have a second function named    

“input” which will receive one 3 digit date: yyyy mm dd.      

“input” will be used to enter today’s date and the birthdate  

and must only accept valid numbers for month and day.              

                                                                

EXAMPLE: If the user entered the #’s:      2018 2 6        

            then output would display         Feb 6, 2018       

                                                                

After the user enters the birthdate, display the dates in     

conventional form and the user's age.                         

                                                                

EXAMPLE: If the user entered a birthdate: 1999 7 4        

                                                                 

            The program would display:                          

                                                                

                    Today is Feb 6, 2018                         

                    Your birthday was on Jul 4, 1999            

                    You are 18 years old                        

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshots of the code:

Screenshots of the output:

The Code Used:

#include<iostream>
using namespace std;
class DateType
{
   public:
       int day,month,year;
       void input();
       void output();
};
void DateType::input()
{
   while(1)
   {
       cout<<"Enter the date in (yyyy mm dd) format: ";
       cin>>year>>month>>day;
       if(month<1 || month >12)
       {
           cout<<"Invalid month enter the date again."<<endl;
           continue;
       }
       if(month==4||month==6||month==9||month==11)
       {
           if(day<1||day>30)
           {
               cout<<"Invalid day enter the date again."<<endl;
               continue;
           }
       }
       else if(month==2)
       {
           if(day<1||day>28)
           {
               cout<<"Invalid day enter the date again."<<endl;
               continue;
           }
       }
       else
       {
           if(day<1||day>31)
           {
               cout<<"Invalid day enter the date again."<<endl;
               continue;
           }
       }
       break;
   }
}
void DateType::output()
{
   string monthName;
   switch(month)
   {
       case 1:
           monthName = "Jan";
           break;
       case 2:
           monthName = "Feb";
           break;
       case 3:
           monthName = "Mar";
           break;
       case 4:
           monthName = "April";
           break;
       case 5:
           monthName = "May";
           break;
       case 6:
           monthName = "June";
           break;
       case 7:
           monthName = "July";
           break;
       case 8:
           monthName = "Aug";
           break;
       case 9:
           monthName = "Sep";
           break;
       case 10:
           monthName = "Oct";
           break;
       case 11:
           monthName = "Nov";
           break;                              
       default:
           monthName = "Dec";  
   }
   cout<<monthName<<" "<<day<<", "<<year<<endl;
}
void calculateAge(DateType today,DateType birthday)
{
   int age;
   if(today.month<birthday.month)
   {
       age = today.year - birthday.year -1;
   }
   else if(today.month==birthday.month)
   {
       if(today.day<birthday.day)
       {
           age = today.year - birthday.year -1;
       }
       else
       {
               age = today.year - birthday.year;
       }
   }
   else
   {
           age = today.year - birthday.year;
   }
   cout<<"You are "<<age<<" years old."<<endl;
}
int main()
{
   DateType today;
   DateType birthday;
   cout<<"Enter todays date"<<endl;
   today.input();
   cout<<"Enter your birthdate"<<endl;
   birthday.input();
   cout<<endl;
   cout<<"Today is ";
   today.output();
   cout<<"Your birthday was on ";
   birthday.output();
   calculateAge(today,birthday);
   return 0;
}

I hope you like the solution. In case of any doubts regarding the solution feel free to ask it in the comment section. If you like the solution please give a thumbs up.

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that determines the user's age after the   user enters both the current...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

    C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook: A formula can be used to calculate the number of days between two dates. This is affected by...

  • Python 3. Date Printer Write a program that reads a string from the user containing a...

    Python 3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018 3. Date Printer Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018

  • Write a program that accepts a date from the user in the form mm/dd/yyyy

    # C PROGRAMMING LANGUAGEWrite a program that accepts a date from the user in the form mm/dd/yyyy and then dis- plays it in the form month dd, yyyy, where month is the name of the month: Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date February 17, 2011 Store the month names in an array that contains pointers to strings.

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Write a program that reads a string from the user containing a date in the form...

    Write a program that reads a string from the user containing a date in the form mm/dd/yyyy. It should print the date in the format March 12, 2018   I am asking for help in basic python please

  • Write in python code Project 11-1: Birthday Calculator Create a program that accepts a name and...

    Write in python code Project 11-1: Birthday Calculator Create a program that accepts a name and a birth date and displays the person's birthday the current day, the person's age, and the number of days until the person's next birthday Console Birthday Calculator Enter name: Joel Enter birthday (MM/DD/YY): 2/4/68 Birthday: Sunday, February 04, 1968 Today: Joel is 48 years old. Joel's birthday is in 73 days Tuesday, November 22, 2016 Continue? (y/n): y Enter name: Django Enter birthday (MM/DD/YY):...

  • Finish the program. Follow the program logic and then complete the algorithm in the function. Use...

    Finish the program. Follow the program logic and then complete the algorithm in the function. Use the programs given constructs and variables only. #include <iostream> #include <string> using namespace std; // Prototype string format(string); int main() { // The following string will hold the user's input. string userDate; // The following string will hold the formatted date. string formattedDate; // Get a date from the user. cout << "Enter a date in the form mm/dd/yyyy: "; cin >> userDate; //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT