string variable

C

cdg

Could anyone tell me how to correctly assign a variable name to the return
of the function call in this example program. I needed to convert a integer
to a string, using stringstream. However after returning the string I am not
sure how to assign a variable name to it.

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

string Convert(int);

void main ()
{
int num = 123456789;

"???" = Convert(num).c_str() //***variable here***
}

string Convert(int num)
{
stringstream test;
test << num;

return test.str();
}
 
V

Victor Bazarov

cdg said:
Could anyone tell me how to correctly assign a variable name to the return
of the function call in this example program. I needed to convert a integer
to a string, using stringstream. However after returning the string I am not
sure how to assign a variable name to it.

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

string Convert(int);

void main ()
{
int num = 123456789;

Here you declare a variable, named 'num', of type 'int', and initialise
it with a number. Seems simple enough, no?
"???" = Convert(num).c_str() //***variable here***

Here, declare another variable, name it something, give it a type
(an appropriate type), and initialise it with a call to 'Convert'.

Now, give it your best shot.
}

string Convert(int num)
{
stringstream test;
test << num;

return test.str();
}

V
 
C

cdg

Thanks.

I have just started using strings with stringstream, and didn`t realize it
would be quite that simple.
 
R

red floyd

cdg said:
Could anyone tell me how to correctly assign a variable name to the return
of the function call in this example program. I needed to convert a integer
to a string, using stringstream. However after returning the string I am not
sure how to assign a variable name to it.

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

string Convert(int);

void main ()
{
int num = 123456789;

"???" = Convert(num).c_str() //***variable here***
}

string Convert(int num)
{
stringstream test;
test << num;

return test.str();
}

Big hint.... What does std::string::c_str() return? That should give
you an idea as to the variable type.
 
C

cdg

Could anyone help me with this next problem I am having with this same
example program. I need to get the the string length, and there is an error
message at the "strlen" line.

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

string Convert(int);

void main ()
{
int num = 123456789;
int tmlen(0);
string result;

result = Convert(num).c_str();

reslen = strlen(result);//***problem here***

cout<<reslen<<endl;
}

string Convert(int num)
{
stringstream test;
test << num;

return test.str();
}
 
J

Jim Langston

cdg said:
Could anyone help me with this next problem I am having with this same
example program. I need to get the the string length, and there is an
error
message at the "strlen" line.

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

string Convert(int);

void main ()
{
int num = 123456789;
int tmlen(0);
string result;

result = Convert(num).c_str();

just do result = Convert(num)
or even better yet
std::string result = Convert(num);
 
B

BobR

cdg wrote in message ...
Could anyone help me with this next problem I am having with this same
example program. I need to get the the string length, and there is an error
message at the "strlen" line.

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

string Convert(int);

void main (){

int main(){ // ALWAYS returns 'int'
int num = 123456789;
int tmlen(0);
// > string result;
// > result = Convert(num).c_str();

std::string result = Convert( num ).c_str();

// > reslen = strlen(result);//***problem here***

size_t reslen( result.size() );
cout<<reslen<<endl;

return 0;
}

string Convert(int num){
stringstream test;
test << num;
return test.str();
}

You don't have a book, do you?

Get "Thinking in C++", 2nd ed. Volume 1 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Then off to:
www.accu.org
 
P

Peter_Julian

| Could anyone help me with this next problem I am having with this same
| example program. I need to get the the string length, and there is an
error
| message at the "strlen" line.
|
| #include <iostream>
| #include <sstream>
| using namespace std;
|
| string Convert(int);
|
| void main ()
| {
| int num = 123456789;
| int tmlen(0);
| string result;
|
| result = Convert(num).c_str();
|
| reslen = strlen(result);//***problem here***
|
| cout<<reslen<<endl;
| }
|
| string Convert(int num)
| {
| stringstream test;
| test << num;
|
| return test.str();
| }
|

A std::string knows it own size().

#include <iostream>
#include <ostream>
#include <sstream>
#include <string>

template< class T >
std::string Convert(const T& t)
{
std::eek:stringstream oss;
oss << t;
return oss.str();
}

int main()
{
int n = 12345;
std::string s = Convert<int>(n);
std::cout << "\nsize of s = " << s.size();
std::cout << "\ns = " << s;

s += Convert<int>(6789);
std::cout << "\nsize of s = " << s.size();
std::cout << "\ns = " << s;

return 0;
}

/*
size of s = 5
s = 12345
size of s = 9
s = 123456789
*/
 
B

benben

Big hint.... What does std::string::c_str() return? That should give
you an idea as to the variable type.

I hope you haven't misled the OP much.

std::string::c_str() returns a const char*. But if you do the following:

const char* s = Convert(num).c_str();

then s will point to invalid memory. To avoid this, copy the content
before it gets destroyed:

string s1 = Convert(num);
string s2 = Convert(num).c_str();

The call to c_str() is redundant.

Regards,
Ben
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top