Thread: Getting program and overload constructors to work

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    4

    Getting program and overload constructors to work

    I need help getting my program to run when the user enters in one of the two choices to print out the results. Any suggestions on how to add my user prompts?

    Prompt:
    Create a Date class with the following capabilities:
    a) Output the date in multiple formats such as
    MM/DD/YYYY
    June 14, 1992


    b) Use overloaded constructors to create Date objects initialized with dates of the formats in part a).

    You should only have 3 member data: integer month, integer day, and integer year. Also, you should have at least 2 constructors and 2 functions in your Date.cpp.
    Code:
    Date::Date ( int mm, int dd, int yy )
    {   // check mm, dd, and yy and save them into month, day, and year member data.
    
    }
     
    Date::Date ( string mstr, int dd, int yy )
    {   // check and convert mstr, dd, and yy and save them 
        //       into month, day, and year member data.
    
    }
     
    void Date::print ( )
    {   //  print the date in MM/DD/YYYY
    
    }
     
    void Date::printFullDate ( )
    {   //  print the date in long date such as
        //                June 24, 2002
    
    }
    /////////////
    You also need to create 3 get functions and 3 set functions for the member data.

    Requirements:
    1. 3 files: Date.h, Date.cpp-> class implementation, test.cpp-> test file
    Sample Run:
    3 choices for the user to pick:
    Enter 1 for format: mm/dd/yyyy
    Enter 2 for format: month dd, yyyy
    Enter 3 to exit:

    Choice: 1 //user input

    //user inputs the information
    Enter month (1-12): 1
    Enter day of month: 5
    Enter year: 2005

    Mm/dd/yyyy: 1/5/2005 //gets checked for valid date.
    //////////////
    Enter 1 for format: mm/dd/yyyy
    Enter 2 for format: month dd, yyyy
    Enter 3 to exit:

    Choice:2
    Enter month name: May
    Enter day of month: 10
    Enter year:2010
    Month dd, yyyy: May 10, 2010

    Enter 1 for format: mm/dd/yyyy
    Enter 2 for format: month dd, yyyy
    Enter 3 to exit:

    Choice:3

    //////
    Here is the code I have so far:
    date.h
    Code:
    #indef DATE_H
    #define DATE_H
    
    
    #include <string>
    using std::string;
    
    
    class Date
    {
    	public:
    		DATE();
    		DATE(int,int);
    		DATE(string,int,int);
    		
    		void setDay(int);
    		void setMonth(int);
    		void print()const;
    		
    		void printDDDYYYY() const;
    		void printMMDDYY() const;
    		void printMonthDDYYYY() const;
    		
    		~Date();
    		private:
    			int month;
    			int day;
    			int year;
    			
    			int chckDay(int) const;
    			int daysInMonth(int) const;
    			bool isLeapYear() const;
    			void setMMDDFromDDD(int);
    			string convertMMToMonth(int) const;
    			void setMMFromMonth(string);
    			int convertYYYYToYY() const;
    			void setYYYYFromYY(int);
    		};
    		#endif
    		
    }
    date.cpp
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    
    #include<iomanip>
    using std::setw;
    using std::setfill;
    
    
    #include <ctime>
    using std:;time;
    using std::localtime;
    using std::tm;
    using std::tim_t;
    
    
    #include "date.h"
    
    
    Date::Date()
    {
    	struct tm *ptr;
    	time_t t=time(0);
    	ptr=localtime(&t);
    	day=ptr->tm_mday;
    	month=1+ptr->tm_mon;
    	year=ptr->tm_year +1900;
    }
    
    
    
    
    Date::Date(int mm,int dd,int yy)
    {
    	setYYYYFromYY(yy);
    	setMonth(mm);
    	setDay(dd);
    }
    //montName ->mstr
    Date::Date(string mstr, int dd, int yyyy)
    {
    	setMMFromMonth(mstr);
    	setDay(dd);
    	year=yyyy;
    }
    void Date::setDay(int d)
    {
    	day=checkDay(d);
    }
    void Date::setMonth(int m)
    {
    	if(m>0 && m<=12)
    	month=m;
    	else{
    		month=1;
    		cout<<"Invalid month";
    		
    	}
    }
    void Date::print() const
    {
    	cout << month <<'/'<<day<<'/'<<year<<endl;
    }
    void Date::printMMDDYY() const
    {
    	cout <<setw(2)<< setfill('0')<<month<<'/'<<setw(2)
    	<<setfill('0')<<day<<'/''
    	<<setw(2)<<setfill('0')<<convertYYYYToYY()<<endl;
    }
    void Date::printMonthDDYYYY()) const
    {
    	cout<<convertMMtoMonth(month)<<''<<day<<","<<year<<endl;
    }
    Date::~Date()
    {
    	cout<<"Date object destructor";
    	print();
    	cout<<endl;
    }
    int Date::checkDay(int testDay) const
    {
    	if(testDay >0 && testDay<=daysInMonth(month))
    	return testDay;
    	if(month==2 && testDay==29 && isLeapYear())
    	return testDay;
    	cout<<"invalid";
    	
    }
    int Date:;daysInMonth(int m) const
    {
    	if(isLeapYear() && m==2)
    	return 29;
    	static const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
    return daysPerMonth[m];
    
    
    }
    bool Date::isLeapYear() const
    {
    	if(year%400==0 || (year % 4 ==0 && year %100!=0))
    	return true;
    	else
    	return false;
    }
    int Date::convertDDtoDDD() const
    {
    	int ddd=0;
    	for(int i=1;i<month;i++)
    	ddd+=daysInMonth(i);
    	ddd+=day;
    	return ddd;
    }
    void Date::setMMDDFromDDD(int ddd)
    {
    	int dayTotal=0;
    	int m;
    	for(m=1;m<=12 && (dayTotal +daysInMonth(m))<ddd;m++)
    	dayTotal+=daysInMonth(m);
    	setMonth(m);
    	setDay(ddd-dayTotal);
    }
    string Date::convertMMToMonth(int mm) const
    {
    	static const string months[]=
    	{"", "January","February", "March", "April","May","June","July","August",
    	"September","October", "November", "December"};
    	return months[mm];
    	
    }
    void Date::setMMFromMonth(string m)
    {
    	bool matchFound=false;
    	for(int i=1;i<=12 && !matchFound;i++)
    	{
    		string tempMonth=convertMMToMonth(i);
    		if(tempMonth==m)
    		{
    			setMonth(i);
    			matchFound=true;
    		}
    	}
    	if(!matchFound)
    	{
    		cout<<"Invalid month name";
    		setMonth(1);
    	}
    }
    int Date::convertYYYYToYY() const
    {
    	return(year>=2000 ? year-2000: year-1900);
    }
    void Date::setYYYYFromYY(int yy)
    {
    	year=(yy<7 ? yy+2000: yy +1900);
    }
    test.cpp
    Code:
    #include "date.h"
    
    
    int main()
    {
    	Date date1(256,1999);
    	Date date2(3,25,04);
    	Date date3("September",1,2000);
    	Date date4;
    	
    	date1.print();
    		date2.print();
    	date3.print();
    	date4.print();
    cout << '\n';
    
    
    
    
    date1.printMMDDYY();
    date2.printMMDDYY();
    date3.printMMDDYY();
    date4.printMMDDYY();
    cout<<'\n';
    
    
    date1.printMonthDDYYYY();
    date2.printMonthDDYYYY();
    date3.printMonthDDYYYY();
    date4.printMonthDDYYYY();
    cout<<endl;
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    You seem to have achieved a lot without knowing (or even attempting) something basic like
    Code:
    cout << "Enter 1 for format: mm/dd/yyyy" << endl;
    cout << "Enter 2 for format: month dd, yyyy << endl;
    cout << "Enter 3 to exit: << endl;
    > using std:;time;
    Did you even compile this before posting?

    Also, your #endif in the header file has migrated since your previous post.

    I guess you need this idea as well.
    Code:
    Date *myDate;
    ///
    if ( choice == 1 ) {
      myDate = new Date(3,25,04);
    } else if ( choice == 2 ) {
      myDate = new Date("September",1,2000);
    }
    ///
    delete myDate;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    Where do I put that code in?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    I'm guessing you didn't write that code all by yourself.

    One of many examples of the same exercise scattered across the web.
    bcit-courses/ex10_07.cpp at master * ddyngrp/bcit-courses * GitHub


    You use that code inside your 'main' to replace the test code you have already.

    At what point are you planning to use your education for real, instead of coasting along using google and maintaining the illusion that you're actually learning anything (trust me, you're not). If you knew anything, you wouldn't have that code, and be asking that question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I don't think it's a good practice either to use using directives inside header files.

  6. #6
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    I forgot to mention that my professor gave us a head start to fill in and wants to adjust like the way it needs to look like. sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload function to work with multiple data types
    By SkywardTaco in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2015, 12:33 PM
  2. Need help understanding overload program
    By OCcounty in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2010, 07:50 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. How do I Overload these operators in this program?
    By advocation in forum C++ Programming
    Replies: 9
    Last Post: 04-24-2005, 11:29 AM
  5. getline overload, solution didn't work
    By Clane in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2002, 09:53 AM

Tags for this Thread