Question

In: Computer Science

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

I need to make this program which supposed to accept the date form the user in format of YYYY-MM-DD and output the corresponding date 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't find.

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

#include<iostream>
#include<string>
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 correct format
   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 day from 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 and day to integers
       yearNum = stoi(inputYear);
       monthNum = stoi(inputMonth);
       dayNum = stoi(inputDay);

       // Determine the month name
       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 number is 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;
}

Solutions

Expert Solution

#include<iostream>
#include<string>

using namespace std;
int main()
{
const unsigned int VALID_DASH_INDEX_1 = 4;
const unsigned int VALID_DASH_INDEX_2 = 7;
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, inputDateLength;
int invalidCharIndex;
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 correct format
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 day from the input date
inputYear = inputDate.substr(0, dashIndex1);

inputMonth = inputDate.substr(dashIndex1 + 1, dashIndex2 - dashIndex1 - 1);

inputDay = inputDate.substr(dashIndex2+1, inputDateLength - dashIndex2 );

// Convert the year, month and day to integers
yearNum = stoi(inputYear);
monthNum = stoi(inputMonth);

dayNum = stoi(inputDay);
// Determine the month name
switch (monthNum) {
case 1:
monthName = "January";
maxValidDayNum = 31;
break;
case 2:
monthName = "February";
maxValidDayNum = 29;
break;
case 3:
monthName = "March";
maxValidDayNum = 31;
break;
case 4:
monthName = "April";
maxValidDayNum = 30;
break;
case 5:
monthName = "May";
maxValidDayNum = 31;
break;
case 6:
monthName = "June";
maxValidDayNum = 30;
break;
case 7:
monthName = "July";
maxValidDayNum = 31;
break;
case 8:
monthName = "August";
maxValidDayNum = 31;
case 9:
monthName = "September";
maxValidDayNum = 30;
break;
case 10:
monthName = "October";
maxValidDayNum = 31;
break;
case 11:
monthName = "November";
maxValidDayNum = 30;
break;
case 12:
monthName = "December";
maxValidDayNum = 31;
break;
default:
inputDateValid = false;
}

// Check whether the day number is 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;
}

SCREEN SHOT :

OUTPUT:

Missed break statement at case 8 : so u got september .


Related Solutions

I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
Write program that prompts the user to enter a person's date of birth in numeric form...
Write program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle...
Using MatLab Write a program which will: a. Accept two numbers from the user m and...
Using MatLab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Accept from the user two row numbers. Multiply the two rows (element by element) and find the sum. Print the result. d. Accept from the user two column numbers. Add the two columns (element by element) and find the sum....
**I need to make this program to make sure that the numbers are unique. In another...
**I need to make this program to make sure that the numbers are unique. In another word, if the number has been generated and appear early, it should continue to find the next number until the number is unique. Hint: use the array to check whether the number has been generated. #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int n, low, high; cout << "Enter how many random numbers you would like to see:...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
I need the program to continue asking the user for a positive value and NOT skip...
I need the program to continue asking the user for a positive value and NOT skip to the value for store 2 if a negative value is entered. This is the code so far: Thanks public static void main(String[] args) {           Scanner input = new Scanner(System.in);//scanner        int Store [] = new int[5];//array for five int               for(int i=0;i<5;i++) { // loops until five inputs are entered            System.out.println("Enter today's sale...
i need to post a file to a url. The "Content-Type: multipart/form-data" and it "accept: application/json"...
i need to post a file to a url. The "Content-Type: multipart/form-data" and it "accept: application/json" i need to upload a file, three strings. It needs to be in Java, but i keep getting issues.
I need a program which tells me if three individual straight line segments form a plane...
I need a program which tells me if three individual straight line segments form a plane two-dimensional triangle. All three segments will have positive lengths. For example, the three segments 2.5, 3.4 and 5.3 will form a triangle but 1.7, 2.4 and 7.5 will not. If the user enters a zero value, it will be rejected. But if a negative number is entered, it will be changed to the corresponding positive value. Please keep in mind that the length of...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Write a program that checks whether or not a date entered in by the user is...
Write a program that checks whether or not a date entered in by the user is valid. Display the date and say if it is valid. If it is not valid explain why. The input may be in the format mm/dd/yyyy, m/d/yyyy, mm/d/yyyy, or m/dd/yyyy. A valid month (mm) must be from 1 to 12 A valid day (dd) must be from 1 to the appropriate number of days in the month April, June, September, and November have 30 days...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT