more c++ help...

EngenZerO

Diamond Member
Dec 24, 2001
5,099
2
0
I am having some trouble with the following stuff. I am creating a string of characters that are going to be passed as a buffer into a Unix Pipe. I decided to use Ostream to do this. I have to create the following strings and pass them into the Unix Pipe one by one. The strings I have to create are as follows: "1a", "2aa", "3aaa"..."10aaaaaaaaaa", "11a", "12aa", "13aaa" ... "20aaaaaaaaaa" ......."291a", "292aa", "293aaa", "294aaaa", "299aaaaaaaaa", "300aaaaaaaaaa".

here is my code segment. I have the correct headers declared. my problem with this though is that my str consists of all the old oss stuff. basically it remebers all the 1a 2aa, etc all the way up to 300aaaaaaaaa. Is there a way to flush the oss stream so that only one string is produced at a time so that it does not remember my previous streams? I tried the flush command but I dont think that is correct. Aslo...if you have any better suggestions on how to do this I would apprecaite it the offer, :). Thanks.


int a = 1;
int i = 1;
int j = 1;

ostringstream oss;

while (i <= 300) {

oss << i;

while (j <= a) {

oss << 'a';
j++;
}

string str = oss.str();

//need to clear the oss here I think.

cout << str << '\n';

i++;
a++;

if (a == 11) {
a = 1;
}

j = 1;

}
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
oss.str("")

or instantiate a new ostringstream for each loop iteration, instead of using the same one over and over for different strings.

And why even assign it to a string? At least for the code you have there, it would work just as well to just use oss.str() in place of str.
 

EngenZerO

Diamond Member
Dec 24, 2001
5,099
2
0
thanks...the reason I converted to the str was because I thought Unix system call write could accept str as a buffer, but unfourtinetly it doesnt.

now i have to figure out a way to do the same code but in c.
 

BFG10K

Lifer
Aug 14, 2000
22,709
3,000
126
The best way is to use a string and use = when you want to replace its contents and += when you want to append to it.

oss.str("")
That won't work. In fact you can't even pass in parameters to the str() member function so it probably won't even compile.