Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting strings to display correctly

Status
Not open for further replies.

jag0666

Programmer
Nov 17, 2003
2
US
Okay, here is my problem.

I need to display a report, but I have to use a vector of strings to do this. It was suggested to me to convert the strings to a char array to do the formatting and then convert back to strings. Here's an example of what I have to do....

Number of points: 20 SD: 8.733
Mean: 46.112 UCL: 72.987
LCL: 19.986

Where each line of that section would be a string. I'm not sure how I can get each field to line up correctly like that. I'm fairly new with programming in C++, so maybe it's just my lack of knowledge in the language that is giving me problems.

Anyone have any suggestions?

thanks!
Ben
 
Use sprintf().

say for
Number of points: 20 SD: 8.733

say you have
int NumPoints = 20;
float SD = 8.733;
char outStr[100];

sprintf(outStr,"Number of points: %d \t\t SD: %f\n",
NumPoints , SD );

hope this helps

 
I don't understand, which source data you have: (unformatted?) strings or native values (ints, doubles etc)?
1st case: you must parse column values before format its again. It's not so easy and not so hard task...
2nd case: use string based streams to format, why sprintf here?
Somewhat exotic know-how (freeware;): try generate html file to format your data in (pro;) table format report! You need only <table borders=0>, <tr> and <td> tags (plus <html>, <head>, <body>, of course). Then print its off-line via browser...
 
Thanks for the replies.

What I need to do is format the lines as strings. For instance,

Number of points: 20 SD: 8.733

Would be one string in a vector of strings.
 
You can use ostringstream for this.

#include <sstream>
#include <iostream>
#include <vector>

using namespace std;

int main(void)
{
ostringstream stream;
stream<<&quot;Number of points: &quot;<<20<<&quot;\t&quot;<<&quot;SD:&quot;<<8.733;
string str(stream.str());
cout<<str<<endl;
return 0;
}

Sean McEligot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top