pointer doesn't work as I want!

Posted on 1997-11-15
Last Modified: 2010-04-10
hi there, the question is within the code:

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

class Date
       int month;
   int day;
   int year;

   char *mn;
   char *dy;
   char *yr;
       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)
      //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");
               cout << "Program error!";
      dy = strtok(NULL, " ,");
         yr = strtok(NULL, " ,");


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

         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 = ";
   cout << '\n';
   Date date2("nOv 9,1997");
   cout << "date2 = ";
   cout << '\n';
   Date date3("12/09/1997");
   cout << "date3 = ";

   return 0;

could you tell me what I should correct?

thank you
Question by:calprcr
  • 2
LVL 84

Expert Comment

ID: 1173333
 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
LVL 22

Accepted Solution

nietod earned 30 total points
ID: 1173334
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];

   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];


   // 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


If you want to skip over leading blanks do

char *CurPos = DatStr;

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

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


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)
 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.
LVL 22

Expert Comment

ID: 1173335
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
   int month;
   int day;
   int year;
   bool NumFmt; // TRUE if MM/DD/YY. FALSE if Mon DD, YYYY.
   Date(int x, int y, int z);
   Date(char *dt);
   void PrintDate();

Author Comment

ID: 1173336
nietol, ozo:
Thank you for all your help!


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question