Solved

address and value of pointer problem.

Posted on 1997-11-14
2
206 Views
Last Modified: 2010-04-10
hi there,

I wrote this file:

#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)
{
   mn = strtok(dt, " ,");
   dy = strtok(NULL, " ,");
   yr = strtok(NULL, " ,");

//after strtok:
   if (mn == "Jan")
      mn = "1";  //if mn is str Jan,
   else if (mn == "Nov")
         mn = "11";
   else
         mn == "13";

   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("Jan 9,1997");
   cout << "date2 = ";
   date2.PrintDate();
   cout << '\n';
   //Date date3("11/09/1997");
   //cout << "date3 = ";
   //date2.PrintDate();
   
   return 0;
}

the output is
11-9-1997
0-9-1997

If I put in the //month = atoi(mn);
the output will be
11-9-1997
101994-9-1997

How can I print out like this
11-9-1997
11-9-1997
Here is what my thinking routine:
I strtol the string: "Nov 9,1997"
mn will be Nov
dy will be 9
yr will be 1997

but for some reasons I can't make the output like 11-9-97 instead of Nov-9-1997 which I can print easily.

any way to solve this?

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 Comments
 
LVL 2

Accepted Solution

by:
wpinto earned 50 total points
ID: 1173191
Hi calprcr,

Your code looks fine except for the way you are comparing strings

Note that the following will not do what you expected
    if (mn == "Jan")

The correct way to compare strings is
    if (strcmp(mn, "Jan") == 0) ...

Please change all your above string compares with the above code & it should then work fine

Hope this helps

Wilfred

0
 

Author Comment

by:calprcr
ID: 1173192
Ah, that is right.
perfect!
Thanks a lot


0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.

729 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