// Сделал программу для практики обработки исключений в C ++, код выполняется без ошибок, но он не откроет файл ifstream и не извлечет введенные мною даты.Infile называется "testdates" и сохраняется с помощью блокнота.
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool leapYear(int year);
class dateType {
public:
dateType();
dateType(int d, int m, int y);
dateType(const dateType &d);
void printDate(ostream &os);
void setDate(int m, int d, int y);
void checkDate(ostream &os);
protected:
int myMonth, myDay, myYear;
};
int main() {
ofstream outfile;
outfile.open("checkdates.txt");
ifstream infile;
infile.open("testdates.txt");
// ^^ Я уже создал это с датами для чтения.^^
if (infile.fail()) {
cout << "file location not found";
system("pause");
return 1;
}
outfile << "John L" << endl << endl;
cout << "John L" << endl << endl;
int d, m, y;
dateType d1;
int i = 1;
while (!infile.eof()) {
infile >> m >> d >> y;
d1.setDate(m, d, y);
d1.checkDate(outfile);
d1.checkDate(cout);
}
system("pause");
infile.close();
outfile.close();
return 0;
}
dateType::dateType() {
myDay = 1;
myMonth = 1;
myYear = 0;
}
dateType::dateType(int m, int d, int y) {
myDay = d;
myMonth = m;
myYear = y;
}
dateType::dateType(const dateType &rhs) {
myDay = rhs.myDay;
myMonth = rhs.myMonth;
myYear - rhs.myYear;
}
void dateType::printDate(ostream &os) {
ofstream outfile;
outfile << "Date: " << myMonth << "/" << myDay << "/" << myYear << endl;
cout << "Date: " << myMonth << "/" << myDay << "/" << myYear << endl;
}
void dateType::setDate(int m, int d, int y) {
myDay = d;
myMonth = m;
myYear = y;
}
void dateType::checkDate(ostream &os) {
class badDate {};
class badDay1 {};
class badDay2 {};
class badDay3 {};
class badDayFeb {};
class badDayLeap {};
class badMonth1 {};
class badMonth2 {};
class negYear {};
class goodDate {};
try {
if ((myMonth >= 1 && myMonth <= 12) && myDay >= 1 && myYear >= 0) {
if (myDay > 31)
throw badDay1();
else if ((myMonth == 4 || myMonth == 6 || myMonth == 9 || myMonth == 11) && myDay > 30)
throw badDay2();
else if (myMonth == 2 && myDay > 28 && !leapYear(myYear))
throw badDayFeb();
else if (myMonth = 2 && myDay > 29 && leapYear(myYear))
throw badDayLeap();
else throw goodDate();
}
else if (myDay < 1)
throw badDay3();
else if (myMonth < 1)
throw badMonth1();
else if (myMonth > 12)
throw badMonth2();
else if (myYear < 0)
throw negYear();
}
catch (badDay1) {
os << "Invalid day: no month has more than 31 days " << endl;
}
catch (badDay2) {
os << "Invalid day: Month only has 30 days " << endl;
}
catch (badDay3) {
os << "Invalid day: Days cannot be less than 1" << endl;
}
catch (badDayFeb) {
os << "Invalid day: february only has 28 years unless it is a
leap year. " << endl;
}
catch (badDayLeap) {
os << "Invalid day: February has 29 days on a leap year " <<
endl;
}
catch (badMonth1) {
os << " Invalid month: Month cannot be less than zero " << endl;
}
catch (badMonth2) {
os << "Invalid month: There are only 12 months " << endl;
}
catch (negYear) {
os << "Invalid year: A year cannot be negative " << endl;
}
catch (goodDate) {
os << " This date works! " << endl;
}
}
bool leapYear(int y) {
return(y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
// в результате получится «местоположение файла не найдено». Я также пробовал полный путь, не работает.
«C: \ Users \ phill \ source\ repos \ ConsoleApplication25 \ ConsoleApplication25 \ testdates.txt "
это то, что файл содержит
" testdates.txt "11 27 1998 -50 10 1990 9 -200 1000 4 14 -1 139 1452 12 32 2018 2 29 2018 2 29 2020 2 20 2020 11 31 2009