Passing array to function

Hi all, I'm having issues with a program for an assignment. The program should have 3 functions to determing the number of days fron January 1st, whether or not the year is a leap year, and if the date exists.
I want to pass an array (with the number of days in each month) to a function that, given the month and day of the month, determines if the day of the month actually exists (eg. jan 43 returns false).

Here's my code at the moment,
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

int year, month, day, days;
bool isLeapYear(int year)
{
    bool isLeap;
    if((year%4)==0)
    {
        isLeap=true;
        if((year%100)==0&&year>1582&&(year%400)!=0)
        isLeap=false;
    }   
}

int findDaysBefore(int month)
{
     if(month<3)
     days=31*(month-1);
     else
     days=30*(month-3)+(4*(month-2))/7+59;    
}

bool dateIsValid(int month, int day, int days[])
{
     if(0<month<13)
     {
         dateIsValid=true;
         if 0<day<=days[month]
         dateisvalid=true;
     }
     else
     dateIsValid=false;
}

using namespace std;

int main()
{
    ifstream fin("date2.txt");
    ofstream fout("D_F_Jan1.txt");
    
    int y=0, m=0, d=0, ds=0;
    bool ly, valid;
    int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    
    while (fin >> y >> m >> d)
    {
          fout << "For an input date of" << setw(5) << y << setw(3) << m << 
          setw(3) << d << ", " << "the total number of Days from Jan 1 is: ";
          
          ds=findDaysBefore(m);
          ly=isLeapYear(y);
          valid=dateIsValid(m, d, days);
          
          if (valid=false)
          fout << "Date is invalid.\n";
          else
          fout << ds+ly <<"\n";
    }   
   
}


and the errors:
63 C:\Documents and Settings\***\main.cpp assignment of function `bool dateIsValid(int, int, int*)'
63 C:\Documents and Settings\***\main.cpp cannot convert `bool' to `bool ()(int, int, int*)' in assignment
64 C:\Documents and Settings\***\main.cpp expected `(' before numeric constant
68 C:\Documents and Settings\***\main.cpp assignment of function `bool dateIsValid(int, int, int*)'
68 C:\Documents and Settings\***\main.cpp cannot convert `bool' to `bool ()(int, int, int*)' in assignment

I appreciate all help and advice. I don't have time to rewrite the whole thing (studying for mid terms), I only really need help with that function determining the validity of the date or if you see anything else that will cause incorrect results. If you want to offer other tips adressing the style or the rest of the program I appreciate it, but won't really be able to change it unless I have to.

Thanks!!!
Errrr. I am going to guess your a VB coder normally.

Line 14 if((year%100)==0&&year>1582&&(year%400)!=0)
You should use better () to make it easier to read and ensure the compiler doesn't have any issues with it.
e.g. if( ((year%100) == 0) && (year>1582) && ((year%400)!=0) )

Line 29: if(0<month<13)
That sort of comparison doesn't work.
Use a proper comparison.
e.g if( (0<month) && (month<13) )

Line 32: if 0<day<=days[month]
Omg, WTF? See above.

Line 59: if (valid=false) = is an assignment operator, this would always be True. You need to use comparison operator ==.

Line 8: bool isLeapYear(int year)
You are not returning a value, there is no return X;. So your function will never work.

Line 19: int findDaysBefore(int month)
Same as above, no return

Line 27: bool dateIsValid(int month, int day, int days[])
Same as above. C++ does not automatically give you a variable named the same as the function. You need to return the value yourself. So there is no variable called dateIsValid like their would be in Visual Basic.

Line 41: int main()
Again, Same as above. No return
Last edited on
Also the month array is not right when you are checking the date is valid:
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
because this will be considered as being numbered from 0 to 11 .
so for example if the month is 1 (january) then days[1] will be 28
so the dateIsValid function won't work.
Thanks for all your help, folks. That should be enough to get me running. Cheers!
Hey again, everything is working now except my function evaluates April 31 as a date that exists.
1
2
3
4
5
6
7
8
9
bool dateIsValid(int year, int month, int day, int days[], int ly)
{
     bool valid=false;
     if( ((0<month) && (month<13)) && ((0<day) && (day<=days[month-1])) )
     valid=true;
     if ( (month=2) && (day=29) && (ly==1) )
     valid=true;
     return valid;
}


The array for days in each month is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    while (fin >> y >> m >> d)
    {
          fout << "For an input date of" << setw(5) << y << setw(3) << m << 
          setw(3) << d << ", " << "the total number of Days from Jan 1 is: ";
          
          ds=findDaysBefore(m)+d;
          ly=isLeapYear(y);
          valid=dateIsValid(y, m, d, days, ly);
          
          if (valid==false)
          fout << "Date is invalid.\n";
          else if (m>2)
          fout << ds+ly <<"\n";
          else
          fout << ds <<"\n";
    }   


Back in a couple hours, have extra lecture in 10 minutes. Thanks for any and all help!
Last edited on
It is not validating April 31 as a valid date:
The problem is line 6 of the dateIsValid()function.
You have used assignment = instead of equality ==
This means that it make any month or day combination valid if the year is a leap year.

It should read:
if ( (month==2) && (day==29) && (ly==1) )

ps. why start using ly as an integer when it started as a bool?
Topic archived. No new replies allowed.