I am supposed to make a program for class that displays the date that you enter
ID: 3534981 • Letter: I
Question
I am supposed to make a program for class that displays the date that you enter only if you have it in the correct format which is 02/15/09 for example well that part of the program is working but also when you enter the format correctly it is supposed to display the date you entered as 02/15/2009 rather than as you entered which would be 02/15/09 and it does not actually display 2009 only 09 and I can not figure out what I did wrong everything looks correct to me but since it wont display the date back to you with a 20 in front of the year you entered I know something is not right and I would really appreciate any help I could get here is the code for the program.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Declare Variables
string date = "";
int subLocation = 0;
//Recieve input
cout << "Enter the date using the format mm/dd/yy (type 'end' to stop) : ";
getline (cin, date);
while (date != "end")
{
//Check for correct format
if (date.length () != 8 ||
date.find ("/", 0) != 2 ||
date.find ("/", 3) != 5)
{
cout << "Incorrect input." << endl;
}
else
{
//Display the date entered
cout << date.insert (6, "20") << endl;
} //end if
//Recieve another input
cout << "Enter the Date using the format mm/dd/yy (type 'end' to stop) ; ";
getline (cin, date);
} //end while
return 0;
system ("pause");
} //end of main function
Explanation / Answer
#include<iostream.h>
void main()
{
int d,m,y;
cout<<"enter date in dd/mm/yy format";
cin>>d>>m>>y;
clrscr();
int a,b;
a=y0;
b=y/100;
if(a<15)
{
y=2000+a;
cout<<"date is "<<m<<"/"<<d<<"/"<<y;
}
else
{
y=b*1000+a;
cout<<"date is "<<m<<"/"<<d<<"/"<<y;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.