USE THE PHOTOS FOR THE BASE OF THE SOLUTION verify that the user has produced ap
ID: 3844148 • Letter: U
Question
USE THE PHOTOS FOR THE BASE OF THE SOLUTION
verify that the user has produced appropriate input. For each field you extract from the user's input, verify that it is a day of the week, a month, an appropriate date, and an appropriate year.
For the day of the week, check that it ends with "day". For the month, check that it appears in the "months.txt" file. For the day, check that it is between 1 and 31 For the year, check that it is greater than 1975 and less than 2075 (or choose a different but reasonable date range) Put the user input in a loop, , so the user is asked to re-enter the date if they provide an invalid entry. You should also ask again if the user enters a correct date, and only end the program when the user enters the word "quit".
53% D OO Fido 7:23 PM urcourses.uregina.ca Fincludekiostream> #include #include using namespace std; int main() string monthstring; will hold the month lookup string string userstring; the string the user inputs START OF FILE INPUT open the file for month abbreviations ifstream monthFile: month File.open ("months .txt"); extract the string for month abbreviations getline (monthFile monthstring) //that's all we need the file for so close the file month File .close END OF FILE INPUT unsigned int spacePlace; used to report the location of the first space in a string string nextstring; used to create a trailing substring after the space string monthAbbrv; for the month abbreviation unsigned int monthLoc dor the location of the month in the string cout Please enter the date in the following format: In"; cout Monday, January 01 1985 In cout "(Please pay attention to spaces and commas) In> getline (cin, userstring); space Place (int) userstring.find //find the first space string weekDay user String substr (0,spacePlace evtract the with
Explanation / Answer
if it is not required to input space after year in the input, then remove the last space condition from the expr variable.
Sample input:
Monday, January 01, 1987
Thursday, December 31, 2074
none, February 13, 1986
Monday, none 13, 2014
Monday, January 34, 2075
Monday, January 31, 2089
invalid string
quit
Output:
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Monday
January
01
1987
January 01 was a Monday in 1987
Mon, Jan 01 '87
1987-01-01
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Thursday
December
31
2074
December 31 was a Thursday in 2074
Thu, Dec 31 '74
2074-12-31
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Invalid Entry
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Invalid Entry
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Invalid Entry
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Invalid Entry
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Invalid Entry
please enter the date in the following format:
Monday, January 01, 1985
(Please pay attention to spaces and commas)
Thank you.
#include<iostream>
#include<string>
#include<fstream>
#include <regex>
#include <cstdlib>
using namespace std;
int main()
{
string monthString;
string userString;
//monthString="01January 02February 03March 04April 05May 06June 07July 08August 09September 10October 11November 12December";
ifstream monthFile;
monthFile.open("months.txt");
getline(monthFile, monthString);
monthFile.close();
while(true)
{
unsigned int spacePlace;
string nextString;
string monthAbbrv;
unsigned int monthLoc;
cout<< "please enter the date in the following format: ";
cout<< "Monday, January 01, 1985 ";
cout<< "(Please pay attention to spaces and commas) ";
std::regex expr("([a-zA-Z]+)day, ([a-zA-Z]+) ([0-9]{2}), ([0-9]{4}) ");
getline(cin, userString);
if(userString == "quit")
break;
else if(!std::regex_match(userString,expr)) //Basic validation of the string format is done here. Data validation happens later.
{
cout << "Invalid Entry" << endl;
continue;
}
else
{
spacePlace = (int)userString.find(" ");
string weekDay = userString.substr(0, spacePlace-1);
nextString = userString.substr(spacePlace+1, userString.length());
spacePlace = (int)nextString.find(" ");
string month = nextString.substr(0,spacePlace);
nextString = nextString.substr(spacePlace+1, nextString.length());
spacePlace = (int)nextString.find(" ");
string date = nextString.substr(0,spacePlace-1);
nextString = nextString.substr(spacePlace+1, nextString.length());
spacePlace = (int)nextString.find(" ");
string year = nextString.substr(0,spacePlace);
nextString = nextString.substr(spacePlace+1, nextString.length());
int numberDate = std::atoi(date.c_str());
int numberYear = std::atoi(year.c_str());
// date should be between 1 to 31
// year should be between 1975 to 2075
// month should exist in the test file.
if(numberDate >= 1 && numberDate <=31 && numberYear > 1975 && numberYear < 2075 && (monthString.find(month,0) != std::string::npos))
{
cout << weekDay << endl;
cout << month << endl;
cout << date << endl;
cout << year << endl;
cout << month << " " << date << " was a " << weekDay << " in " << year << endl;
cout << weekDay.substr(0,3) << ", " << month.substr(0,3) << " " << date << " ";
cout << "'" << year.substr(2,2) << endl;
monthLoc = (int) monthString.find(month,0);
monthAbbrv = monthString.substr(monthLoc - 2, 2);
cout << year << "-" << monthAbbrv << "-" << date << endl;
}
else
{
cout << "Invalid Entry" << endl;
}
}
}
cout <<endl;
cout <<"Thank you. ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.