Hi guys, i just written a simple program which requires some date verification. However im facing some trouble with the input
Problem 1: if the input has more than 8 chars, garbage characters gets printed
Problem 2: if input contains invalid values(like negative), error gets printed
Problem 3: if input contains characters,( like abc), error occurs.
I was wonderin is there a function out there that can solve this? Pardon my beginner skills as i just started learning. My code is written below
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Employee
{
private:
int idNum;
int birthDate;
int hireDate;
int verifyDate(int);
public:
void display();
void setIdnum(int);
void setbirthdate(int);
void sethireDate(int);
};
void Employee::sethireDate(int date)
{
int ok = verifyDate(date);
if(ok)
hireDate=date;
else
cout<<"error";
}
int Employee::verifyDate(int date)
{
const int YEARFINDER = 10000;
const int MONTHFINDER=100;
const int EARLYYEAR=1900;
const int LATEYEAR = 1992;
const int LOWMONTH =1;
const int HIGHMONTH = 12;
int year = date/YEARFINDER;
int month = date % YEARFINDER/MONTHFINDER;
int day = date%YEARFINDER*MONTHFINDER;
int ok =1;
if (year<EARLYYEAR||year>LATEYEAR)
ok=0;
if(month<LOWMONTH||month>HIGHMONTH )
ok = 0;
return ok;
}
Main
int main()
{
Employee aWorker;
int id,birth,hire;
cout<<"Enter employee ID";
cin >>id;
cout<<"Enter employee birthdate in the format yyyymmdd";
cin>>birth;
cout<<"enter employee hire date";
cin>>hire;
aWorker.setbirthdate(birth);
aWorker.sethireDate(hire);;
aWorker.display();
system("pause");
}
These are where the problem lies. I post the entire code if u guys request