CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2012
    Posts
    1

    Need help with extra credit problem

    I have an extra credit project where I'm supposed to calculate an approximate the user's age based on the current date and their birthday. My problem is that I am not getting the proper date and time. C++ keeps giving me some random date. I looked up c++ time format on the internet because this is extra credit and not in my book.

    I am currently using dev C++ 4.9.9.2.

    Thanks in advance for any help I receive.

    Here is my current code :
    Code:
    /*
    
    */
    
    // included libraries
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <time.h>
    
    #define cls system("cls") 
    #define pauseOutput system("pause")
    
    
    void printM(int x);
    void age(int m, int d, int y, int &yearAge, int &monthAge, int &dayAge);
    
    
    using namespace std;
    
    int main()
    {
        time_t nowIsTheMoment; // set time variable nowIsTheMoment
        int bMonth,bDay, bYear;
        int yearAge, monthAge, dayAge;
        
        cout << "The current time is, " << ctime(&nowIsTheMoment) << endl << endl;
        
        cout << "Enter your birth month in number format ex. 1 for January: ";
        cin >> bMonth;
        while(cin.fail())
        {
             cin.clear();
             cout << "Error\n";
             cout << "Enter your birth month in number format ex. 1 for January: ";
             cin >> bMonth;         
        }
        while(bMonth < 1 || bMonth > 12) 
        {
             cin.clear();
             cout << "Error\n";
             cout << "Enter your birth month in number format ex. 1 for January: ";
             cin >> bMonth;        
        }
        cin.clear();
        
        
        cout << "\nEnter your birth date: ";
        cin >> bDay;
        while(cin.fail())
        {
             cin.clear();
             cout << "Error\n";
             cout << "Enter your birth date: ";
             cin >> bDay;         
        }
        while(bDay < 1 || bDay > 31) 
        {
             cin.clear();
             cout << "Error\n";
             cout << "Enter your birth date: ";
             cin >> bDay;        
        }
        cin.clear();
        
        cout << "\nEnter your birth year: ";
        cin >> bYear;
        while(cin.fail())
        {
             cin.clear();
             cout << "Error\n";
             cout << "Enter your birth year: ";
             cin >> bYear;         
        }
        cin.clear();
        
        cout << "\n --- Your Birthday --- " << endl << endl;
        printM(bMonth);
        cout << " " << bDay << ", " << bYear << endl;
        
        age(bMonth,bDay,bYear,yearAge,monthAge,dayAge);
        
        cout << yearAge << " " << monthAge << " " << dayAge;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void printM(int x)
    {
         //Choose and print Month
         switch ( x )
         {
               case 1:
               cout << "January"; 
               break;
               
               case 2:
               cout << "February";
               break;
               
               case 3:
               cout << "March";
               break;
               
               case 4:
               cout << "April";
               break;
               
               case 5:
               cout << "May";
               break;
                      
               case 6:
               cout << "June";
               break;
                     
               case 7:
               cout << "July" ;
               break;
               
               case 8:
               cout << "August"; 
               break;
               
               case 9:
               cout << "September";
               break;
               
               case 10:
               cout << "October";
               break;
               
               case 11:
               cout << "November";
               break;
               
               case 12:
               cout << "December";
               break;
         
         }
         
    }
    
    void age(int m, int d, int y, int &yearAge, int &monthAge, int &dayAge)
    {
         time_t nowIsTheMoment; // set time variable nowIsTheMoment
         time(&nowIsTheMoment);
        
         struct tm* timeinfo;
         timeinfo = localtime( &nowIsTheMoment );
         
         yearAge = (timeinfo->tm_year) - y;
         monthAge = timeinfo->tm_mon - m;
         dayAge = timeinfo->tm_mday - d;
         
        
        
        
    }

  2. #2
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: Need help with extra credit problem

    You are getting the wrong year because tm struct has a particular year representation see the "year format" here: http://www.cplusplus.com/reference/ctime/tm/

    And the random values most likely come because in the main function you don't initialize the nowIsTheMomentVariable:
    Code:
    int main()
    {
        time_t nowIsTheMoment; // set time variable nowIsTheMoment
        time(&nowIsTheMoment); //add a call to time(...)
    //...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured