473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2252
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

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

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
2
by: rong.guo | last post by:
Hello Group, I am having a really weird problem... Can anyone tell the difference between Query 1 and Query 2 below? Why Query 2 excludes '2/28/2005'? Many thanks! create table a...
15
by: Steffen Ramlow | last post by:
Hi there, is there any way besides a trigger to create a unique number for a column in a "insert into... select from" statement? Thx, sr -- Kein Plan überlebt die erste Feindberührung.
2
by: Russell Smith | last post by:
Timestamps support infinity. However if appears dates do not. When timestamps are cast to dates, there is no output. Is this an acceptable option or not? Below are a number of examples...
22
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same...
7
by: JJ | last post by:
How do I set one field to have the updated timestamp, and another to have the created timestamp? I want to do this directly from code generated from DB Designer if possible?! JJ
0
by: robert | last post by:
I'm told "select distinct on" or even "select distinct" is inefficient because it selects then discards rows. However, I'm stumped to find an alternative to this query : select distinct on...
8
by: kanwal | last post by:
Hi, I have millions of records in my xxxxx table in mysql. And I have a column of time in which I have stored the timestamp using php time() function. Now I wanna write an SQL query to fetch...
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.