YYYYMMDD format MM / DD / YYYY

closed account (9N7koG1T)
Prompt the user to enter a date in the YYYYMMDD format. Example: 10 March 2010 is 20100310.

Read the user input into a variable.

Print an error message and exit the program if the user enters a negative number.

Split the YYYYMMDD number into its respective YYYY, MM, DD pieces using integer division and the modulus operator. [hint: 20100101 / 10000 = 2010 and 20100101 % 10000 = 101]

Print a line that has the following format.
YYYYMMDD is MM/DD/YYYY

examples...

20100314 is 3/14/2010
19571105 is 11/5/1957
20380118 is 1/18/2038

Your source code file must contain a file comment block. In addition, you need to turn in a printed copy of the output generated by your program for at least three different inputs.
Last edited on
I'm very impressed, I've actually never seen this before. You literally copy / paste your exact 'word for word' instructions for your homework and post it here. Usually people are at least somewhat subtle when requesting homework help, most of the time they at least say SOMETHING in their own words. Usually I actually just post a solution with the hopes that whoever is requesting help will learn something from it and re-code it themselves using their own methods.

You're not trying to get help enough for me to care though - this is literally the laziest 'help' request I have ever seen. With that said, I truly do hope you fail your computer course and I truly do hope no one else helps you. Well actually I hope that you decide to man up and just do the work yourself and, who know, maybe you'll even enjoy it. But seeing as how I highly doubt that will be the case...
closed account (9N7koG1T)
#include <iostream>
using namespace std;


int main() {

long int longdate = 20120305, newlongdate;
int Day,Month,Year;
do {d:

cout<<" Enter Long Date (yyyymmdd) "<<endl;
cin>>longdate;

}

while(longdate<0); {

Month = (int)longdate %100; // month

longdate /=100;


Day =(int) longdate % 100; //getting date

longdate /=100;


Year =(int) longdate;
} //year

goto d;
cout<< longdate <<" is "<< Day <<" / " <<Month<<" / " <<Year<<endl;



system("pause");
return 0;
}
closed account (9N7koG1T)
I forgot to add my code sorry,, now my question is why are you so angry!just calm down, if you dont want to help then dont'...

I couldn't type some part and I thınk this was the wrong code because when I type 20101212 it shows 2010 is 12/12/2010 it is supposed to be 20101212 is 12/12/2010
Put your code in the "[ code ] and [ / code ]" brackets (without spaces or quotes) please so that we can read it more clearly. Trust me, I'm not even a little bit mad about your post - I really just found it amusing as I've never seen that before. I really do hope that anyone fails their class if they don't even put the slightest effort into it themselves, it's not fair to those that do.
closed account (9N7koG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;


    int main() {
    
    long int longdate = 20120305, newlongdate;
    int Day,Month,Year;
    do {d:
    
     cout<<" Enter Long Date (yyyymmdd) "<<endl;  
     cin>>longdate;
     
      }
     
          while(longdate<0); {
          
     Month = (int)longdate %100; // month
     
     longdate /=100;
     
     
     Day =(int) longdate % 100; //getting date
     
     longdate /=100;
     
     
     Year =(int) longdate;
     } //year
     
    
     cout<< longdate <<" is "<< Day <<" / " <<Month<<" / " <<Year<<endl;
     

     
     system("pause");
     return 0;
 }
Lots and lots are wrong with your code.

1
2
3
4
5
6
7
8
9
10
11
long int longdate = 20120305, newlongdate;
int Day,Month,Year;

do {d:
    
     cout<<" Enter Long Date (yyyymmdd) "<<endl;  
     cin>>longdate;
     
      }
     
          while(longdate<0); {


What is the point of this? std::cin is going to wait for user input so putting it in a loop is pointless in the first place. Also, the d: is pointless as it's never called and, by the way, goto statements should always be avoided so don't use those in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(longdate<0); {
          
     Month = (int)longdate %100; // month
     
     longdate /=100;
     
     
     Day =(int) longdate % 100; //getting date
     
     longdate /=100;
     
     
     Year =(int) longdate;
     } //year 


This part is the actual reason that your code is printing out the original wrong. You're dividing the original by 100 without backing it up both times. Make a backup of the original integer and then print that out at the bottom of your program to avoid this error. Think about it. You're setting Year =(int) longdate so why would you expect the two to have different results? You also don't really need to backup, you could just use proper math.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	int CurrentDate, Day, Month, Year;

	std::cout << "Please enter a date in YYYYMMDD format (Example: 20120329): ";
	std::cin >> CurrentDate;

	Day = CurrentDate % 100;
	Month = ( CurrentDate / 100 ) % 100;
	Year = ( CurrentDate / 10000 );

	if ( CurrentDate < 0 || Day < 0 || Month < 0 || Year < 0 )
	{
		std::cout << "Please enter positive numbers" << std::endl;
		return 0;
	}

	std::cout << "The original YYYYMMDD date of " << CurrentDate << " has been converted to MM / DD / YY format below" << std::endl;
	std::cout << Month << " / " << Day << " / " << Year << std::endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.