Question: Modify the C++ program so that it asks the user whether he or she wants to display the formatted date using either slashes (/) or
Modify the C++ program so that it asks the user whether he or she wants to display the formatted date using either slashes (/) or hyphens (-). Save and then run the program. Test the program by entering 4 as the month, 9 as the day, 2017 as the year, and a - (hyphen) as the separator. The program displays 4-9-2017 on the computer screen. Run the program again. Enter 12 as the month, 21 as the day, 2016 as the year, and a / (slash) as the separator. The program displays 12/21/2016 on the computer screen. (Hint: The getFormattedDate method should receive a string that indicates whether the user wants slashes or hyphens in the date.)
.cpp
//TryThis8.cpp - displays a formatted date //Created/revised by
using namespace std;
int main() { //create a FormattedDate object FormattedDate hireDate; //declare variables string hireMonth = ""; string hireDay = ""; string hireYear = ""; //get month, day, and year cout << "Enter the month number: "; cin >> hireMonth; cout << "Enter the day number: "; cin >> hireDay; cout << "Enter the year number: "; cin >> hireYear; //use the FormattedDate object to set the date hireDate.setDate(hireMonth, hireDay, hireYear); //display the formatted date cout << "Employee hire date: " << hireDate.getFormattedDate() << endl; return 0; } //end of main function
header
//TryThis8 FormattedDate.h //Created/revised by
//declaration section class FormattedDate { public: FormattedDate(); void setDate(string, string, string); string getFormattedDate(); private: string month; string day; string year; }; //implementation section FormattedDate::FormattedDate() { //initializes the private variables month = "0"; day = "0"; year = "0"; } //end of default constructor
void FormattedDate::setDate(string m , string d, string y) { //assigns program values to the private variables month = m; day = d; year = y; } //end of setDate method
string FormattedDate::getFormattedDate() { //formats and returns values stored in the private variables return month + "/" + day + "/" + year; } //end of getFormattedDate method
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
