I may as well show you what I have so far in case you're interested...
Code:
#include <iostream.h>
#include <fstream.h>
#include <APSTRING.H>
void GetDate(ifstream&, int&, int&, int&);
void print(ostream&, int, int, int, int);
bool BadDay(int, int, int);
int DaySwitch(int, int, int);
bool LeapCalc(int);
bool NotDone();
void OpenFileName(ifstream&);
int main()
{
int y, m, d, caseday;
ofstream OutFile;
OutFile.open("C:OutM");
ifstream InFile;
OpenFileName(InFile);
do
{
GetDate(InFile, y, m, d);
if (BadDay(y, m, d))
{
cout << "You have entered an invalid date."<<endl;
}
caseday = DaySwitch(y, m, d);
print(cout, y, m, d, caseday);
print(OutFile, y, m, d, caseday);
}while (NotDone());
InFile.close();
OutFile.close();
return 0;
}
void OpenFileName(ifstream&InFile)
{
apstring infilename;
cout << "Enter the input file's name: ";
cin >> infilename;
InFile.open(infilename.c_str());
}
void GetDate(ifstream&InFile, int&y, int&m, int&d)
{
while(! InFile.fail())
{
InFile>>m>>d>>y;
}
}
int DaySwitch(int y, int m, int d)
{
int dig;
dig=y+y/4-y/100+y/400+1;
if(LeapCalc(y) && (m==1 || m==2))
dig--;
switch(m)
{
case 1:
case 10:
break;
case 2:
case 3:
case 11: dig += 3;
break;
case 4:
case 7: dig += 6;
break;
case 5: dig += 1;
break;
case 6: dig += 4;
break;
case 8: dig += 2;
break;
case 9:
case 12: dig += 5;
break;
default: return 7;
}
dig += d - 1;
dig = dig%7;
return dig;
}
bool LeapCalc(int y)
{
return (y%4==0 && y%100 !=0) || (y%400==0);
}
void print(ostream& oout, int y, int m, int d, int caseday)
{
if(caseday !=7)
{
oout << m << "/" << d <<"/" << y << " is a ";
switch(caseday)
{
case 1: oout << "Sunday.";
break;
case 2: oout << "Monday.";
break;
case 3: oout << "Tuesday.";
break;
case 4: oout << "Wednesday.";
break;
case 5: oout << "Thursday.";
break;
case 6: oout << "Friday.";
break;
case 0: oout << "Saturday.";
break;
}
}
else if(caseday==7) cout << "you have entered an invalad date."<<endl;
oout << endl << endl;
}
bool BadDay(int y, int m, int d)
{
if(m==2 && !LeapCalc(y))
return !(d>0 && d<29);
else if(m==2 && LeapCalc(y))
return !(d>0 && d<30);
else if(m==4 || m==6 || m==9 || m== 11)
return !(d>0 && d<31);
else return !(d>0 && d<32);
}
bool NotDone()
{
char yn;
cout << "Another? <Y/N>: ";
cin>>yn;
cout<<endl;
return yn=='Y'||yn=='y';
}
As you can see, the user is prompted to input the name of a file that contains several dates, then the program calculates what day of the week each dates is.
Thanks for your ideas. I'll try to use an array in this program for the various dates.