How do I add the date to it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blondie1966
    New Member
    • Mar 2010
    • 12

    How do I add the date to it?

    I am wanting to add the date to this program, how do I do it? What I mean is everytime I get into to it I want to be able to enter the days date, help please...Thanks

    Code:
    // Gail Sears
    /*This program allows the user to add trucks to their
    inventory. It also allows them to add and remove donations
    from the truck chosen. It also display a chart of
    what trucks are in inventory, with the name of the
    truck and the amount of donations in each one.
    */
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define max_trucks 500
    #define max_cap 250
    
    class OIL_TANK
    {
    private:
    int ID_NUM;
    int TRUCK_CAP;
    int DON_ON_HAND;
    static int NUM_TRUCKS;
    
    public:
    OIL_TANK (int id, int don= 0);
    ~ OIL_TANK ();
    int ADD_DONATIONS (int amt);
    int REMOVE_DONATIONS (int amt);
    inline int GET_ID_NUM (){return ID_NUM;}
    inline int GET_TRUCK_NAME () (return MERS/GOODWILL;)
    inline int GET_CAP () {return TRUCK_CAP;}
    inline int GET_CONT() {return DON_ON_HAND;}
    static int TOTAL_TRUCKS ();
    };
    int OIL_TANK::NUM_TRUCKS = 0;
    
    OIL_TANK::OIL_TANK (int id, int don)
    {
    ID_NUM = id;
    DON_ON_HAND = don;
    TRUCK_CAP = 250;
    ++NUM_TRUCKS;
    }
    int OIL_TANK::ADD_DONATIONS (int amt)
    {
    	if (DON_ON_HAND + amt > TRUCK_CAP)
    return -1;
    else
    	return DON_ON_HAND += amt;
    }
    int OIL_TANK::REMOVE_DONATIONS (int amt)
    {
    	if (DON_ON_HAND - amt < 0)
    return -1;
    	else
    		return DON_ON_HAND -= amt;
    }
    
    OIL_TANK::~OIL_TANK ()
    {
    delete [] TRUCK_NAME;
    }
    int OIL_TANK::TOTAL_TRUCKS()
    {
    return NUM_TRUCKS;
    }
    OIL_TANK* create_truck();
    void display (OIL_TANK&);
    
    void main ()
    {
    	int don_amt,
    	result,
    	count = 0,
    	choice;
    	char select  = ' ';
    	char answer = ' ';
    
    OIL_TANK* truck_table[max_trucks];
    do
    {
    cout << "\n\n		WELCOME TO THE MERS/GOODWILL TRUCK INVENTORY\n";
    cout << "\n\nPlease choose a number from the menu below.\n";
    cout << "\nl. Add a truck to inventory.";
    cout << "\n2. Add donations to a truck.";
    cout << "\n3. Remove donations from a truck.";
    cout << "\n4. Display the trucks in inventory.";
    cout << "\n5. Quit the Program.";
    cout << "\n\nPlease enter your selection now: ";
    cin >> select;
    clrscr ();
    switch (select)
    {
    case '1':
    cout << "\nDo you want to add a truck to inventory? (Y/N): ";
    cin >> answer;
    while (toupper(answer) == 'Y' && count < max_trucks)
    {
    truck_table [count] = create_truck();
    ++count;
    cout << "\nDo you want to add a truck to inventory? (Y/N): ";
    cin >> answer;
    }
    break ;
    case '2':
    cout << "\nTo which truck would you like to add donations?(truck_num): ";
    cin >> choice;
    if (choice < 1 || choice > count)
    {
    cout << "\n\nINVALID ENTRY!!! ";
    break;
    }
    else
    {
    cout << "n\nPlease enter the amount of donations to add: ";
    cin >> don_amt;
    result = truck_table[choice - 1]->ADD_DONATIONS (don_amt);
    if (result < 0)
    cout<< "\nERROR Truck capacity is not large enough!!!\n";
    else
    cout << "\nDonations Added!!! ";
    break;
    }
    case '3':
    cout << "\nFrom which truck would you like to remove donations? (truck_num): ";
    cin >> choice;
    if (choice < 1 || choice > count)
    {
    cout << "\n\nINVALID ENTRY!!! ";
    break;
    }
    else
    cout << "\nPlease enter the amount of donations to remove: ";
    cin >> don_amt;
    result = truck_table[choice - 1]->REMOVE_DONATIONS (don_amt);
    if (result < 0)
    cout <<"\nERROR Truck does not have enough donations in it!!!\n";
    else
    cout << "\nDonations Removed!!! ";
    
    break;
    
    case '4':
    for (int x = 0; x < count; ++x)
    display(*truck_table[x]);
    break;
    }
    
    }
    while (select != '5');
    cout << "\n\n\n\n   				    THANK YOU";
    cout << "\n\n  				     GOODBYE";
    for (int x = 0; x < count; ++x)
    delete truck_table[x];
    }
    OIL_TANK* create_truck()
    {
    int id_num,
    donations = 0;
    char name [80];
    OIL_TANK* tankptr;
    cout << "\nPlease enter the truck number: ";
    cin >> id_num;
    cin.ignore(81, '\n');
    cout << "\nPlease enter the truck name: ";
    cin.getline(name, 80);
    cout << "\nPlease enter the amount of donations in truck: ";
    cin >> donations;
    cin.ignore(81, '\n');
    clrscr();
    tankptr = new OIL_TANK (id_num, donations);
    cout << "\n\n\Trucks in inventory: " << tankptr->TOTAL_TRUCKS();
    return tankptr;
    }
    void display (OIL_TANK& tank)
    {
    cout << "\nTruck ID " << tank.GET_ID_NUM();
    cout << "\nName of Truck: " << tank.GET_NAME();
    cout << "\nTruck Capacity: " << tank.GET_CAP();
    cout << "\nDonations on Hand: " << tank.GET_CONT();
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    So are you saying that every time the program is run you want a coded command
    to provide the system date which will be printed to screen?

    Comment

    • Blondie1966
      New Member
      • Mar 2010
      • 12

      #3
      yes or somewhere I can enter it, how do I code that in?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Why not write your own Date class?

        Comment

        • Blondie1966
          New Member
          • Mar 2010
          • 12

          #5
          i dont know how to add it

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6



            Note that time in seconds are returned so if you want it to print in a custom manner you can take a substring of the asctime.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              You prompt for the date and then enter it.
              Code:
              string s;
              cout<<"enter current date";
              getline(cin,s);
              //type June 14 2010 which will appear on the screen and will be the content of s.
              cout<<s;//prints June 14 2010 to screen.

              Comment

              • Blondie1966
                New Member
                • Mar 2010
                • 12

                #8
                ok I understand that part but now where do I put that, to return the correct date, how do I know the correct date is being entered and not any date being put in and the program taking it, again thanks for all the help

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  You already have an OIL_TANK class so why not a Date class?

                  Code:
                  int main()
                  {
                      Date dt;
                  
                      dt.GetDate();
                  
                      //etc...
                  }
                  I am sure you can write a Date class that holds a month, day and year and cna write a member function that stores the month day and year from the keyboard into the Date object.

                  Comment

                  • whodgson
                    Contributor
                    • Jan 2007
                    • 542

                    #10
                    Yes but that will not necessarily guard against the entry of an incorrect date. I presume the date (and time) has to be entered by the system in this case.

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Here's where you earn you pay as a developer.

                      When you have the user enter a date, any old thing may be entered. So you need a) to have the user enter a monthn, day and year then b) you need to verify that what was entered is a valid date. So you call your nifty date validation function. If tyhe date is invalid, it does not get put in the Date object. Instead, the function fails and you test that in main() and ask for the user to try again.

                      Your date verification routine needs to a) insure the month is in the range 1-12. b) unsure that the days are possible for the month-that includes a 28 or 29 value for month 2. And that means you need to call a leap year caculation function that returns true of the year is a leap year and false otherwise. BTW: Leap years a divisible by 4 but not by 400. A yeat divisible by 4 and 400 is not a leap year.

                      All this means when you create your Date object that initializing the the date in the object to 0 month 0 days and 0 years is not correct since this is not a valid date. You Date constrcutor needs to set the object to a default date. Likemaybe it calls the operating system clock and parses out the current month, day and year.

                      Lastly, be sure this Date code is in a Date.h and a Date.cpp because I guarantee you will use this code all over the place forever.

                      Comment

                      • whodgson
                        Contributor
                        • Jan 2007
                        • 542

                        #12
                        The following code in conjunction with:
                        Code:
                        #include<ctime>
                        time_t t = time(NULL);
                            cout<<ctime(&t);
                        //prints Mon Apr 12 12:30:00 2010 //there are ways of changing the format

                        Comment

                        • Blondie1966
                          New Member
                          • Mar 2010
                          • 12

                          #13
                          Yes Im wanting the user to be promted each time using the program to enter the date, not necessarily the time and of course I want it to recognize it and make sure the correct date is being entered each time. Thanks for alls help

                          Comment

                          Working...