I am doing a project for school that requires me to make a program that asks the user to enter a date by selecting two options for the format
1.MM/DD/YY (in numbers)
2.Month ,DD, YY (with the name of the month entered)

both options should then display the date in numbers and the date with the name of the month

however ive run into some problems
when i run option 1, the number date displays fine but when it prints the name of the month it only displays the letter r no matter what month is chosen, this may have to do with the if then statement i used to determine what the month name is based on the number given.
When running option 2 after the input is given it just freezes
i would really appreciate help with finding the problem and there may be other errors i may have overlooked because i am a beginner

ive attached the assignment instructions so you can better understand what the program is supposed to do and so you can see an example of what the output should look like

here is the code

date.h

#include <string>
using namespace std;
/*
 * No description
 */
class Date
{
	public:
           Date( int = 1, int = 1, int = 1900);
           Date( string , int = 1, int = 1900);
           ~Date();
 
          void print();
          void printFullDate();
          void setDay(int);
          void setMonth(int);
          void setYear(int);
          int getDay();
          int getMonth();
          int getYear();
		// class constructor
	
	private:
          int Day;
          int Month;
          int Year;
          int checkDay( int ) const;
           
};

#endif // DATE_H

date.cpp

// Class automatically generated by Dev-C++ New Class wizard

#include "date.h" // class's header file
#include <iostream>
#include <string>
using namespace std;
// class constructor
Date::Date(int mm, int dd, int yy )
{
  setMonth(mm);
  setYear(yy);
  setDay(dd);
  
  print();
  cout << endl;
  printFullDate();
  cout << endl;
  
	
} //End Date constructor

// class constructor 2
Date::Date(string mstr, int dd, int yy)
{
   int nummonth;              
   
   //Begin if statement
   if(mstr == "January")
   {
     nummonth = 1;
   }
   else if(mstr == "Febuary")
   {
     nummonth = 2;
   }
   else if(mstr == "March")
   {
     nummonth = 3;
   }
    else if(mstr == "April")
   {
     nummonth = 4;
   }
    else if(mstr == "May")
   {
     nummonth = 5;
   }
    else if(mstr == "June")
   {
     nummonth = 6;
   }
    else if(mstr == "July")
   {
     nummonth = 7;
   }
    else if(mstr == "August")
   {
     nummonth = 8;
   }
    else if(mstr == "September")
   {
     nummonth = 9;
   }
    else if(mstr == "October")
   {
     nummonth = 10;
   }
    else if(mstr == "November")
   {
     nummonth = 11;
   }
    else if(mstr == "December")
   {
     nummonth = 12;
   }
         else             
        cout << "Invalid month (" << mstr << ") set to 1. \n";  
   //End if statement
    
      setDay(dd);
      setYear( yy );
      setMonth( nummonth);
      
  
  print();
  cout << endl;
  printFullDate();
  cout << endl;
}                                  
//print object
void Date::print()
{
     cout << getMonth() << "/" << getDay() << "/" << getYear()<< endl;
} // end print

void Date::printFullDate()
{
     string Fmonth; 
     int nummonth = getMonth();

    switch(nummonth)
   {
     case 1:
          Fmonth = 'January';
     case 2: 
          Fmonth = 'February';
     case 3:
          Fmonth = 'March';
     case 4:
          Fmonth = 'April';
     case 5:
          Fmonth = 'May';
     case 6:
          Fmonth = 'June';                             
     case 7:
          Fmonth = 'July';
     case 8: 
          Fmonth = 'August';
     case 9:
          Fmonth = 'September';
     case 10:
          Fmonth = 'October';
     case 11:
          Fmonth = 'November';
     case 12:
          Fmonth = 'December';
          break;
     }// end switch
     cout<< Fmonth << " "<< getDay() << ", " << getYear() << endl;
} // end printFullDate
                 
// class destructor
Date::~Date()
{
	// insert your code here
}

void Date::setDay(int dd)
{
   Day =  checkDay( dd ) ;
}

int Date::getDay()
{
   return Day;
}

                      
void Date::setYear(int yy)
{
   Year = yy;
}

int Date::getYear()
{
   return Year;
}             
void Date::setMonth( int mm)
{   
    if( mm > 0 && mm <= 12)
      {
      Month = mm;
      }    
  else
  {
      Month = 1; // invalid month set to 1
      cout << "Invalid month (" << mm << ") set to 1. \n";
  }//end if    
}

int Date::getMonth()
{
     return Month;
}                
//Checks if the day is valid based on the month
int Date::checkDay(int testDay) const
{
    static const int daysPerMonth[13] =
           {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
           
           if ( testDay > 0 && testDay <= daysPerMonth[Month])
              return testDay;
           if ( Month == 2 && testDay ==29 && (Year % 400 == 0 ||
           ( Year % 4 == 0 && Year % 100 != 0) ) )
           return testDay;
       cout << "Invalid day " << testDay << ") set to 1. /n";
       return 1;
}

TestDate.cpp

#include "date.h" // class's header file
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int choice = 0;
    
    string monthname;
    
    cout<<"Enter 1 for format: MM/DD/YYYY"<<endl;
    cout<<"Enter 2 for format: Month DD, YYYY"<<endl;
    cout<<"Enter 3 to exit"<<endl;
    cin>>choice;
    // Begin While
    while(choice != 3)
    {
    // Begin if statment             
     if(choice == 1)
     {
               int month = 0;
               int day = 0;
               int year = 0;
               
               cout<<"Enter Month (1-12): ";
               cin>>month;
               cout<<"Enter Day of Month: ";
               cin>>day;
               cout<<"Enter Year: ";
               cin>>year;
               cout<<endl;
               
              
               Date(month, day, year);
               
       }
               
              else if(choice == 2)
               {
                    string monthname;
                    int day;
                    int year;
                    
               cout<<"Enter Month Name: ";
               cin>>monthname;
               cout<<"Enter Day of Month: ";
               cin>>day;
               cout<<"Enter Year: ";
               cin>>year;
               cout<<endl;
               Date(monthname, day, year);
               }
               else 
               {
                    cout<<"Invalid choice choose again";
               }
               //End if statement
               
    cout<<"Enter 1 for format: MM/DD/YYYY"<<endl;
    cout<<"Enter 2 for format: Month DD, YYYY"<<endl;
    cout<<"Enter 3 to exit"<<endl;
    cin>>choice;  
                  }
                  // End While
                  system("pause");
                  return 0;
    }

Recommended Answers

All 3 Replies

I've only glanced over your code, this caught my eye:

case 1:
          Fmonth = 'January';

What compiler are you using? This line should generate an error among the lines of "too many characters in constant".

I don't know if this is the cause of your problem, as it should not even compile. The easiest way is to run your code under a debugger and step through the code to see exactly what is going on. If after you do that, and you still don't know what is happening you can probably at least give us a more detailed description of the problem and show us what you've already eliminated.

its odd i used to get a warning when i compile of this but it still let it run and now it doesnt show it at all except when i debug
how can i make it display the right month name without having the value be too long

Please don't immediately ask how to do something... you've already done exactly what you have to do elsewhere in the code! How do you initialize a string?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.