STORING IN AN ARRAY

Suppose a function prints out the numbers : 1 2, 3, 4, 5, 6

is there any methods to store the output numbers into the new array?
Here is how to store the first in an array. I'll leave the rest to you.

newArray[0] = 1;

Not brilliantly helpful, I know, but your question is so vague that it's impossible to answer any more specifically.
Last edited on
@ Moschops: thanks for the reply but it's not helpful
As helpful as your original question.

I'm going to guess that your function outputs to cout. In the absence of information, I have to guess. You can replace the streambuf associated with cout. Then, you can get the data from that buffer. In this example, the function outputting the number 1 2 3 4 5 6 is in the middle. Instead of going to screen, the output is put into the buffer, which is then dumped to a string and after cout is restored, output to screen. You could easily put it into an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>

using namespace std;


int main()
{
  stringstream replacementStream;

  // replace the cout buffer with your redirected buffer :
  streambuf *oldbuf = cout.rdbuf(replacementStream.rdbuf());

  { // Placeholder for a function sending values to cout
    cout << 1 << 2 << 3 << 4 << 5 << 6;
  }
  string line;
  getline(replacementStream, line);


  // put cout back
  cout.rdbuf(oldbuf);
  cout << line;

}







Last edited on
@Moschops: Thanks for your reply.. I already tackle this another way. Hope I'll use your idea next time.
Topic archived. No new replies allowed.