473,765 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there an alternative to program a timestamp?

24 New Member
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!!apprec iate your help??hope to get your good news soon!!
regrads andrew.
god bless ya all
Feb 27 '07 #1
13 2281
willakawill
1,646 Top Contributor
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
andrewanderson
24 New Member
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=y ear,h=hour,n=mi n,s=sec) 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 Top Contributor
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
andrewanderson
24 New Member
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!!appreciat e 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 Recognized Expert Specialist
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
andrewanderson
24 New Member
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
andrewanderson
24 New Member
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 Recognized Expert Specialist
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
andrewanderson
24 New Member
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

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

Similar topics

13
9304
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 three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
2
5182
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 (ApplicationID int ,timestamp datetime)
15
2793
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
4371
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 showing what I am experiencing. The last own shows how converting timestamps to dates and then ordering doesn't give you the order you want. Maybe you should just order by the timestamp to begin with. However Date does not understand infinity at all.
22
6430
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 database. The original Update and Delete SP's all use a timestamp for concurreny checking. I am trying to use the same Update SP from my sqlDataSource but I keep getting the following error:
7
6113
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
4163
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 (user_id) * from bids where auction_id = 3 order by user_id,created_at DESC; I'm trying to retreive only the last bid for each user for a given auction. I'd appreciate any suggestions.
8
2077
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 the records either for year (2006) or for month and year (Jan 2006) Currently I had implement this logic:
82
3731
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. Let's name the update method as doUpdate and stl::map read methods as getData and copyData. Since stl::map is not thread-safe, we should do synchronization by ourselves. A usable solution is to create a boost::mutex::scoped_lock object in all above...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8832
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.