I need to make this program which supposed to accept the date form the user in...

70.2K

Verified Solution

Question

Programming

I need to make this program which supposed to accept the dateform the user in format of YYYY-MM-DD and output the correspondingdate in its expanded format.
output should be something like: Enter the date in YYYY-MM-DD:2020-10-03
October o3, 2020

(in C++)

I have this program but there are some errors that I couldn'tfind.

// Lab3_Project_jk.cpp : This file contains the 'main' function.Program execution begins and ends there.
//

#include
#include
using namespace std;
int main()
{
   const unsigned int VALID_DASH_INDEX_1 = 4;
   const unsigned int VALID_DASH_INDEX_2 = 6;
   const unsigned int VALID_DATE_LENGTH = 10;
   const unsigned int MIN_VALID_DAY_NUM = 1;
   string inputDate, inputYear, inputMonth, inputDay,monthName;
   unsigned int dashIndex1, dashIndex2, invalidCharIndex,inputDateLength;
   unsigned int yearNum, dayNum{}, monthNum,maxValidDayNum = 31;
   bool inputDateValid = true;

   // Get a date from the user
   cout << \"Enter date in YYYY-MM-DD format:\";
   getline(cin, inputDate);

   // Check whether the input date has the correctformat
   dashIndex1 = inputDate.find(\"-\");
   dashIndex2 = inputDate.find(\"-\", dashIndex1 +1);
   invalidCharIndex =inputDate.find_first_not_of(\"0123456789-\");
   inputDateLength = inputDate.size();
   if ((dashIndex1 == VALID_DASH_INDEX_1)&&
       (dashIndex2 == VALID_DASH_INDEX_2)&&
       (invalidCharIndex == string::npos)&&
       (inputDateLength ==VALID_DATE_LENGTH))
   {
       // Extract the year, month and dayfrom the input date
       inputYear = inputDate.substr(0,dashIndex1);
       inputMonth =inputDate.substr(dashIndex1 + 1, dashIndex2 - dashIndex1 -1);
       inputDay =inputDate.substr(dashIndex2, inputDateLength - dashIndex2 - 1);

       // Convert the year, month andday to integers
       yearNum = stoi(inputYear);
       monthNum = stoi(inputMonth);
       dayNum = stoi(inputDay);

       // Determine the monthname
       switch (monthNum) {
       case1:
           monthName =\"January\";
           maxValidDayNum =31;
           break;
       case2:
           monthName =\"February\";
           maxValidDayNum =29;
           break;
       case3:
           monthName =\"March\";
           maxValidDayNum =31;
           break;
       case4:
           monthName =\"April\";
           maxValidDayNum =30;
           break;
       case5:
           monthName =\"May\";
           maxValidDayNum =31;
           break;
       case6:
           monthName =\"June\";
           maxValidDayNum =30;
           break;
       case7:
           monthName =\"July\";
           maxValidDayNum =31;
           break;
       case8:
           monthName =\"August\";
           maxValidDayNum =31;
       case9:
           monthName =\"September\";
           maxValidDayNum =30;
           break;
       case10:
           monthName =\"October\";
           maxValidDayNum =31;
           break;
       case11:
           monthName =\"November\";
           maxValidDayNum =30;
           break;
       case12:
           monthName =\"December\";
           maxValidDayNum =31;
           break;
       default:
           inputDateValid =false;
       }

       // Check whether the day numberis valid
       if ((dayNum < MIN_VALID_DAY_NUM)&& (dayNum > maxValidDayNum)) {
           inputDateValid =false;
       }
   }
   else {
       inputDateValid = true;
   }
   // Output the date in expanded format
   if (inputDateValid) {
       cout << monthName << \"\" << dayNum << \", \" << inputYear << \"\"<< endl;
   }
   else {
       cout << \"Invalid date format\"<< endl;
   }
   return 0;
}

Answer & Explanation Solved by verified expert
4.1 Ratings (648 Votes)
includeincludeusing namespace stdint mainconst unsigned int VALIDDASHINDEX1 4const unsigned int    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students