HOW TO FIND

SUPPOSE I gave my input as 16 5 2013

how can i get output as tomorrow is not hoilday.
std::cout << "tomorrow is not holiday\n";
Logically you have to store these data (calendar with holidays) somewhere in the program. Multidimitional array can be use or use database.
Firstly, the question is not clear.

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.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>  // required in order to use the file streaming

using namespace std;

/* Class definition and other stuff goes here */

const short 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)

5 12 2013
6 12 2013
25 12 2013
26 12 2013
1 1 2014


I hope I made it clear.


NOTE: This is just an exemplification. You need to implement all this in your class!
Last edited on
> 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]?
Last edited on
>
Undefined behaviour

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.


Kind regards,
Raul Butuc.
Last edited on
Works PERFECTLY.

Define 'works'. It's an error to increment and read a scalar in the same expression (more or less, see http://en.cppreference.com/w/cpp/language/eval_order for more details


It's a regular reading check of the file state. Learnt that from Doina Logofatu,

I didn't believe it so I found that book...
ifstream in("boxes.in");
ofstream out("boxes.out");
while(in && !in.eof() && in >> vCBoxes){

that is seriously repeated over and over (and it's not even the most bizarre code it has)

Try to find a different book if you want to learn C++. A good start is the list at http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Last edited on
> Undefined what ?
> Works PERFECTLY.

Tip: Compile with all warnings enabled.

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.

Kind regards,
Raul
Last edited on
@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.
> ? And in >> holidays[++i] isn't ?

There is no problem with
1
2
in >> holidays[++i]  ;
in >> holiday[i].MM >> holiday[i].YYYY;


But there is a problem with:
in >> holiday[++i].DD >> holiday[i].MM >> holiday[i].YYYY;
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.

Kind regards,
Raul
Topic archived. No new replies allowed.