0

I wanted to do a simple date validation to disallowed string from entering when prompt. Pls guide me. Tks.

Eg,
"Enter the publish date in DD/MM/YYYY : ghghjb
"Sorry, pls enter in DD/MM/YYYY"
"Enter the publish date in DD/MM/YYYY :

# include<iostream>
# include<string>

using namespace std;

class date
{
	public : char publish_date[11];
};

date list[2];

void printbook();

int main()
{
	
	char ch;
	for(int i = 0; i < 2; i++)
	{
		
		cout << "Enter the publish date in DD/MM/YYYY : ";
		cin >> list[i].publish_date;
	
		
	}

	cout << "Press 'L' to view the list of books...";
	cin >> ch;

	if(ch == 'l' || ch == 'L')
	{
		printbook();
	}
}



void printbook()
{
	for(int i = 0; i < 2; i++)
	{
		cout << "Book Publishing date : " << list[i].publish_date << endl << endl;
	}
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Taywin
0

What you may do is to accept the user input as string, and then verify it before you put it in your date variable. If the input string is invalid, keep looping until you get the right format. Your should not use cin>>list.publish_date right away but rather put it in another string for validation first.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.