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

Recommended Answers

All 5 Replies

By looking at your post all that I can say is as the writer of the code you are at your will to limit the input to any way by putting checks where ever necessary.And your problems :

>>Problem 1: if the input has more than 8 chars, garbage characters gets printed
let your variable be a char array of n if you want to limit it to n

>>Problem 2: if input contains invalid values(like negative), error gets printed
You can do something like

int input;
cin >> inp;
while(inp<0)
{
     cout<<"Please Enter a positive value :"<<endl;
     cin>>inp;
}

>>Problem 3: if input contains characters,( like abc), error occurs.
Check for those characters and ask the user to input proper values.Or you can intimate the user before hand that he should feed in only some particular characters and then deploy your check also.

i was thinking along these lines

bool b
b=isdigit(birth)
cin>>birth

while (b=false)
{
cout<<"Please Enter a positive value :"<<endl;
     cin>>birth;
}

However if i put it just after birth, it still writes the data into the class, which is something i want to prevent. Any help would be appreciated

while (b=false)

You are using the assignment operator here. This would assign the value false to b and continue in the loop.
Rather try an use:

while(b==false){
//code to be used
}

Rather use this code:

bool b;
cin>>birth;  //remember to put in ';'
b=isdigit(birth);
while (b!=false)
{
cout<<"Please Enter a positive value (a digit):"<<endl;
     cin>>birth;
     b=isdigit(birth);
}

Having trouble with the above code, it seems that the input cannot exceed int 256. Problem with the isdigit?

nvm solved it by putting in unsigned character

the issue is, where do i put the conditions in the int main()? because no matter what errorneous input i put in, it continues to input it in the class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.