View Full Version: A date writer simple...

C++ Learning Community > C++ Creations > A date writer simple...


Title: A date writer simple...


Shonoby - December 30, 2006 09:22 AM (GMT)
Well, i was bored like i always am so i decided to write this code just to see if i could with 'class', and yes it worked i made 2 versions one at 'randome' and the other a 'user input'
public: void printsetUP() is also univerisal time, its needed so printDate knows if it is PM or AM, hences printsetUP
Here is the randome one:
CODE

// Times In a Day
// By: Darwin Aguirre
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setfill;
using std::setw;

#include <cstdlib>
#include <ctime>

class Time {

public:
   Time();
   void setTime( int, int, int, int );
   void printsetUp();
   void printDate();

private:
   int month;
   int day;
   int hour;
   int minute;

};

Time::Time()
{
   month = day = 1;
   hour = minute = 0;
}

void Time::setTime( int m1, int d, int h, int m2 )
{
   month = ( m1 >= 0 && m1 <= 12 ) ? m1 : 0;
   day = ( d >= 1 && d <= 31 ) ? d : 0;
   hour = ( h >= 0 && h < 24 ) ? h : 0;
   minute = ( m2 >= 0 && m2 < 60 ) ? m2 : 0;
}

void Time::printsetUp()
{
   cout << setfill( '0' ) << setw( 2 ) << hour << ":"
        << setw( 2 ) << minute;
}

void Time::printDate()
{
   const char *month1[ 13 ] =
   { "WONT HAPPEN", "January", "Febuary", "March", "April", "May", "June", "July", "August",
     "September", "October", "December" };

   cout << "On " << month1[month] << '/' << day << "/92 at ";
   cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
        << ":" << setfill( '0' ) << setw( 2 ) << minute
        << ( hour < 12 ? " AM" : " PM" ) << endl;
}

int main()
{
   Time T;
   srand( time( 0 ) );
   int m1;
   int d;
   int h;
   int m2;

   cout << "Base time without change is: ";
   T.printDate();

   cout << "Base time changed to my birthday is: ";
   T.setTime( 8, 16, 19, 20 );
   T.printDate();

   cout << "Base time changed to randome is: ";

   m1 = ( 1 + rand() % 11 );
   d = ( 1 + rand() % 30 );
   h = ( rand() % 23 );
   m2 = ( rand() % 59 );

   T.setTime( m1, d, h, m2 );
   T.printDate();

   return 0;
}



Heres the one with user input:
CODE

// Times In a Day, User Input
// By: Darwin Aguirre
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setfill;
using std::setw;

class Time {

public:
   Time();
   void setTime( int, int, int, int );
   void printsetUp();
   void printDate();

private:
   int month;
   int day;
   int hour;
   int minute;

};

Time::Time()
{
   month = day = 1;
   hour = minute = 0;
}

void Time::setTime( int m1, int d, int h, int m2 )
{
   month = ( m1 >= 1 && m1 <= 12 ) ? m1 : 0;
   day = ( d >= 1 && d <= 31 ) ? d : 0;
   hour = ( h >= 0 && h < 24 ) ? h : 0;
   minute = ( m2 >= 0 && m2 < 60 ) ? m2 : 0;
}

void Time::printsetUp()
{
   cout << setfill( '0' ) << setw( 2 ) << hour << ":"
        << setw( 2 ) << minute;
}

void Time::printDate()
{
   const char *month1[ 13 ] =
   { "WONT HAPPEN", "January", "Febuary", "March", "April", "May", "June", "July", "August",
     "September", "October", "December" };

   cout << "On " << month1[month] << '/' << day << "/92 at ";
   cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
        << ":" << setfill( '0' ) << setw( 2 ) << minute
        << ( hour < 12 ? " AM" : " PM" ) << endl;
}

int main()
{
   Time T;
   int m1;
   int d;
   int h;
   int m2;

   cout << "Base time without change is: ";
   T.printDate();

   cout << "Base time changed to my birthday is: ";
   T.setTime( 8, 16, 19, 20 );
   T.printDate();

   cout << "\nNow Enter Your Time\n";
   
   cout << "Enter month( number according to month ): ";
   cin >> m1;
   cout << "Enter day: ";
   cin >> d;
   cout << "Enter hour: ";
   cin >> h;
   cout << "Enter minute: ";
   cin >> m2;

   cout << "Base time changed to your settings are: ";
   T.setTime( m1, d, h, m2 );
   T.printDate();

   return 0;
}



Thats about it... Both are simple, not hard at at only a couple of diffences
If you want to get PM in the user input u have to use unversial time like this:
if i want to get 9:20 PM i will do this:
21 for hour
20 for min ( minutes are the same universial and standared )
Universial is decribed as such:
0 hr ( this is the same for unviersial && standared time, up till 12)
1 hr
2 hr
3 hr
4 hr
5 hr
6 hr
7 hr
8 hr
9 hr
10 hr
11 hr
12 hr (AM)

13 hr ( 1 PM )
14 hr ( 2 PM )
15 hr ( 3 PM and so on )
16 hr
17 hr
18 hr
19 hr
20 hr
21 hr
22 hr
23 hr

This is universial time....
K hope u like it!




Hosted for free by InvisionFree