Question

In: Computer Science

Language: C++ The program I have written below needs to allow the user to enter in...

Language: C++

The program I have written below needs to allow the user to enter in their own date, however I currently can only get it to output a default date, 10/10/2018. After the user enters the date it needs to go through the exception checks to make sure it is a valid date. Afterword the user needs to be prompted to change their date.

Here is the code:

#include <iostream>

#include <cmath>

#include <fstream>

#include <cstring>

#include <sstream>

#include <vector>

using namespace std;

class Date

{

private:

   int month, day, year;

   int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

   string month_name[13] = { "","January","February","March","April","May","June","July","August","September","October","November","December" };

public:

   Date(int m, int d, int y)

   {

       if (m < 1 || m>12)

       {

           throw "Invalid month";

       }

       if (d<1 || d>days[m])

       {

           throw "Invalid date";

       }

       if (y < 1950 || y>2020)

       {

           throw "Invalid year";

       }

       month = m;

       day = d;

       year = y;

   }

   void setMonth(int m)

   {

       if (m < 1 || m>12)

       {

           throw "Invalid month";

       }

       month = m;

   }

   void setYear(int y)

   {

       if (y < 1950 || y>2020)

       {

           throw "Invalid year";

       }

       year = y;

   }

   void setDate(int d)

   {

       if (d<1 || d>days[d])

       {

           throw "Invalid date";

       }

       day = d;

   }

   void toString()

   {

       cout << month_name[month] << " " << day << ", " << year << endl;

   }

};

int main()

{
   cout << "Please enter in the date in format mm, dd, yyyy." << endl;

  
   Date d(10, 10, 2018);

   d.toString();

   try

   {

       d.setDate(44);

   }

   catch (const char* msg)

   {

       cerr << msg << endl;

   }

   return 0;

}

Solutions

Expert Solution

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
class Date
{
private:
int month, day, year;
int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
string month_name[13] = { "","January","February","March","April","May","June","July","August","September","October","November","December" };
public:
Date(int m, int d, int y)
{
if (m < 1 || m>12)
{
throw "Invalid month";
}
if (d<1 || d>days[m])
{
throw "Invalid date";
}
if (y < 1950 || y>2020)
{
throw "Invalid year";
}
month = m;
day = d;
year = y;
}
void setMonth(int m)
{
if (m < 1 || m>12)
{
throw "Invalid month";
}
month = m;
}
void setYear(int y)
{
if (y < 1950 || y>2020)
{
throw "Invalid year";
}
year = y;
}
void setDate(int d)
{
if (d<1 || d>days[d])
{
throw "Invalid date";
}
day = d;
}
void toString()
{
cout << month_name[month] << " " << day << ", " << year << endl;
}
};
int main()
{
int D,m,y;
cout << "Please enter in the date in format mm, dd, yyyy." << endl;
cin>>D>>m>>y;
try
{
Date d(D,m,y);
d.toString();
}
catch (const char* msg)
{
cerr << msg << endl;
}
return 0;

}


Related Solutions

Write a C++ program that : 1. Allow the user to enter the size of the...
Write a C++ program that : 1. Allow the user to enter the size of the matrix such as N. N must be an integer that is >= 2 and < 11. 2. Create an vector of size N x N. 3. Call a function to do the following : Populate the vector with N2 distinct random numbers. Display the created array. 4. Call a function to determines whether the numbers in n x n vector satisfy the perfect matrix...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
Write a C++ program to allow a user to enter in any positive number greater than...
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit. An abundant number is a number n...
In C++ 2. Test Scores #2 Modify the program #1 to allow the user to enter...
In C++ 2. Test Scores #2 Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT