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 66 67 68 69
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <limits> // <--- For std::cin.ignore.
#include <chrono> // <--- For the sleep code used in checking for file not open.
#include <thread> // <--- For the sleep code used in checking for file not open.
// The program is to have five functions including main.
int numberOfDaysInMonth(int, int);
bool dateIsValid(int, int, int);
bool isALeapYear(int year, int month);
void dayOfTheYear(int, int, int);
bool readDateFromFile(std::ifstream&, std::string& date); // <--- Changed.
bool SplitDate(std::string date, int& year, int& month, int& day); // <--- Added, but could be done in main.
bool ValidateMonthAndDay(const int year, int& month, int& day);
int main()
{
// Always initialize your variables.
int year{ 0 }, month{ 0 }, day{ 0 };
int daysInMonth{ 0 };
//char c; // <---Not used yet.
std::string date{ "2017-09-08" };
bool validInput = false;
bool validRead{ true }; // <---Added.
bool validDate{ false }; // <--- Added.
bool validDay{ false }; // <--- Added.
// Better ways to do this, but good for learning.
std::ifstream inFile;
std::string iFileName{ "datefile.txt" };
inFile.open(iFileName);
// Always a good idea to make sure the file has opened.
if (inFile.fail())
{
std::cout << "The file " << iFileName << " did not open." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Requires header files "chrono" and "thread".
exit(1); // <--- Because there is no point in contiueing.
}
//************** For User Input *********************
//std::cout << "\n Enter a date: ";
////std::cin >> date;
//std::cout << date << std::endl; // <--- Used for testing. Comment out and uncomment above for use.
//std::cout << "\n Date read: " << date << std::endl;
//validInput = SplitDate(date, year, month, day);
//************* For File Input **********************
while (readDateFromFile(inFile, date)) // <--- Used when input is form file. Will process one date at a time.
{
validInput = SplitDate(date, year, month, day);
//daysInMonth = numberOfDaysInMonth(year, month, day); // <--- Chnged. Not needed here.
validDay = ValidateMonthAndDay(year, month, day);
validDate = dateIsValid(year, month, day); // <--- Changed.
//isALeapYear(year, month, daysInMonth); // <---- Not the place to use this here.
dayOfTheYear(year, month, day);
}
}
|