Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pointer doesn't work as I want!

Posted on 1997-11-15
4
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 120 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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