Write a program which prompts the user for the date in the form yyyymmdd which i
ID: 3545638 • Letter: W
Question
Write a program which prompts the user for the date in the form yyyymmdd which is stored in a variable of type long. Your program will then extract the year from the long variable, the month and the day and output them with appropriate messages. You are to write 2 versions of this program.
Version 1:
Write three functions which have one argument, the long entered by the user (pas byvalue), and each return one part of the date.
Version 2:
Write one void function which has 4 arguments, the long entered by the user (pass by value) and three integer arguments (pass by reference) which will be used to return the 3 parts of the date.
The output produced by both versions should be identical. Figure 3 illustrates a sample output screen. Again the format of your output does not have to be the same as the one in figure 3, but it should contain the same information.
Hint: the date is entered as a long (not a String). You will need to use the integer division and the remainder operations to extracts the different parts of the date.
Assume the user enters a valid date.
Explanation / Answer
/****************version1*********************/
#include<iostream>
using namespace std;
int year(long a)
{return a/10000;
}
int month(long a)
{return (a%10000)/100;
}
int day(long a)
{return a%100;
}
int main()
{long b;
int y,m,d;
cout<<"Welcome to Nancy's Date Info Extractor Version 1"<<endl;
cout<<"------------------------------------------------"<<endl<<endl;
cout<<"Enter a date in the form yyyymmdd : ";
cin>>b;
y=year(b);
m=month(b);
d=day(b);
cout<<"The year is : "<<y<<endl;
cout<<"The month is : "<<m<<endl;
cout<<"The day is : "<<d<<endl;
cout<<endl<<"Thank you for using the Date Extractor Program. ";
return 0;
}
/**********************version2************************/
#include<iostream>
using namespace std;
void fun(long a,int *year,int *month,int *day)
{*year=a/10000;
*month= (a%10000)/100;
*day=a%100;
}
int main()
{long b;
int y,m,d;
cout<<"Welcome to Nancy's Date Info Extractor Version 2"<<endl;
cout<<"------------------------------------------------"<<endl<<endl;
cout<<"Enter a date in the form yyyymmdd : ";
cin>>b;
fun(b,&y,&m,&d);
cout<<"The year is : "<<y<<endl;
cout<<"The month is : "<<m<<endl;
cout<<"The day is : "<<d<<endl;
cout<<endl<<"Thank you for using the Date Extractor Program. ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.