By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

question: std:string and float values??

P: n/a
I am starting to use templates and have managed to figure out how to use
std::string, std::map and make_pair successfully so far (Yeah I know -
not much of a big step but I'm getting there)

Previously I was using something like:

char *buffer[50];
double value = 10.5;

sprintf(buffer, "text %.2f text", value);

How should I do this using an std:string in the place of the buffer
variable?

Thanks in advance

Sean Hannan
Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Woodster wrote:
I am starting to use templates and have managed to figure out how to use
std::string, std::map and make_pair successfully so far (Yeah I know -
not much of a big step but I'm getting there)

Previously I was using something like:

char *buffer[50];
double value = 10.5;

sprintf(buffer, "text %.2f text", value);

How should I do this using an std:string in the place of the buffer
variable?
Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());

std::cout<<buffer;

return 0;
}

This is how i'd do .. mabe experts here can show you a better way..



Thanks in advance

Sean Hannan


Jul 19 '05 #2

P: n/a
>

Sorry a small correction in the program

Use stringstream

#include <iostream>
#include <sstream>
#include <string> // I missed it last time...


int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

std::string buffer(str.str()); //In my last post it was just string...not
std::string.

std::cout<<buffer;

return 0;
}

This is how i'd do .. mabe experts here can show you a better way..


Thanks in advance

Sean Hannan


Jul 19 '05 #3

P: n/a
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());


Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'

Sean Hannan
Jul 19 '05 #4

P: n/a
Woodster <mi****@127.0.0.1> writes:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());


Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'


use std::setprecision:

#include <iomanip>
#include <sstream>

int main() {
std:: stringstream str;
double value = 10.5;

str << std::setprecision(2) << "text " << value << " text";
string buffer(str.str());
....

HTH & kind regards
frank

--
Frank Schmitt
4SC AG phone: +49 89 700763-0
e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
Jul 19 '05 #5

P: n/a
Woodster wrote:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());
Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'


Use setprecision of <iomanip>
Think this is what you ask for

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

int main()
{
std:: stringstream str;
double value = 10.590878;

str<<"text "<<std::setprecision(5)<<value<<" text"; //--> value will have
exactly five digits( before decimal point + after decimal point)

std::string buffer(str.str());

std::cout<<buffer;

return 0;
}


Sean Hannan


Jul 19 '05 #6

P: n/a
In article <MP************************@news.westnet.com.au> ,
Woodster <mi****@127.0.0.1> wrote:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());


Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'


Sure, use stream manipulators just like you would if you were writing to
cout or a file. I'm not completely "up" on C-style formatting so I'm not
sure exactly what format you want, but this might come close:

str << "text " << fixed << setprecision(2) << value << " text";

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #7

P: n/a
Thanks for the prompt response from all who replied. This will get me
going for what I am trying to do.

Regards

Sean Hannan

In article <bp**********@jtbell.presby.edu>, jt*******@presby.edu
says...
In article <MP************************@news.westnet.com.au> ,
Woodster <mi****@127.0.0.1> wrote:
In article <3F***************@adcc.alcatel.be>,
Sa********************@adcc.alcatel.be says...
Use stringstream

#include <iostream>
#include <sstream>

int main()
{
std:: stringstream str;
double value = 10.5;

str<<"text "<<value<<" text";

string buffer(str.str());


Only problem is that this does not allow formatting of the variable (ie:
%.2f in this case). Is there a way I can still format the variable
'value'


Sure, use stream manipulators just like you would if you were writing to
cout or a file. I'm not completely "up" on C-style formatting so I'm not
sure exactly what format you want, but this might come close:

str << "text " << fixed << setprecision(2) << value << " text";

Jul 19 '05 #8

P: n/a
In article <MP***********************@news.westnet.com.au>,
Woodster <mi****@127.0.0.1> wrote:

char *buffer[50];
double value = 10.5;

sprintf(buffer, "text %.2f text", value);

How should I do this using an std:string in the place of the buffer
variable?


#include <cstdio>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
// C-style method

double value = 10.5;
char buf[80];
sprintf (buf, "text %.2f text\n", value);
cout << buf;

// C++-style method

ostringstream bufstream;
bufstream << "text "
<< fixed << setprecision(2) << value
<< " text\n";
cout << bufstream.str();

return 0;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.