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();
	
	
	
	
}

Let's try to use pencil and paper. Say user enters pDate with value of 20101231, where the first four digits on the left are the year (2010), the next 2 digits to the right are the month (12) and the last 2 digits going right (31) are the day. If that's the format you are looking for in terms of input then you might want to use type long for date and pDate as the upper limit of size for type int is often something like 35000.

Assuming that you have a valid date available (say 20101231) then using your protocol:
year = 20101231/10000 = 2010;
so far so good. Next;
month = 20101231 % (10000/100)
which probably won't work because % works with type ints, but not type longs.
But let's say it does work, then:
month = 20101231 % (10000/100) = 20101231 % 100 = 31;
which works great for day, but not so hot for month. So how to get the correct values into the correct variables? To do that I would probably try:

long date = 20101231;
int year = (int)(date/YEARFINDER); //2010
int temp =(int)(date - (year * YEARFINDER)); //1231;
int month = temp/MONTHFINDER; //12
int day = temp % MONTHFINDER; //31

You can sharpen your pencil and do similar paperwork if this isn't what you had wanted.

To allow user to enter new data you can use a loop controlled by a sentinnel varible:

bool sentinnel = true;
while(sentinnel)
{	
  int ok = verifyDate(pDate);		
   if(ok)			
    date=pDate;	
   else			
   {
     cout<<"ERROR,enter new value"<<endl;
     cin >> pDate; 
   }

Oh yeah, in the while loop controlled by a sentinnel variable you need to change the valeue of the sentinnel to false when conditions are appropriate to exit the loop. Sorry about that.

Edited 6 Years Ago by Lerner: n/a

Hi,that code just solved one part of the problem.

The code you provided has helped me clear the date verification part itself. But now i have to check for the input of the date itself. In this section

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);
		}

The problem here is that it doesnt loop back into inputting the character again. Any help is much appreciated

I wouldn't use gets_s(), can't say as I've heard of it, though it may be a valid input method I don't know about. I'd use >> or getline(). And rather than atoi() I'd use isdigit() to check each char of the string to be sure they are digits. Then I'd do a string conversion to int, though I'll have to look up whethr strstr or stringstream is the appropriate method for the type of string you are using and I have no idea what the myFav[] stuff is about.

1) declare mystr
2) get data from user and store in mystr
3) loop through each char of mystr to be sure they are all digits
4) if loop is completed and all characters determined to be valid, then I would convert mystr to an int.

myFav[] is to store each successful entry into a booklist(inventory of sorts). But hey i didnt realise that atoi doesnt recognise errorneous inputs such as qwer1234, thanks for the advice.

For your above im guessing declaring mystr as an array, then manually checking each element for int input? Maybe you can post some psuedocode so i can understand better.

Once again, thank you for your advice

This article has been dead for over six months. Start a new discussion instead.