473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

string variable

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();
}
Mar 7 '06 #1
8 2124
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
Mar 7 '06 #2
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.
Mar 7 '06 #3
cdg
Thanks.

I have just started using strings with stringstream, and didn`t realize it
would be quite that simple.
Mar 7 '06 #4
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();
}
Mar 8 '06 #5
"cdg" <an****@anywhere.com> 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();
}

Mar 8 '06 #6

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
Mar 8 '06 #7

"cdg" <an****@anywhere.com> 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::ostringstream 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
*/

Mar 8 '06 #8
> 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
Mar 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
4
by: JohnR | last post by:
Hi all, I'm finally sick and tired of manually generating get/set properties for each private variable in a class so I'm trying to create a macro to do it. I'm stuck because I can't figure out...
2
by: HerbD | last post by:
I have a loooong debugging session behind me! I finally found the reason for the problem and now would like to know, if it is a bug in my code or not standardconformant behavour of the compiler(s) or...
21
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
5
by: cameljs18 | last post by:
Converting a string variable into a string literal. How do I add the @ character in front of the string? I cannot add it when the string is created as it will affect other parts of the program. ...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
4
by: mthread | last post by:
Hi, I am using a string variable in which I do lot of appending. The only difficulty I am facing as of now is appending a integer/float value to this variable. Although I can accomplish this task...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.