Design a struct with global scope called Date, which contains three integers to
ID: 3767587 • Letter: D
Question
Design a struct with global scope called Date, which contains three integers to represent the day of the month, the month, and the year respectively. Design a function called readDate that receives in a single Date argument by reference, and prompts the user to fill in the data. Ask the user for the month, day, and year. Make sure the prompt is clear about what exact data you want the user to provide, and you should validate the Date information the user gives you (day has to be between 1 and 31, and month has to be between 1 and 12). To do this, you should build a "helper" function that returns true if the date passed into it is valid, and false otherwise. Declare a few Date variables in main, call readDate with a Date one at a time, and display it after function call to verify the date. Declare a vector of Date. Add 5 Dates to the vector. Write the function printDate that prints a Date input argument in a valid format. Test printDate with the Dates in your vector. Write the code for a function findEarlier. findEarlier should "return" the earlier of the two Dates passed to it (by reference, and declared const, of course) by copying the earlier Date into a third pass-by-reference parameter called earlierDate. Do not use the assignment (=) operator on Dates, but instead write a helper function called copyDate that takes in two Dates by reference and copies the data from the first into the second, element by element. Use the dates in your vector in main to find the earliest date in the vector.Explanation / Answer
#include<iostream>
#include <fstream>
#include <string>
#include <time.h>
#include<ctime>
#include <vector>
using namespace std;
struct Date
{
int day;
int month;
int year;
};
int check_day(int d)
{
if(d>=1 && d<=31)
return 1;
else
return 0;
}
int check_month(int m)
{
if(m>=1&&m<=12)
return 1;
else
return 0;
}
int check_year(int y)
{
if(y>=0 && y<=2015)
return 1;
else
return 0;
}
void readDate(Date *d1)
{
int d,m,y;
cout<<"Enter day"<<endl;
cin>>d;
while(check_day(d)!=1)
{
cout<<"Incorrect Day"<<endl;
cout<<"Enter day"<<endl;
cin>>d;
}
cout<<"Enter month"<<endl;
cin>>m;
while(check_month(m)!=1)
{
cout<<"Incorrect Month"<<endl;
cout<<"Enter month"<<endl;
cin>>m;
}
cout<<"Enter year"<<endl;
cin>>y;
while(check_year(y)!=1)
{
cout<<"Incorrect year"<<endl;
cout<<"Enter year"<<endl;
cin>>y;
}
(*d1).day=d;
(*d1).month=m;
(*d1).year=y;
}
void printDate(Date *d)
{
cout<<(*d).day<<"-";
cout<<(*d).month<<"-";
cout<<(*d).year<<endl;
}
int main()
{
Date d1,d2,d3;
readDate(&d1);
//cout<<d1.day;
printDate(&d1);
readDate(&d2);
printDate(&d2);
readDate(&d3);
printDate(&d3);
//Date d4,d5;
/*
vector<Date> date(5);
date.push_back(d1);
date.push_back(d2);
date.push_back(d3);
date.push_back(d4);
date.push_back(d5);
cout<<"Dates in Vector"<<endl;
printDate(&date.at(0));
printDate(&date.at(1));
printDate(&date[2]);
printDate(&date[3]);
printDate(&date[4]);
*/
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.