pointer doesn't work as I want!

hi there, the question is within the code:

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

class Date
{
 private:
       int month;
   int day;
   int year;

   char *mn;
   char *dy;
   char *yr;
 public:
       Date(int x, int y, int z);
   Date(char *dt);
   void PrintDate(); //print the date with format: 11-09-1997.
};

Date::Date(int x, int y, int z)
{
      month = x;
   day = y;
   year = z;
}

Date::Date(char *dt)
{

   if isalpha(*dt)
   {
         mn = strtok(dt, " ,");

      //make the letters in the string become lower case !
      for (; *mn != '\0'; ++mn)
            tolower(*mn);
      //but the following line doesn't print out anything! why?
      //I want the string that mn points to is "nov". How?
      cout << mn << endl;
      if (strcmp(mn, "Jan") == 0)
            strcpy(mn, "1");
         else if (strcmp(mn, "Feb") ==0)
               strcpy(mn, "2");
         else if (strcmp(mn, "Mar") == 0)
               strcpy(mn, "3");
         else if (strcmp(mn, "Apr") == 0)
               strcpy(mn, "4");
         else if (strcmp(mn, "May") == 0)
               strcpy(mn, "5");
         else if (strcmp(mn, "Jun") == 0)
                  strcpy(mn, "6");
         else if (strcmp(mn, "Jul") == 0)
               strcpy(mn, "7");
      else if (strcmp(mn, "Aug") == 0)
               strcpy(mn, "8");
         else if (strcmp(mn, "Sep") == 0)
               strcpy(mn, "9");
         else if (strcmp(mn, "Oct") == 0)
               strcpy(mn, "10");
         else if (strcmp(mn, "Nov") == 0)
               strcpy(mn, "11");
         else if (strcmp(mn, "Dec") == 0)
            strcpy(mn, "12");
         else if (strcmp(mn, "nov") == 0)
               strcpy(mn, "11");
         else
               cout << "Program error!";
      dy = strtok(NULL, " ,");
         yr = strtok(NULL, " ,");

   }

   else if isdigit(*dt)
   {
   mn = strtok(dt, " /");
   dy = strtok(NULL, " /");
   yr = strtok(NULL, " /");


   }
   else
         cout << "Program error!";

   month = atoi(mn);
   day = atoi(dy);
   year = atoi(yr);


}

void Date::PrintDate()
{
      cout << month << '-'        << day << '-'  << year;
}

int main(void)
{
      Date date1(11, 9, 1997);
   cout << "date1 = ";
   date1.PrintDate();
   cout << '\n';
   Date date2("nOv 9,1997");
   cout << "date2 = ";
   date2.PrintDate();
   cout << '\n';
   Date date3("12/09/1997");
   cout << "date3 = ";
   date2.PrintDate();

   return 0;
}

could you tell me what I should correct?

thank you
calprcrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
 for (; *mn != '\0'; ++mn) tolower(*mn);
perhaps you wanted:  *mn = tolower(*mn);
 //but the following line doesn't print out anything! why?
                 cout << mn << endl;
because mn is now pointing to the '\0' which ended your loop
 // I want the string that mn points to is "nov". How?
don't move mn:
 for(char *m=mn; *m != '\0'; ++m) *m = tolower(*m);

 else if (strcmp(mn, "Nov") == 0)  
that won't match if mn is lower case, maybe you want strcasecmp(mn, "Nov") == 0
0
nietodCommented:
hello again,

First of all you probably want the string passed in the parameters to be constant.  This unfortunately means you can't change iy by capitalizing or tokenizing.  However, you should just copy it to a local variable.  You can either decide that there is a maximum length you will allow like

constant int MaxDatLen = 80

* * *

Data::Date(const char *StrPtr)
{
   char DatStr[MaxDatLen+1];

   strncpy(DatStr,StrPtr,MaxDataLen);
   DatStr[MaxDatLen] = 0; // Make sure string is terminated.

//   Now, Process DatStr.
}

or you can get a dynamic string to work with.

Date::Date(const char *DatPtr)
{
   char *DatStr = new[strlen(DatPtr)+1];

   strcpy(DatStr,DatPtr)

   // process DatStr

   delete [] DatStr;
}

once you have a date string you can change, then lowercase the whole thing.  you could do

char *ChrPtr = DatStr;

while(tolower(*ChrPtr++)) ;

which migh be a bit cryptic, but will optimize well, but easier is

_strlwr(DatStr);

If you want to skip over leading blanks do

char *CurPos = DatStr;

while (*CurPos && *CurPos == ' ')
   ++CurPos;

Next test what kind of string you have numeric (12/34/56) or text.

isalpha(CurPos);

Now if you are in a text string, the next 3 characters must be a valid month (or you have an error and I'm not sure what you want to do in that case.)  You don't can if there is a comma or space after the month, you just wnat to look at the next three characters.  To be safe though, lets make sure there are three more characters in case the string passed was empty, or just contained spaces, or less than three characters long.

 if (strlen(CurPos) < 3)
    error();
 if (strncmp(CurPos,"jan") == 0)
    month = 1;

Note a coule things.  We are using CurPos, not DatStr because we've skipped leading blanks.  As you process you'll continue to use CurPos and keep incrementing it.  We look for a lowercase "jan". and we change "month".  I'm not sure what you are planning to do with "mn", but it seems wrong.  (First of all, you never initialize it to anything).

next you would add 3 to the CurPos, look for a comma if any, if found increment CurPos.  Then skip spaces again.  This should put you at the date digits.  Hopefully you can go on from there.  Try it and post a comment if not.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Oh, I got confussed with the "mn" stuff.  I see what you are doing.  Its probably a bad idea though.  You probalby want your (* char) constructor to the same sort of information as you (int,int,int) constructor.  That is get numbers for the month, day and year.  you just want to set a flag somewhere that indicates what format the date was constructred with.  otherwise a data object could the date in two different manners (one of which is inconvenient) and dealing with the two different manners is likely to be a headache.  

do something like

class Date
{
private:
   int month;
   int day;
   int year;
   bool NumFmt; // TRUE if MM/DD/YY. FALSE if Mon DD, YYYY.
public:
   Date(int x, int y, int z);
   Date(char *dt);
   void PrintDate();
};
0
calprcrAuthor Commented:
nietol, ozo:
Thank you for all your help!

calprcr
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.