1. Write struct Date: month day year display() displayLong() set(int,int,int) Me
ID: 3625542 • Letter: 1
Question
1. Write struct Date:month
day
year
display()
displayLong()
set(int,int,int)
Member function display(), displays the date in the mm/dd/yyyy format. Member function
displayLong() displays the date in the nameOfMonth dd, yyyy format. (e.g. 11/10/2010 and
November 10, 2010).
Member function displayLong() has a local string array that contains the names of the 12 months.
Member function set(int,int,int) will set month, day, and year to the parameter values.
2. Write a program that contains a while loop. In the while loop prompt the user for a day, month and
year, and then display the date in the two formats.
Explanation / Answer
Here you go
----------------------------
struct Date
{
int month;
int day;
int year;
void Display (void);
void DisplayLong (void);
void Set (int, int, int);
};
void Date::Display ()
{
int temp = year;
if (month < 10)
cout << "0" << month << "/";
else
cout << month << "/";
if (day < 10)
cout << "0" << day << "/";
else
cout << day << "/";
if (year > 1000)
{
temp = year % 100;
temp * 10;
}
if (temp < 10)
cout << "0" << temp << endl;
else
cout << temp << endl;
}
void Date::DisplayLong ()
{
char months [] [12] = {"January ","February ", "March ","April ","May ","June ", "July ","August ", "September ","October ", "November ", "December "};
for (int i = 0; ((i < 12) && (months [month] [i] != ' ')); i++)
cout << months [month] [i];
cout << " ";
if (day < 10)
cout << "0" << day << ", ";
else
cout << day << ", ";
if (year < 10)
cout << "0" << year << endl;
else
cout << year << endl;
}
void Date::Set (int i, int j, int k)
{
month = i;
day = j;
year = k;
}
void main ()
{
bool Continue = false;
char g;
while (true)
{
Continue = false;
int m;
int d;
int y;
Date D;
cout << "Enter the month: ";
cin >> m;
cout << "Enter the day: ";
cin >> d;
cout << "Enter the year: ";
cin >> y;
D.Set (m, d, y);
D.Display ();
D.DisplayLong ();
cout << "Continue? y/n" << endl;
cin >> g;
if ((g == 'y') || (g == 'Y'))
Continue = true;
else;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.