Input Validation

I seem to be having a validation error I believe with my code. The application is supposed to create a Date instance based on whether the inputs are valid. Here is the code files.

HEADER FILE:

enum DateFormat { mdy1, mdy2, ymd1, ymd2 };
const int MIN_YEAR = 1900;
const int MAX_YEAR = 2020;
const string monthStr[] = //alternative: const char monthStr[] [15]=
{ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December" };
const string monthStrAbbrev[] = //not strictly necessary, but helpful
{ "jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov",
"dec" };
const int monthDays[] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };



class Date
{
private:
string month;
int day;
int year;
bool isValidMonth(string);
bool isValidYear(int);
bool isValidDay(int);
public:
Date(string&,int& ,int&); // constructor
void getDate();
};



This is the test.cpp where the main function is located and the call are happening. this file is not yet complete due to the inconsistencies in my Date.cpp, I choose not to move on into it.

include <iostream>
using namespace std;
#include "Date.h"
#include <string>
#include <algorithm>
void setDateValues(string&, int&, int&);
int main()
{
string mth;
int day, yr;
setDateValues(mth, day, yr);
Date D1(mth, day, yr);
cout << "Date is:\n";
D1.getDate();

return 0;
}


void setDateValues(string& month, int& day, int& year)
{
cout << "Enter month: ";
cin >> month;
transform(month.begin(), month.end(), month.begin(), (int(*) (int)) tolower);
cout << "Enter day: ";
cin >> day;
cout << "Enter year: ";
cin >> year;
}



This is my Date.cpp file where the class implementation


int dayscount = 0; //global to save index of month match

Date::Date(string& newmonth, int& newday, int& newyear) //my constructor
{
if (isValidMonth(newmonth))
{
if (isValidDay(newday))
{
if (isValidYear(newyear))
{
month = newmonth;
day = newday;
year = newyear;
}
}

}
else
month = monthStr[0];
day = 1;
year = 2001;
}

//working on getting the three validation functions
//need to create the print function
// mdy1, mdy2, ymd1, ymd2
void Date::getDate() //pass an arguement that indicates the desired format
{
cout << month << "/" << day << "/" << year << endl;
}

bool Date::isValidMonth(string inputmonth)
{

for (int x = 0; x <= 11; x++)
if (inputmonth.compare(monthStrAbbrev[x])==0)
{
dayscount = x;
return true;
}
else
return false;

}
bool Date::isValidDay(int d)
{
if (d < 0 || d > monthDays[dayscount])
return false;
else
return true;
}
bool Date::isValidYear(int y)
{
if (y < MIN_YEAR || y > MAX_YEAR)
return false;
else
return true;
}
as in

Date(mth,day,yr);

?
Topic archived. No new replies allowed.