I am having to write a program that has a user-defined class. In this program I need to convert an INT to a STRING. For example, if the program reads in the date "7/17/2009" it will need to be converted to "July 17, 2009", where all it does is take the month's number value and change it to the string equivalant.

Can somebody help me with the INT to STRING conversion? I can't quite grasp it.

Here is what I have thus far for the implementation of my class:

#include "testerResult.h"
#include <iostream>
#include <string>

using namespace std;

Date::Date()
{
	month = 1;
	day = 1;
	year = 1800;
}

Date::Date(int m, int d, int y)
{
}

Date::Date(string m, int d, int y)
{
	Date::convertMonthNumberToString(month);

	month = m;
	day = d;
	year = y;
}

int Date::compareDates()
{

	return 0;
}

string Date::convertMonthNumberToString(int month)
{
	switch (month)
  {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";  
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";

  }
}

int Date::dayNumber()
{

	return 0;
}

void Date::displayDate()
{
}

void Date::increment()
{
}

void Date::isLeapYear()
{
	if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
	{
		cout << "This is a Leap Year." << endl;
	}
	else
	{
		cout << "This is not a Leap Year." << endl;
	}
}

void Date::isValidDate()
{
	int d = 0;
	int m = 0;
	int y = 0;

	if (d < 1 || d > 31)
	{
		cout << "Invalid Date." << endl;
	}
	else if (m < 1 || m > 12)
	{
		cout << "Invalid Date." << endl;
	}
	else if (y < 1800 || y > 2100)
	{
		cout << "Invalid Date." << endl;
	}
	else
	{
		cout << "Valid Date." << endl;
	}
}

void Date::setDate(int m, int d, int y)
{
	if(m < 1 || m >12) return;
	month = m;
	cout << m;
	if(d < 1 || d > 31) return;
	day = d;
	cout << d;
	if(y < 1800 || y > 2100) return;
	year = y;
	cout << y;
}
 
int Date::getDay() const
{
	return day;
}

int Date::getMonth() const
{
	return month;
}
 
int Date::getYear() const
{
	return year;
} 


int Date::getDate() const
{
	return month;
	return day;
	return year;
}

I know this code is full of errors. I just need assistance with the 'convertMonthNumberToString' function.

Don't use class name along with member if that member is an instance (object) member. Static members are qualified with class name only.
You have to define member function convertMonthStringToNumber.

Date::Date(string m, int d, int y)
{
	month = convertMonthStringToNumber(m);
	day = d;
	year = y;
}
commented: This helped a lot. Thanks +1

This helped a lot. Thanks a lot. I appreciate it.

Well, yes, it is full of errors, and keep in mind that sometimes when something is full of errors, the part you are having a hard time with may NOT be the error. The function you are worried about seems fine to me save for one thing:

string Date::convertMonthNumberToString(int month)
{
	switch (month)
  {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";  
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";
  }
}

I personally try to NEVER name my function parameters the same as my class variables. Leave one this off and you're in a lot of trouble. Since month is a class variable, I'd strongly advise you change the variable in red to m , as you do elsewhere. It gets too confusing otherwise, for me at least. Or perhaps you don't want month passed to this function (i.e. the function should take no parameters)? I don't know. Your function CALL in your constructor is wrong. Since I'm not sure what you're doing here, I won't hazard a guess as to how to fix it.

Date::Date(string m, int d, int y)
{
	Date::convertMonthNumberToString(month);

	month = m;
	day = d;
	year = y;
}

[EDIT]
I see someone has beaten me to it and I'm a couple of posts behind. I'll post anyway.
[/EDIT]

Regarding adatapost's suggestion, this isn't going to work:

Date::Date(string m, int d, int y)
{
	month = convertMonthStringToNumber(m);
	day = d;
	year = y;
}

convertMonthToString takes an int and returns a string. Here, m is a string. Is month an int or a string? Look at your other constructor:

Date::Date()
{
	month = 1;
	day = 1;
	year = 1800;
}

month is an int here. One or both of these constructors is therefore incorrect. Since you didn't post the .h file, we don't know what all the types are.

[EDIT]
Also, the function name got messed up (I did it too). I think you're going to have to repost with more info. Post the .h file at the very least. We need to know the types and what everything represents.
[/EDIT]

OP hasn't defined a convertMonthStringToNumber(m) method yet. A class has convertMonthNumberToString method.

OP hasn't defined a convertMonthStringToNumber(m) method yet. A class has convertMonthNumberToString method.

Right, anyway, the OP marked it solved, so I guess somehow it is. it just seemed from the description that the OP actually named it correctly the first time:

convertMonthNumberToString

which matches the OP's English description:

In this program I need to convert an INT to a STRING.

The function you suggested converts a STRING to an INT.

You have to define member function convertMonthStringToNumber.

Anyway, it's marked "Solved", so I guess the OP in fact needed what you suggested, so your reading of the problem was apparently the correct one.

I actually had to change part of it around to get the program to like what adatapost's post was. Since my program accepted it without giving errors I assumed it was correct. I will post the .h program as well as the implementation program. I will try to clean it up as best as possible. It runs, but I haven't quite gotten it to do what I want it to. What I am mainly trying to do is to get all the functions to be able to work, at least partially. From there I will be able to refine my work a little bit and make it work the way it should. But for now I am wanting to focus on converting an INT into a STRING.

// Here is the .h section
#ifndef DATE_H
#define DATE_H

#include <string>
using namespace std;

class Date
{
public:
	//Member Functions
	//Constructors
	Date();
	Date(int,int,int);
	Date(string,int,int);

	//is the date valid
	void isValidDate();

	//convert int to string
	string convertMonthNumberToString(int);

	//dsplays entire date using convertMonthNum... if necessary
	void displayDate();

	//compare the dates
	int compareDates();

	//display day number
	int dayNumber();

	//increment
	void increment();

	//is it a leap year
	void isLeapYear();

	//setter
	void setDate(int,int,int);

	//getter
	int getMonth() const;
	int getDay() const;
	int getYear() const;

private:
	//Data members
	int month;
	int day;
	int year;
};
#endif //DATE_H

I'm sure I could revise that section too. Anyhow, I am required to have at least 13 items in this .h section. They are as follows:
3 Constructors
6 regular member functions as listed in my book
3 data items
1 regular member function named isLeapYear

Here is the implementation portion:

#include "testerResult.h"
#include <iostream>
#include <string>

using namespace std;

Date::Date()
{
	month = 1;
	day = 1;
	year = 1800;
}

Date::Date(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}

Date::Date(string m, int d, int y)
{
	m = convertMonthNumberToString(month);
	day = d;
	year = y;
}

int Date::compareDates()
{

	return 0;
}

string Date::convertMonthNumberToString(int month)
{
	switch (month)
  {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";  
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";

  }
}

int Date::dayNumber()
{

	return 0;
}

void Date::displayDate()
{
}

void Date::increment()
{
}

void Date::isLeapYear()
{
	if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
	{
		cout << "This is a Leap Year." << endl;
	}
	else
	{
		cout << "This is not a Leap Year." << endl;
	}
}

void Date::isValidDate()
{
	int d = 0;
	int m = 0;
	int y = 0;

	if (d < 1 || d > 31)
	{
		cout << "Invalid Date." << endl;
	}
	else if (m < 1 || m > 12)
	{
		cout << "Invalid Date." << endl;
	}
	else if (y < 1800 || y > 2100)
	{
		cout << "Invalid Date." << endl;
	}
	else
	{
		cout << "Valid Date." << endl;
	}
}

void Date::setDate(int m, int d, int y)
{
	if(m < 1 || m >12) return;
	month = m;
	cout << m;
	if(d < 1 || d > 31) return;
	day = d;
	cout << d;
	if(y < 1800 || y > 2100) return;
	year = y;
	cout << y;
}


int Date::getMonth() const
{
	return month;
}
 
int Date::getDay() const
{
	return day;
}
 
int Date::getYear() const
{
	return year;
}

So, at this current time I am just trying to work on converting an INT to a STRING... I will have to remark this post unsolved...

Read my earlier post regarding naming a parameter the same name as your class variable. Here is your function:

string Date::convertMonthNumberToString(int month)
{
	switch (month)
  {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";  
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";
  }
}

Here is what I assume your function should be (note intentional lack of function parameters) :

string Date::convertMonthNumberToString()
{
	switch (month)
  {
    case 1: return "January";
    case 2: return "February";
    case 3: return "March";
    case 4: return "April";
    case 5: return "May";
    case 6: return "June";
    case 7: return "July";  
    case 8: return "August";
    case 9: return "September";
    case 10: return "October";
    case 11: return "November";
    case 12: return "December";
  }
}

Having a local variable with the same name as a class variable always makes me screw everything up, so I avoid them like the plague. Others may be able to keep them straight better. At any rate, you have NOT declared this a static function, which you could do if month in this case was intended to not be the class variable. That's why I am guessing that the function takes no parameters. I don't know whether you have learned static versus non-static functions and variables yet. If not, probably don't worry about it and keep everything non-static.

This constructor doesn't make any sense:

Date::Date(string m, int d, int y)
{
	m = convertMonthNumberToString(month);
	day = d;
	year = y;
}

month isn't initialized, so you shouldn't be passing it to a function. m is immediately overwritten, and nothing is ever done with m, so it's a local variable that goes out of scope after the constructor is done executing.

Question: Are you supposed to STORE any strings anywhere? Right now, you have no class variables of type string.

Not quite sure about storing any strings anywhere. However, the alterations that you said to make worked perfectly. Thanks. That solves that issue. I can call this issue solved now. I appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.