Write a commented C++ program to re-format dates. Prompt the user to input a dat
ID: 3794318 • Letter: W
Question
Write a commented C++ program to re-format dates. Prompt the user to input a date in the form:
Display the date in each of the following formats:
(a)
Write commented C++ code which will use getline(cin,someString) to get a string from the user (in the form shown above), and then extract the day, month, date, and year into separate string variables. To do this you will have to search the input string for substrings or characters that will uniquely identify each portion of the string you plan to extract, in order. Test your code with various different dates to make sure the extraction process is working properly.
(b)
Write commented C++ code which will use the strings extracted in part (b) to write out the date in the formats given. You will need to employ substring functions for the second format. For the third format, you will need to convert from the month in string format to the numerical value for that month; for example,“January” should be converted to “01”. In order to accomplish this, you are provided with the file months.txt containing the months and their corresponding numbers, for example 01January 02February etc. Use getline(yourFile,yourString) to read in a string from the file, and use the yourstring.find() function to search for the month and extract the corresponding number which immediately precedes it.
For format 3, you should also make sure that single digit days or months are formatted in a width field of 2, that is, your code should output "02" instead of "2" for the second of January.
(c)
Test your program by trying a number of different input combinations, including different months, days, and days of the week. See what happens if the user inputs non-well-formed input, for example if the user forgets one part of the input, adds or forgets commas or spaces (or writes "May 5th" for example), enters “Blurnsday, Decembruary 39, 20112” or even something like “This program isn’t working for me, help!”
(d)
Speculate on how you might check to make sure the input is well-formed, how to handle non-well formed input, and even how to ensure that the day of the week is correct.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;
//Structure for storing month data , name : like January , number : 1
struct month{
short number;
string name;
};
//Array for 12 months
struct month months[12];
//Strore Day Names for Verification
string days[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
//Method to validate Day
bool isValidDay(string input);
//Method to get Month Number like January ==>1
int getMonthNumber(string month);
//Method to load months data from file
void loadMonthsData();
int main(int argc, char *argv[])
{
string input,day,month;
int date,year;
loadMonthsData();
cout << "Enter Date in <Day, Month Date , Year> format , for exmaple: 'Sunday, January 2, 2010' " << endl;
std::getline(std::cin, input);
char *dup = strdup(input.c_str());
char * pch = strtok (dup," ,");
int tokIdx = 0;
while (pch != NULL)
{
switch(tokIdx){
case 0:
day.assign(pch,strlen(pch));
break;
case 1:
month.assign(pch,strlen(pch));
break;
case 2:
date = atoi(pch);
break;
case 3:
year = atoi(pch);
break;
default:
break;
}
pch = strtok (NULL, " ,");
tokIdx++;
}
free(dup);
if(day.empty() || !isValidDay(day)){
cout << "You entered input if not in correct format";
return 0;
}
if(month.empty() || getMonthNumber(month) == -1){
cout << "You entered input if not in correct format";
return 0;
}
if(!(date >0 && date < 32)){
cout << "You entered input if not in correct format";
return 0;
}
if(year < 1990 || year > 2050){
cout << "You entered input if not in correct format";
return 0;
}
//Display in Format 1 <January 2 was a Sunday in 2010 >
cout<<month<< " "<<date<<" was a "<<day<<" in "<<year<<endl;
//Diaplay in Format 2 <Sun, Jan 2 ’10>
cout<<day.substr(0,3)<<", "<<month.substr(0,3)<<" "<<date<<" `"<<(year%1000)<<endl;
//Diaplay in Format-3 <2010-01-02>
printf("%d-%02d-%02d ",year,getMonthNumber(month),date);
return 0;
}
/**
* Method to validate Day
* input : Day in String Format , like Sunday
* output : true/false
*/
bool isValidDay(string input){
bool found = false;
for(int i = 0 ; i < 7 ; i++){
if(days[i].compare(input) == 0){
found = true;
break;
}
}
return found;
}
/**
* Method to get Month Number like January ==>1
*/
int getMonthNumber(string month){
int monthNum = -1;
for(int i = 0 ; i < 12 ; i++){
if(months[i].name.compare(month) == 0){
monthNum = months[i].number;
break;
}
}
return monthNum;
}
/**
* Method to load months data from file
*/
void loadMonthsData(){
std::string str,month_str;
std::ifstream infile;
infile.open("months.txt");
short monthCount = 0;
while(infile >> month_str)
{
if(month_str.at(0) == '0'){
months[monthCount].number = atoi(month_str.substr(1,1).c_str());
}
else{
months[monthCount].number = atoi(month_str.substr(0,2).c_str());
}
months[monthCount].name.assign(month_str.substr(2));
monthCount++;
}
infile.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.