-
November 23rd, 2012, 09:14 AM
#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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|