Requirements Write a function string get_date(int day) that will return a string
ID: 3591126 • Letter: R
Question
Requirements
Write a function string get_date(int day) that will return a string indicating the month and day of the year based on an integer input. For this function to return a string that is a combination of a month (a string) and a day (an integer), you will have to use the to_string() function
In the function, declare a string array as below
In the function, declare an int array as below
In main, ask the user to enter an integer between 1 and 365
Display to the user the month and day by calling the get_date() function. (See interaction)
Explanation / Answer
#include <iostream>
using namespace std;
string get_date(int day)
{
// more code here
string months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month=0, day_of_month=1;
int previous = 0, present = 0;
int sumDays = 0;
while(sumDays < day) {
sumDays = sumDays + days[month];
month++;
}
day_of_month = day - ( sumDays - days[month]);
// to use to_string(), see the compiler requirements below
// to_string() is a library function
// you do not need to create it
return months[--month] + " " + to_string(day_of_month);
}
int main()
{
int n;
cout<<"Enter an integer between 1 and 365: "<<endl;
cin >> n;
cout<<get_date(n)<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.