calprcr
asked on
address and value of pointer problem.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect!
Thanks a lot