3-day novice, How do I calculate age in days? [C++]

From: wwwolf (myname_at_alltel.net)
Date: 03/25/05


Date: Fri, 25 Mar 2005 00:29:18 -0500

This is the 3rd day into teaching myself C++. I have looked at some WWWtutorials, LOTS of
broken links, read the 3 FAQs recommended, and have some books ordered. (although I
ordered a Schildt book before reading the FAQ...Doh! Luckily it is used so it was
cheap.)

My niece is visiting next week and I want to write a small program that will carry on a
short conversation and perform a mathematical demonstration (age in days) to spark her
interest in computer programming.

For me, having the screen cleared, and having the program "cout" what she "cin"ed are
bells and wistles enough, but to have the program appear more interesting, I wanted it
to:
1. Ask her birthdate;
2. Call the OS for the current date;
3. Subtract (date2 - date1);
4. Tell her she is x days old.

In the C-FAQ, which is still over my head, Q13.14 "How can I find the difference between
two dates?" says I should use mktime, difftime, and getdate. I have no idea how to set
these up and I am also weary of following the advice in that FAQ if there is a C++ way
to do it instead.

In the C++FAQ-Lite, also intimidating, Q13.9 "Overloading operators" says I should
provide arithmetic operators to allow (date1 - date2) and provide a reasonable class
type to represent the duration. I think this is the approach I would prefer but I don't
really know how to set this up as well.

If someone could help me I would really appreciate it. Please remember that I am only 3
days old concerning C++ and know squat about C. If you see any bad habits in my code
please tell me. I'm afraid that teaching myself is going to open up the opportunity for
bad habits so I want them corrected now before they really become a habit.

Thanx very much for your help.

// Proggy the conversing program

#include <iostream>
using std::cout;
using std::cin;

int main ()
   {

   int n (100); // Since I can't figure out how to clear the screen on start, this works.
   while (n>0)
      {
      cout << '\n';
      --n;
      }

   char name [50];
   char animal [50];

   cout << "What is your name? ";

   cin.getline (name,50);

   cout << "\nHello " << name << ". My name is Proggy. It is a pleasure to meet you.\n";
   cout << "\nWhat is your favorite animal? ";

   cin.getline (animal,50);

   cout << "\nWell " << name << "!" << " We have something in common, because I like ";
   cout << animal << " too!\n\n";

   return 0;
   }



Relevant Pages

  • Re: Do all tables have to have a primary key?
    ... You're teaching good habits (although you'll get a very heated debate as to ... whether you should always use autonumbers, ... have a primary key, no matter how minor the table. ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Learning other styles without teacher?
    ... teaching myself at home on my electric? ... I don't think there is such a thing as bad habits, just the style you play ... lot of your classical playing could transfer to rock. ...
    (alt.guitar)
  • Re: Progress - should I continue?
    ... Thanks for all the responses. ... The track I was talking about was really making sure I wasn't teaching myself any bad habits early on in terms of technique. ... I know a few songs and yes, that is definitely the most fun way to practise. ...
    (alt.guitar.beginner)
  • Re: I finally got her to sit still
    ... Looks are deceiving the others are already teaching her their bad habits ... little devils ...
    (rec.pets.cats.anecdotes)