Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string variable

Reply
Thread Tools

string variable

 
 
cdg
Guest
Posts: n/a
 
      03-07-2006
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();
}


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2006
cdg wrote:
> 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
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
 
cdg
Guest
Posts: n/a
 
      03-07-2006
Thanks.

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


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-07-2006
cdg wrote:
> 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.


 
Reply With Quote
 
cdg
Guest
Posts: n/a
 
      03-08-2006
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();
}


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-08-2006
"cdg" <(E-Mail Removed)> wrote in message
news:EspPf.10426$Tf3.5711@dukeread09...
> 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);

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


result.length();

> cout<<reslen<<endl;
> }
>
> string Convert(int num)
> {
> stringstream test;
> test << num;
>
> return test.str();
> }
>
>



 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      03-08-2006

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/...ngInCPP2e.html

Then off to:
www.accu.org

--
Bob R
POVrookie


 
Reply With Quote
 
Peter_Julian
Guest
Posts: n/a
 
      03-08-2006

"cdg" <(E-Mail Removed)> wrote in message
news:EspPf.10426$Tf3.5711@dukeread09...
| 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: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
*/

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      03-08-2006
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Evaluate an string variable's value to a variable Mir Nazim Python 2 12-21-2008 07:05 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
How to set a variable to a not modifying string expression using a variable Stefan Mueller Javascript 9 11-01-2005 09:06 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
noob question: Trying to extract part of a string in a variable to another variable cayenne Perl Misc 19 05-19-2004 11:22 PM



Advertisments