I am new to C . I am facing a problem. I want to have 2 different dates (DD/MM/YYYY) How do I assign an overall variable for the first chunk and another one for the 2nd chunk?
For example:
First Day of date: 2
First Month "" : 5
First year "" : 1985
-------
Second ""
second ""
second ""
--------
if both are the same dates the output will be "Both are equal" else the greater will be mentioned "Date ... is greater"
I appreciate the help in advance.
#include <iostream>
using namespace std;
int main()
{
//This should be my first chunk
int first_date;
cout << "first day: ";
cin >> first_y;
int first_month;
cout << "first month: ";
cin >> first_month;
int first_year;
cout << "first year: ";
cin >> first_year;
}
CodePudding user response:
You could create a struct
to keep the day, month and year:
struct date_t {
int day;
int month;
int year;
};
You could also add member functions to that struct
to simplify the input process:
struct date_t {
// ... the above member variables goes here ...
void enter_day(const char* prompt) {
std::cout << prompt << " day: ";
std::cin >> day;
}
void enter_month(const char* prompt) {
std::cout << prompt << " month: ";
std::cin >> month;
}
void enter_year(const char* prompt) {
std::cout << prompt << " year: ";
std::cin >> year;
}
void enter_all(const char* prompt) {
enter_day(prompt);
enter_month(prompt);
enter_year(prompt);
}
};
If you now create an array of date_t
:
date_t dates[2];
you can just call the enter_all()
function for both:
dates[0].enter_all("first");
dates[1].enter_all("second");
Now, everything related to the first date will be in dates[0]
, like dates[0].day
and everything related to the second date will be in dates[1]
, like dates[1].year
etc.
To compare two date_t
's you can add comparison operators. Here are a few examples. You should be able to use these to create the missing operators, like operator<=
etc. If you use C 20, you could implement operator<=>
.
// compare two `date_t`s to see if they are equal:
bool operator==(const date_t& lhs, const date_t& rhs) {
return lhs.day == rhs.day &&
lhs.month == rhs.month &&
lhs.year == rhs.year;
}
// check if the left date is before the right date
bool operator<(const date_t& lhs, const date_t& rhs) {
if(lhs.year != rhs.year) return lhs.year < rhs.year;
if(lhs.month != rhs.month) return lhs.month < rhs.month;
return lhs.day < rhs.day;
}
// check if the left date is after the right date
bool operator>(const date_t& lhs, const date_t& rhs) {
return rhs < lhs; // use the above operator<
}
With that you could do:
if(dates[0] == dates[1]) std::cout << "they are equal\n";
// or...
if(dates[0] < dates[1]) std::cout << "first < second\n";
// or...
if(dates[0] > dates[1]) std::cout << "first > second\n";
CodePudding user response:
The below program shows what you want:
#include <iostream>
struct Date
{
//always always initialize built in type in block/local scope
int date = 0, month = 0, year = 0; // by default public
//default constructor
Date() = default;
//lets overload operator= for comparing two Date types as you desire
friend bool operator==(const Date &lhs, const Date &rhs);
friend bool operator!=(const Date &lhs, const Date &rhs);
//overload operator>> for taking input from user
friend std::istream& operator>>(std::istream &is, Date &rhs);
};
bool operator==(const Date &lhs, const Date &rhs)
{
return lhs.date == rhs.date &&
lhs.month == rhs.month &&
lhs.year == rhs.year;
}
bool operator!=(const Date &lhs, const Date &rhs)
{
return !(lhs == rhs);
}
std::istream& operator>>(std::istream &is, Date &rhs)
{
std::cout<<"Enter date: ";
//take date as input from user
std::cin >> rhs.date;
std::cout<<"Enter month: ";
//take month as input from user
std::cin >> rhs.month;
std::cout<<"Enter year: ";
//take year as input from user
std::cin >> rhs.year;
//check if input succedded
if(!is)
{
rhs = Date();
}
return is;
}
int main()
{
//create first Date
struct Date d1;
std::cin >> d1;//take input from user
//create second Date
struct Date d2;
std::cin >> d2;//take input from user
//lets check if dates d1 and d2 entered by user are equal or not
std::cout<<"The dates d1 and d2 are: "<<(d1==d2? "equal": "not equal")<<std::endl;
return 0;
}