string conversion problem

C

cdg

Could anyone explain how to write this sample program correctly. I need to
convert an integer to a string. However, I would prefer to convert the
integer to char array. But I didn`t want to use "itoa".
And I am not that familar with using "stringstream". And is there better
approach to converting integers to char arrays.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

char StringTest(int);

void main ()
{
int num = 123456789;
char num_ch = StringTest(num);
cout<<num_ch<<endl; //***this is to test print***
}

char StringTest(int num)
{
stringstream test;
test << num;
string int_str = test.str();

return int_str; //***needs to convert to char***
}
 
J

Jim Langston

cdg said:
Could anyone explain how to write this sample program correctly. I need
to
convert an integer to a string. However, I would prefer to convert the
integer to char array. But I didn`t want to use "itoa".
And I am not that familar with using "stringstream". And is there better
approach to converting integers to char arrays.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

char StringTest(int);

void main ()
{
int num = 123456789;
char num_ch = StringTest(num);
cout<<num_ch<<endl; //***this is to test print***
}

char StringTest(int num)
{
stringstream test;
test << num;
string int_str = test.str();

return int_str; //***needs to convert to char***
}

Just have StringTest return a std::string and take the c_str() of it to
wherever you want it to go. Then you could even do:

char MyCStyleString[1000];

strcpy( MyCStyleString, StringTest(num).c_str() );

strcpy's 2nd parm is a const char*, which the c_str() gives you.

I would do it this way. Other's might save the results of StringTest to a
temp var then do the strcpy. Of course, if all you need is a const c-style
string that you'll not change, why not just leave it as std::string in a var
and use .c_str() when you need the c-style version?
 
C

cdg

In the program that I am writing, the integer that I need to convert to a
"char array" would not actually be a constant. So, how would I write the
code to save the results of StringTest to a temp variable. This is the where
I am having a problem.
And then I would need to either return the "temp var" or use "strcpy" in
the StringTest function, and return the char array. Either way would not be
a problem, but unfortunately I am not sure how to write most of this.

void main ( )
{
StringTest(num);
}

char StringTest(int num)
{
stringstream test;
test << num;
string int_str = test.str();

return( ??? );
}
 
P

Peter_Julian

| Could anyone explain how to write this sample program correctly. I need
to
| convert an integer to a string. However, I would prefer to convert the
| integer to char array. But I didn`t want to use "itoa".
| And I am not that familar with using "stringstream". And is there better
| approach to converting integers to char arrays.
|
| #include <iostream>
| #include <sstream>
| #include <string>
| using namespace std;
|
| char StringTest(int);
|
| void main ()
| {
| int num = 123456789;
| char num_ch = StringTest(num);
| cout<<num_ch<<endl; //***this is to test print***
| }
|
| char StringTest(int num)
| {
| stringstream test;
| test << num;
| string int_str = test.str();
|
| return int_str; //***needs to convert to char***
| }
|

A std::string is a container, a char is a primitive type. A pointer to char
may point to a single char or to an array of chars. Don't mix these. If you
have a std::string and you want a constant char* to it,
use c_str():

std::string s("my string");
char* p_s = s.c_str();

A std::string does not need a terminator. The result of what the statement
above generates is a distinct constant char array with a terminator at *p_s:

{'m', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g', '0x0'}

Allow me to indulge in the topic a little.
A pointer's most important attribute is its type. A pointer to an integer is
not a pointer to an array of integers, etc. Typically, a pointer to a
container is what should be used instead.

The char* and const char* break that rule blatantly for historical
preservation of compatibility with C. A very unfortunate reality.

In the case of a std::string or any user-type, why deal with the uglyness
when you can simply pass a std::string* or better yet a reference to a
std::string:

void foo(const std::string& ref_s)
{
...
}

std::string n("my string");
foo(n);

Its much easier to program without pointers. In fact, the only reason to use
pointers at all in C++ is polymorphism allocations, functors along with a
few other exceptions.
 
G

Gavin Deane

cdg said:
In the program that I am writing, the integer that I need to convert to a
"char array" would not actually be a constant. So, how would I write the
code to save the results of StringTest to a temp variable. This is the where
I am having a problem.
And then I would need to either return the "temp var" or use "strcpy" in
the StringTest function, and return the char array. Either way would not be
a problem, but unfortunately I am not sure how to write most of this.

As Jim Langston said, return a std::string and convert to C-string
array-of-char at the last possible minute.

// You need a prototype for StringTest here.
void main ( )
main must return int, not void.
{
StringTest(num);
std::string s = StringTest(num); // Assuming num is an int with a valid
value.
}

char StringTest(int num)

You're talking about returning a C-string but you've declared the
function to return a single char. Leave C-strings and pointers alone
until you absolutely can't avoid them.

std::string StringTest(int num)
{
stringstream test;
test << num;

return test.str();
}

This bit below can go.
string int_str = test.str();

return( ??? );
}

The StringTest function is now responsible for converting an int to a
string. Elsewhere in your code, you can use the c_str() member function
of the std::string class if and when you really need a C-string.

Gavin Deane
 
M

Maxim Yegorushkin

cdg said:
Could anyone explain how to write this sample program correctly. I need to
convert an integer to a string. However, I would prefer to convert the
integer to char array. But I didn`t want to use "itoa".

Can you use snprintf?

#include <cstdio>
#include <limits>

int main(int ac, char** av)
{
char buf[
std::numeric_limits<int>::digits10
+ std::numeric_limits<int>::is_signed
+ 1 // for the most significant digit
+ 1 // for the trailing zero
];
std::snprintf(buf, sizeof buf, "%d", ac);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top