Solved

pointer doesn't work as I want!

Posted on 1997-11-15
4
226 Views
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
{
 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
0
Comment
Question by:calprcr
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
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
0
 
LVL 22

Accepted Solution

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

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

Expert Comment

by:nietod
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
{
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
 

Author Comment

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

calprcr
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now