SHARE
TWEET

Part 2: Test 1: How old are you?

zCool May 3rd, 2012 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. int birthYear;
  8. int age;
  9. time_t seconds;
  10. double numYears;
  11. int currentYear;
  12. std::string message;
  13.  
  14.  
  15. // seconds since January 1, 1970
  16. seconds = time(NULL);
  17.  
  18. //ok the following does not account for leap years...
  19. //works right now, but would probably break over time.
  20. numYears = seconds/(3600*24*365);
  21. currentYear = 1970 + (int) floor(numYears);
  22. std::cout << "Please enter your birth year(YYYY): ";
  23. while(true)
  24. {
  25.         if(std::cin >> birthYear)
  26.         {
  27.                 if(birthYear < 0)
  28.                 {
  29.                         std::cin.clear();
  30.                         std::cin.ignore();
  31.                         std::cout << "Please enter a real year: ";
  32.                 }
  33.                 else
  34.                 {
  35.                         break;
  36.                 }
  37.         }
  38.         else
  39.         {
  40.                 std::cin.clear();
  41.                 std::cin.ignore();
  42.                 std::cout << "Sorry I didn't get that, please enter your birth year: ";
  43.         }
  44.  
  45. }
  46.  
  47. age = currentYear - birthYear;
  48.  
  49. if ( age >= 21)
  50. {
  51.         message = "Here is a Beer";
  52. }
  53. else if ( 18 <= age && age < 21)
  54. {
  55.         message = "You can vote or join the military";
  56. }
  57. else if ( 16 <= age && age < 18)
  58. {
  59.         message = " You can drive now";
  60. }
  61. else if ( age < 16)
  62. {
  63.         message = "Sorry no perks besides being a kid";
  64. }
  65. std::cout << "Your Approximate age is: " << age << "\n" << message << "\n";
  66.  
  67. system("PAUSE");
  68.  
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top