Hi guys, im having problems verifying date. My exact problem is that even if user inputs an incorrect condition, the system still stores it into the class. My question is, where do i put the conditions so that the error gets printed AND the user gets to reinput that value again.my code is as follows
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class bookInput
{
private:
string title;
int date;
int verifyDate(int);
public:
void setTitle(string);
void setDate(int);
void displaybook();
};
void bookInput::setTitle(string booktitle)
{
title = booktitle;
}
void bookInput::setDate(int pDate)
{
date = pDate;
int ok = verifyDate(date);
if(ok)
date=pDate;
else
{cout<<"ERROR,enter new value"<<endl;
}
}
void bookInput::displaybook()
{
cout<< "Date Published:"<< date << " Title: " << title<< endl;
}
int bookInput::verifyDate(int date)
{
const int YEARFINDER = 10000;
const int MONTHFINDER=100;
const int EARLYYEAR=1900;
const int LATEYEAR = 2010;
const int LOWMONTH =1;
const int HIGHMONTH = 12;
int year = date/YEARFINDER;
int month = date % YEARFINDER/MONTHFINDER;
int day = date%MONTHFINDER;
int ok =1;
if (year<EARLYYEAR||year>LATEYEAR)
ok=0;
if(month<LOWMONTH||month>HIGHMONTH )
ok = 0;
if(day<0||day>31)
ok=0;
return ok;
}
void printbook()
{
const int MAX=3;
bookInput myFav[MAX];
char mystr[100];
char selection;
string Title, Title1,n;
int date,length,x,b;
for(x=0; x < MAX; ++x)
{
cout<< "Enter book title #" << (x+1) << "\n ";
cin >> n;
getline (cin, Title1);
Title = n + Title1;
myFav[x].setTitle(Title); //storing Title into setTitle
cout << " Enter the published date in YYYYMMDD format:"<<endl ;
gets_s(mystr);
length=strlen(mystr); //calculates length of string
b=atoi (mystr); //calculates whether is a character or integer
if ((length==8)&&(b!=0)) //length must be 8 and an integer
{
stringstream(mystr) >> date;
myFav[x].setDate(date);
}
else //everything else
cout<<"Error";
}
cout<<endl<< " Book list:"<<endl;
for (x=0; x<MAX; ++x)
myFav[x].displaybook();
}