Convert int to string

Jul 18, 2009 at 12:18am
closed account (42ApX9L8)
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#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.
Jul 18, 2009 at 2:22am
closed account (S6k9GNh0)
A common solution is to use streams.

1
2
3
4
5
6
7
template <typename ROFL>
std::string IntToStr(ROFL tmp)
{
    std::ostringstream out;
    out << tmp;
    return out.str();
}


I think this is out ya do it.
Last edited on Jul 18, 2009 at 2:23am
Jul 18, 2009 at 2:43am
closed account (42ApX9L8)
I'm not far enough along in my programming class to use templates. I am still a few chapters away from that. Thanks for the help but I will need something a little more simplistic or slightly different.
Jul 18, 2009 at 4:39am
I can`t see anything basically wrong with your code, Why dont you write a main() driver for it and determine where the faults lie? If i had one criticism it would be that you have tried to encompass every contingency in your code (e.g leap year determination ) instead of concentrating on the conversion first. There is no indication of how the input is to be provided.
If it were cin>>m>>d>>y ; a simple enum statement could suffice.
e.g enum month{Jan=1,Feb,Mar, etc}.....but in any case the switch construct would print out the month and the remaining 2 cin inputs repeated with cout<< plus punctuation would complete the output.
hth`s
rgds
Jul 21, 2009 at 9:19am
string IntToString(int intValue) {
char *myBuff;
string strRetVal;

// Create a new char array
myBuff = new char[100];

// Set it to empty
memset(myBuff,'\0',100);

// Convert to string
itoa(intValue,myBuff,10);

// Copy the buffer into the string object
strRetVal = myBuff;

// Delete the buffer
delete[] myBuff;

return(strRetVal);
}
Jul 21, 2009 at 11:07am
itoa() is not standard.
Jul 21, 2009 at 11:56am

computerquip's solution specific to int:-

1
2
3
4
5
6
std::string IntToStr(int tmp)
{
    std::ostringstream out;
    out << tmp;
    return out.str();
}



When you are confortable with templates, use cq's code
Jul 21, 2009 at 9:52pm
itoa() is not standard.

Why does that matter? It's the easiest way to do it, why get into streams and whatnot? (:
Last edited on Jul 21, 2009 at 9:52pm
Jul 21, 2009 at 10:25pm
Did I actually just read that?

For the same reason that it matters to use int main() instead of void main(). Writing code that compiles only on select compilers is defeating the purpose of having a portable language.

How exactly is this
1
2
3
4
5
6
7
8
9
10
11
12
13
//no std:: and no sign of using namespace
string IntToString(int intValue) {
	char *myBuff;
	string strRetVal;
	//Dynamic allocation of constant size. Brilliant.
	myBuff = new char[100];
	//'\0': when 0 is just not good enough.
	memset(myBuff,'\0',100);
	itoa(intValue,myBuff,10);
	strRetVal = myBuff;
	delete[] myBuff;
	return(strRetVal);
}
simpler than this?
1
2
3
4
5
std::string itoa(long n){
	std::stringstream stream;
	stream <<n;
	return stream.str();
}


What's more, std::stringstream lets you mess around with templates:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
std::basic_string<T> itoa(long n,unsigned w=0){
	std::basic_stringstream<T> stream;
	if (w){
		stream.fill('0');
		stream.width(w);
	}
	stream <<n;
	return stream.str();
}

//sample calls:
std::string s=itoa<char>(1024);    //"1024"
std::wstring s2=itoa<wchar_t>(1024,8); //L"00001024" 
Last edited on Jul 21, 2009 at 10:25pm
Topic archived. No new replies allowed.