469,609 Members | 1,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Is there an alternative to program a timestamp?

hi all,

i'm wondering is there any other alternative to do a timestamp which consists of date and time!! below is a program i've created but its too long and to large i would like to know other way to do it can anyone help me?

here is some part of the program i've created its impossible to show u guys the whole program as ive copy it to save in a notepad and its file is 64kb!!please tell me if there is other alternative to do this program??

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. int main()
  3. {
  4.  
  5.     int dd,mm,cent;
  6.     double yyyy;
  7.  
  8. x:  cout<<endl;
  9.     cout<<"        Please Follow The Following Instruction To Get The Valid Date"<<endl;
  10.     cout<<"--------------------------------------------------------------------------------"<<endl;
  11.     cout<<endl;
  12.     cout<<"Please Choose The Century You Want(1-4):"<<endl;
  13.     cout<<"1)19th century"<<endl;
  14.     cout<<"2)20th century"<<endl;
  15.     cout<<"3)21st century"<<endl;
  16.     cout<<"4)22nd century"<<endl;
  17.     cout<<endl;
  18.     cout<<"Century:";
  19.     cin>>cent;
  20.     cout<<"Please Enter The Last 2 Digit Of The Year You Want:";
  21.     cin>>yyyy;
  22.     cout<<"Please Enter The Month You Want:";
  23.     cin>>mm;
  24.     cout<<"Please Enter The Day You Want:";
  25.     cin>>dd;
  26.     cout<<endl;
  27.  
  28.         switch (cent)
  29.     {
  30.     case 1:
  31.         { 
  32.             cout<<"You have choose the 19th century."<<endl;
  33.             switch (mm)
  34.     {
  35.     case 1:
  36.         {
  37.  
  38.             if (yyyy>=00 && yyyy<10)
  39.             {
  40.  
  41.                 if (dd>=01 && dd<10)
  42.                 {
  43.                     cout<<"The Year You Choose Is 180"<<yyyy<<endl;
  44.                     cout<<endl;
  45.                     cout<<"Date(dd/mm/yy): 0"<<dd<<"010"<<yyyy<<endl;
  46.                 }
  47.  
  48.                 else if (dd>=10 && dd<=31)
  49.                 {
  50.                     cout<<"The Year You Choose Is 180"<<yyyy<<endl;
  51.                     cout<<endl;
  52.                     cout<<"Date(dd/mm/yy): "<<dd<<"010"<<yyyy<<endl;
  53.                 }
  54.  
  55.                 else
  56.                 {
  57.                     cout<<"The date you input is invalid. There is an error in day. Please try again."<<endl;goto x;
  58.                 }
  59.             }
  60.  
  61.             else if (yyyy>=10 && yyyy<=99)
  62.             {
  63.  
  64.                 if (dd>=01 && dd<10)
  65.                 {
  66.                     cout<<"The Year You Choose Is 18"<<yyyy<<endl;
  67.                     cout<<endl;
  68.                     cout<<"Date(dd/mm/yy): 0"<<dd<<"01"<<yyyy<<endl;
  69.                 }
  70.  
  71.                 else if (dd>=10 && dd<=31)
  72.                 {
  73.                     cout<<"The Year You Choose Is 18"<<yyyy<<endl;
  74.                     cout<<endl;
  75.                     cout<<"Date(dd/mm/yy): "<<dd<<"01"<<yyyy<<endl;
  76.                 }
  77.  
  78.                 else
  79.                 {
  80.                     cout<<"The date you input is invalid. There is an error in day. Please try again."<<endl;goto x;
  81.                 }
  82.             }
  83.  
  84.             else
  85.             {
  86.                 cout<<"The date you input is invalid. There is an error in year. Please try again."<<endl;goto x;
  87.             }
  88.             break;
  89.         }
  90.  
  91.     case 2:
  92.     {
  93.         if (yyyy>=00 && yyyy<10)
  94.         {
  95.             if (int (yyyy)%4==0)
  96.             {
  97.                 if (dd>=01 && dd<10)
  98.                 {
  99.                     cout<<"The Year You Choose Is 180"<<yyyy<<endl;
  100.                     cout<<endl;
  101.                     cout<<"Date(dd/mm/yy): 0"<<dd<<"020"<<yyyy<<endl;
  102.                 }
  103.  
  104.                 else if (dd>=10 && dd<=29)
  105.                 {
  106.                     cout<<"The Year You Choose Is 180"<<yyyy<<endl;
  107.                     cout<<endl;
  108.                     cout<<"Date(dd/mm/yy): "<<dd<<"020"<<yyyy<<endl;
  109.                 }
  110.  
  111.                 else
  112.                 {
  113.                     cout<<"The date you input is invalid. There is an error in day. Please try again."<<endl;goto x;
  114.                 }
  115.             }
  116.  
  117.             else
  118.             {
  119.                 if (dd>=01 && dd<10)
  120.                 {
  121.                     cout<<"The Year You Choose Is 180"<<yyyy<<endl;
  122.                     cout<<endl;
  123.                     cout<<"Date(dd/mm/yy): 0"<<dd<<"020"<<yyyy<<endl;
  124.                 }
  125.  
  126.                 else if (dd>=10 && dd<=28)
  127.                 {
  128.                     cout<<"The Year You Choose Is 180"<<yyyy<<endl;
  129.                     cout<<endl;
  130.                     cout<<"Date(dd/mm/yy): "<<dd<<"020"<<yyyy<<endl;
  131.                 }
  132.  
  133.                 else
  134.                 {
  135.                     cout<<"The date you input is invalid. There is an error in day. Please try again."<<endl;goto x;
  136.                 }
  137.             }
  138.         }
  139.  
  140.         else if (yyyy>=10 && yyyy<=99)
  141.         {
  142.             if (int (yyyy)%4==0)
  143.             {
  144.                 if (dd>=01 && dd<10)
  145.                 {
  146.                     cout<<"The Year You Choose Is 18"<<yyyy<<endl;
  147.                     cout<<endl;
  148.                     cout<<"Date(dd/mm/yy): 0"<<dd<<"02"<<yyyy<<endl;
  149.                 }
  150.  
  151.                 else if (dd>=10 && dd<=29)
  152.                 {
  153.                     cout<<"The Year You Choose Is 18"<<yyyy<<endl;
  154.                     cout<<endl;
  155.                     cout<<"Date(dd/mm/yy): "<<dd<<"02"<<yyyy<<endl;
  156.                 }
  157.  
  158.                 else
  159.                 {
  160.                     cout<<"The date you input is invalid. There is an error in day. Please try again."<<endl;goto x;
  161.                 }
  162.             }
  163.  
  164.             else
  165.             {
  166.                 if (dd>=01 && dd<10)
  167.                 {
  168.                     cout<<"The Year You Choose Is 18"<<yyyy<<endl;
  169.                     cout<<endl;
  170.                     cout<<"Date(dd/mm/yy): 0"<<dd<<"02"<<yyyy<<endl;
  171.                 }
  172.  
  173.                 else if (dd>=10 && dd<=28)
  174.                 {
  175.                     cout<<"The Year You Choose Is 18"<<yyyy<<endl;
  176.                     cout<<endl;
  177.                     cout<<"Date(dd/mm/yy): "<<dd<<"02"<<yyyy<<endl;
  178.                 }
  179.  
  180.                 else
  181.                 {
  182.                     cout<<"The date you input is invalid. There is an error in day. Please try again."<<endl;goto x;
  183.                 }
  184.             }
  185.         }
  186.  
  187.         else
  188.         {
  189.             cout<<"The date you input is invalid. There is an error in year. Please try again."<<endl;goto x;
  190.         }
  191.         break;
  192.     }
  193.  
thanks in advance!!appreciate your help??hope to get your good news soon!!
regrads andrew.
god bless ya all
Feb 27 '07 #1
13 1974
willakawill
1,646 1GB
Hi. This does seem to be a very long winded method for getting a user to input a date. What is the purpose of the code?
Feb 27 '07 #2
Hi. This does seem to be a very long winded method for getting a user to input a date. What is the purpose of the code?
thanks willa for replying

yes indeed its a long winded method that is why im trying to find out is there any other way u can do it!!

the program basically need the user to keyin the date and time in ddmmyyhhnnss(d=day,m=month,y=year,h=hour,n=min,s=s ec) where the day must have a condition which is 01-31/28/30 and soo on for the rest therefore the oni way i can think off is using switch case which turn out to be way too long!!so is there any other way to do this??

thanks for ur HELP appreciate it alots!!
Feb 28 '07 #3
DeMan
1,806 1GB
There seems to be significant repetition in the code. As soo as you need to write the samething several times, consider using a function.

You may like to create a function that selects number of days in the month viz
Expand|Select|Wrap|Line Numbers
  1. int daysInMonth(int month, int yr)
  2. {
  3.   switch(month)
  4.   {
  5.     case 1:
  6.     case 3:
  7.     case 5:
  8.     case 7:
  9.     case8:
  10.     case 10:
  11.     case 12:
  12.       return 31;
  13.     case 2:
  14.       return daysInFebThisYear(yr);  //you'll need to implement this method
  15.     case 4:
  16.     case 6:
  17.     case 9:
  18.     case 11;
  19.       return 30;
  20.     default:
  21.       printf("Can't identify month");
  22.       return 0;
  23.   }
  24. }
  25.  
This can replace your case statement included.

I'm not entirely sure why you bother analyzing things about the year, unless this is a requirem,ent....
Feb 28 '07 #4
There seems to be significant repetition in the code. As soo as you need to write the samething several times, consider using a function.

You may like to create a function that selects number of days in the month viz
Expand|Select|Wrap|Line Numbers
  1. int daysInMonth(int month, int yr)
  2. {
  3.   switch(month)
  4.   {
  5.     case 1:
  6.     case 3:
  7.     case 5:
  8.     case 7:
  9.     case8:
  10.     case 10:
  11.     case 12:
  12.       return 31;
  13.     case 2:
  14.       return daysInFebThisYear(yr);  //you'll need to implement this method
  15.     case 4:
  16.     case 6:
  17.     case 9:
  18.     case 11;
  19.       return 30;
  20.     default:
  21.       printf("Can't identify month");
  22.       return 0;
  23.   }
  24. }
  25.  
This can replace your case statement included.

I'm not entirely sure why you bother analyzing things about the year, unless this is a requirem,ent....
hi deman,

thanks for this info it does help me alot in this method!!LOL wondering why can't i think of this method!!looks like there is no other way besides switch case and if else condition to do this program rite??ok then thanks for ur help!!appreciate it lots!!

but how to calculate the month of feb? when there is a leap year and not leap year how do i calculate it??

thanks alot!!
Mar 1 '07 #5
Ganon11
3,652 Expert 2GB
From what I remember,

You have a year y.

y is a leap year if y is divisble by 4.

BUT, y is NOT a leap year if it is divisible by 100.

BUT, y IS a leap year if it is divisible by 400.

So 1996 is a leap year because it IS divisible by 4.

2000 is a leap year because it is divisible by 400.

1900 is NOT a leap year because it is divisible by 100 BUT NOT 400.

2001 is NOT a leap year because it is not divisible by 4.

Can you develop an algorithm for this?
Mar 1 '07 #6
From what I remember,

You have a year y.

y is a leap year if y is divisble by 4.

BUT, y is NOT a leap year if it is divisible by 100.

BUT, y IS a leap year if it is divisible by 400.

So 1996 is a leap year because it IS divisible by 4.

2000 is a leap year because it is divisible by 400.

1900 is NOT a leap year because it is divisible by 100 BUT NOT 400.

2001 is NOT a leap year because it is not divisible by 4.

Can you develop an algorithm for this?
hi ganon,
oh yea thanks for the information i needed!!
i'll try to develop an algorithm for it!!
if i still have prob wif that i'll be back!!haha~~
thanks
Mar 1 '07 #7
hi all members of thescripts

i've done some part of the program but it appears error on case 8 and case 9!!don't know what is the problem!!and i've done somehting which in switch case but not sure can it work please help me!! below is the program i've created for timestamp thanks to the idea of deman!!

i declare month as char month[2]

Expand|Select|Wrap|Line Numbers
  1. cout<<"Please enter the current month:"<<endl;
  2. month:            cin>>month;
  3.                 if(strlen (month)==2)
  4.                 {
  5.                     for(int i=0; i<=1; i++)
  6.                     {
  7.                         if(!isdigit(month[i]))
  8.                         {
  9.                             cout<<"Invalid input for month!!"<<endl;
  10.                             cout<<"Please re-enter two digit of the current month"<<endl;
  11.                             goto month;
  12.                         }
  13.                     }
  14.  
  15.                 switch(month)
  16.                 {
  17.                     case 01:
  18.                     case 03:
  19.                     case 05:
  20.                     case 07:
  21.                     case 08:
  22.                     case 10:
  23.                     case 12:
  24.                         cout<<"Has 31 days"<<endl;break;
  25.                     case 04:
  26.                     case 06:
  27.                     case 09:
  28.                     case 11:
  29.                         cout<<"Has 30 days"<<endl;break;
  30.                     case 02:
  31.                         cout<<"Has 28 and 29 days"<<endl;break;
  32.                     default:
  33.                         cout<<"Invalid month"<<endl;break;
  34.                 }
  35.                 }
  36.                 else
  37.                 {
  38.                     cout<<"You have entered more than 2 digit or other character"<<endl;
  39.                     cout<<"Please re-enter two digit of the current month"<<endl;
  40.                     goto month;
  41.                 }
  42.  
Question:
1)can i declare case as case 01?(i need 2 digit for month dat's why i do this is it possible?)
2)there is an error on case 8 and 9 saying that illegal digit ' 8' for base '8' same as 9. WHY?

thanks for ur help!!
Mar 1 '07 #8
Ganon11
3,652 Expert 2GB
Well, you can't use a switch statement with an array. switch requires 1 numeral type, not a pointer (which is what month is right now). Why are you using a char array? Why not an integer?
Mar 1 '07 #9
Well, you can't use a switch statement with an array. switch requires 1 numeral type, not a pointer (which is what month is right now). Why are you using a char array? Why not an integer?
if i use an integer array if can do the string compare which i've done so i need to use a char array. thanks for the switch statement. is there any other way to do if i need to have 2digit output coz i need to print out 01 for jan instead of just 1?

and do u know why it have the error on case 8 and 9?

thanks ganon
Mar 1 '07 #10
DeMan
1,806 1GB
One (simple) way to do it, would be to do your checks and allocate an integer value based on this....If you can tell whether a char is a digit, you should be able to returbn which one. make a method to return the value of teh digit if it is one, or return a negative number if it isn't, then you can modify your code.....
Expand|Select|Wrap|Line Numbers
  1. .if(strlen (month)==2)
  2.  {
  3.   int temp = -1;
  4.   int monthVal = 0;
  5.    for(int i=0; i<=1; i++)
  6.    {
  7.      temp = getDigit(month[i]);
  8.      if(temp < 0)
  9.      {
  10.        cout<<"Invalid input for month!!"<<endl;
  11.        cout<<"Please re-enter two digit of the current month"<<endl;
  12.        goto month
  13.     }
  14.     else
  15.     {
  16.       monthVal = monthVal* 10 + temp;
  17.     }
  18.   }
  19.  
  20. //continue with rest of code.....
  21.  
Mar 1 '07 #11
One (simple) way to do it, would be to do your checks and allocate an integer value based on this....If you can tell whether a char is a digit, you should be able to returbn which one. make a method to return the value of teh digit if it is one, or return a negative number if it isn't, then you can modify your code.....
Expand|Select|Wrap|Line Numbers
  1. .if(strlen (month)==2)
  2.  {
  3.   int temp = -1;
  4.   int monthVal = 0;
  5.    for(int i=0; i<=1; i++)
  6.    {
  7.      temp = getDigit(month[i]);
  8.      if(temp < 0)
  9.      {
  10.        cout<<"Invalid input for month!!"<<endl;
  11.        cout<<"Please re-enter two digit of the current month"<<endl;
  12.        goto month
  13.     }
  14.     else
  15.     {
  16.       monthVal = monthVal* 10 + temp;
  17.     }
  18.   }
  19.  
  20. //continue with rest of code.....
  21.  
hi ganon,

sorry to disturb u again but when i use your way it shows error. BTW what is getdigit? isit same as isdigit? i have error on that shows undeclare.

ive tried another way but dunno it works anot? can u help me?

is almost the same problem with the month which i need to put 0 in the front of every single digit.

question:
i declare as integer because i'm using swith case but in the case i convert it into char because i'm using stringcopy to make the output that i need!!so is this the right way? am i missing some steps? coz i still can't get the output that i want.
below is my program:
Expand|Select|Wrap|Line Numbers
  1. //hour declared as integer
  2. cout<<"Please enter the current hour:"<<endl;
  3. hour:            cin>>hour;
  4.                                     if((hour)>0 && (hour)<=24)
  5.                                     {
  6.                                         switch(hour)
  7.                                         {
  8.                                         case 1:
  9.                                             {
  10.                                                                                         char hour[2];    //converting into char
  11.                                                  strcpy(hour,"01");
  12.  
  13.                                                 goto nama; //con't on the program
  14.                                                 break;
  15.                                             }
  16.  
thanks for those who help me!!sorry for the trouble i'm kinda new to c++ so alot of things are yet to learn from you all!! thanks in a millon!!

regrads andrew
Mar 2 '07 #12
WOW the code is in a mess!!
here is a better one!!

Expand|Select|Wrap|Line Numbers
  1. cout<<"Please enter the current hour:"<<endl;
  2. hour:            cin>>hour; //declare hour as integer because using switch case
  3.                                     if((hour)>0 && (hour)<=24)
  4.                                     {
  5.                                         switch(hour)//there are 9 cases
  6.                                         {
  7.                                         case 1:
  8.                                             {
  9.  
  10.                                         char hour[2];//converting integer into char because using stringcopy
  11.                                                  strcpy(hour,"01");
  12.  
  13.                                                 goto nama;//proceed to the rest of the program
  14.                                                 break;
  15.                                             }
  16.  
Mar 2 '07 #13
DeMan
1,806 1GB
sorry the intention was to show you an algorithm.....I was suggesting that you could create a method called "getDigit" which wopuld return the value of the entered sequence....
Mar 2 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by rong.guo | last post: by
15 posts views Thread by Steffen Ramlow | last post: by
7 posts views Thread by JJ | last post: by
reply views Thread by robert | last post: by
82 posts views Thread by Bill David | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.