If you wish to know which date is a holiday and which is not, as @ErrorFree mentioned, you are supposed to create a database with all those dates. Given you have the calendar stored in a file (suppose a .txt or maybe .dat one) you can call it in your program like this:
- Create a file stream for input.
- Read all the dates which are holidays.
- Store those dates in either an array or in a STL vector.
#include <iostream>
#include <fstream> // required in order to use the file streaming
usingnamespace std;
/* Class definition and other stuff goes here */
constshort MAX = 100; // Max number of days for the holiday array below
struct Date{ // Structure for date
short DD;
short MM;
short YYYY;
} date, holiday[MAX]; // Declare a variable date of type Date and an array for the holidays
int main(void) {
// Some code
short i = 0;
fstream in("holidays.dat", ios::in); // Could be .txt or anything else
while (in && !in.eof() && in.good()){ // Check the state of the file -- not end of file
i++;
in >> holiday[i].DD >> holiday[i].MM >> holiday[i].YYYY;
}
// Rest of the code
// In order to check whether a date is a holiday or not:
cin >> date.DD >> date.MM >> date.YYYY; // Read a date
for (short j = 1; j <= i; ++j){
if (date.DD == holiday[j].DD && date.MM == holiday[j].MM && date.YYYY == holiday[j].YYYY){
cout << "The date " << date.DD << "/" << date.MM << "/" << date.YYYY << " is a holiday." << endl;
}
}
// Maybe some other code
}
EDIT:
Example for the file "holidays.dat" (you can open that extension with notepad)
> in >> holiday[++i].DD >> holiday[i].MM >> holiday[i].YYYY;
Undefined behaviour
> while (in && !in.eof() && in.good()){ // Check the state of the file -- not end of file
check the reading operation while(in >> holiday[i].DD >> holiday[i].MM >> holiday[i].YYYY) ++i;
> fstream in("holidays.dat", ios::in);
Don't ask for what you won't use std::ifstream in("holidays.dat");
> struct Date{/**/} date;
limit the scope of your variables (¿why global?)
> for (short j = 1; j <= i; ++j)
¿why are you throwing away holiday[0]?
Undefined what ?
It's a basic struct operation. Learnt that about 2 years ago among the first lessons.
Works PERFECTLY. (EDIT: Haven't seen that possible unexpected behaviour warning in my complier, my apologies).
>
check the reading operation
Yes. So, what's the problem with it ? It's a regular reading check of the file state.
>
Don't ask for what you won't use
I have also learnt using this from Doina Logofatu in order to declare a file stream.
>
limit the scope of your variables (¿why global?)
Just because it was an example. I said it is not the actual program. He has to implement that struct in a class.
He won't use any declaration like that at all. He will declare the DD, MM, YYYY as either private variables with accessor and mutator functions, or just as public variables.
>
why are you throwing away holiday[0]?
Because it's more handy and less confusing for a beginner to work with values starting from 1. And not 0. That's how I learnt programming.
Nowadays, I do use the element at position 0, but only if it represents a crucial problem in means of memory allocation and space restrictions.
If I were at the IOI or ACM, yes, I would definitely use 0 too. For home projects, no need.
With -std=c++11 -Wall -Wextra (GCC), you would get:
1 2
in >> holiday[++i].DD >> holiday[i].MM >> holiday[i].YYYY;
// *** warning: operation on 'i' may be undefined [-Wsequence-point]
Even after removing the undefined behaviour, (and starting with position 1 instead of 0), this construct leads to a subtle error:
1 2 3 4
while (in && !in.eof() && in.good()){ // Check the state of the file -- not end of file
++i ;
in >> holiday[i].DD >> holiday[i].MM >> holiday[i].YYYY;
}
The value of i would be one more that what it ought to be when the attempted input fails.
Hmm ? So, let me see if I got it right.. I am not allowed to increment while reading ? Well, that's something my ex teacher taught me at school a looong time ago.
When I've been taught that, my teacher mentioned that the ++i, being a pre-incrementation will be done first. So at the time the holiday[i] is read, i is already equal to 1.
However, about that thing with the fstream I know I read it in one of her books or articles. I'm sure.. I don't know exactly which, but I can assure you I did.
About the while thingy I always thought it meant:
in => checks if file exists
!in.eof() => checks if not end of file
in.good() => checks if file is corrupted/finished/damaged/etc.. :|
I don't know to what extent that might be considered good or bad programming practice, but however, I'm still learning, so if that really isn't good, I will try to find a new style. Though, I am going to check that list of books and maybe buy some.
@http://en.cppreference.com/w/cpp/language/eval_order i = i++ + 1; // undefined behavior (but i = ++i + 1; is well-defined) ? So, i = ++i + 1 is well-defined ? And in >> holidays[++i] isn't ? Hmm, well, I have to do some research on that.
Ahh.. I got it.. I guess this is my mistake. My teacher talked only about single variables and not about reading more than 1. I interpreted the information wrongly, it seems..
Thanks for clearing this out for me. Won't happen again.