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

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....
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 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.
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...
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...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
I'm supposed to create a c++ program which is supposed to take a sentence as an...
I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter. ex. Enter a phrase: It's a hard knock life A2 I2 K2 C1 D1 E1 F1 H1 L1 N1 O1 R1 S1 T1 I was also given a recommended code to use as a bubble sort procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped =...
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
In matlab.write a program that converts a numerical date given by the user into a month,...
In matlab.write a program that converts a numerical date given by the user into a month, day, and year. Specifically, the user should input a sequence of eight numbers 05141956. the program should print out the same date in complete format. In this example it would print "The date is May 14th, 1956". Notice the "th" addition after the day. This changes depending on the number. Make sure this feature is included. MATLAB has built-in date conversion functions. You cannot...
DO IN C++ Secret Codes! Create a program that will accept a message from the user...
DO IN C++ Secret Codes! Create a program that will accept a message from the user and either encrypt ordecrypt it with the following algorithms: To encrypt: - get the character you wish to encrypt - find the index of that character in the alphabet array - add the shift offset to the index - add the increment to the index - "wrap around" the new index so the result is between 0 and 29 - find the character at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT