Computer Science, asked by abdullahabssi888, 5 hours ago

2. Write a program that converts dates from numerical month/day format to alphabetic month/day (for example 1/31 or 01/31 corresponds to January 31). The dialogue should be similar to that in Programming Project 1. You will define two exception classes, one called MonthError and another called DayError. If the user enters anything other than a legal month number (integers from 1 to 12), then your program will throw and catch a MonthError. Similarly, if the user enters anything other than a valid day number (integers from 1 to either 29, 30, or 31, depending on the month), then your program will throw and catch a DayError. To keep things simple, always allow 29 days for February

Answers

Answered by Abhisheksingh5722
0

// Header file section

#include<iostream>

#include<string>

#include<cstdlib>

using namespace std;

class MonthError(); class DayError();

//function prototype

void get input (string &input);

void Output (int month, int day); void convert (string alpha format, int &month, int day);

//main function

void main()

(

//variable declaration

string alpha_format, s2;

int imon, iday;

//function call to input alphabet format get input (alpha_format);

try

convert (alpha_format, imon, iday);

catch (MonthError)

cout<<" Error ! Input there are 1-12 months

in year: "<<endl;

system ("pause"); exit(0);

catch( DayError)

cout<<" Error! Input there are 29-31 in a

months : "<<endl;

system ("pause"); exit (0);

//function call to output data Output (imon, iday); //pause system for a while

system ("pause");void convert (string alpha_format, int &month, int sday)

char arr [10]; string sl;

int pos;

for (int i=0;i<alpha_format.length(); i++) if (alpha_format.at (i)=="/")

pos=i;

//places first sustring before / in sl sl=alpha_format.substr(0, pos); for (int i=0;i<s1.length(); i++)

arr[i]=sl. at (i);

month-atoi (arr);

string s2;

//places first sustring after in s2

s2-alpha_format.substr(pos+1, alpha_format. length ()); for (int i=0;i<s2.length(); i++)

arr[i]=s2.at (i);

day-atoi (arr);

if (month>121 |month<1)//validating month

throw MonthError(); if (day>31| |day<1)//validating day

throw DayError();

} //end convert

//displays in alphabet format void Output (int month, int day)

switch (month)

case 1: cout<<"January "<<day<<endl;

break;

case 2: cout<<"Febravary "<<day<<endl; break; case 3: cout<<"March <<day<<endl;

break; case 4:cout<<"April "<<day<<endl;

break;

case 5: cout<<"May "<<day<<endl; break;

case 6: cout<<"June "<<day<<endl;

 \\  \\

Hope you understand :)

Similar questions