streamstring useage

I wrote a program
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
	int n;
	cin >> n;
	int *data = new int[n];
	string one;
	stringstream two;
	getline(cin >> ws, one);
	for(int i = 0; i < n && (two << one); i++){
		two >> *(data+i);
	}
	for(int i = 0; i < n; i++){
		cout << *(data+i) << " ";
	}
	cout << "test " << *(data+8) << endl;
	system("pause");
	return 0;
}


This is my input:

1
2
10
1 2 3 4 5 6 7 8 9


This is my execution result:

1
2
3
10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 91 2 test 91


I have no idea why *(data+8) is 91 instead of 9.
Can someone explain what happened?
Last edited on
Your code is bizarre. Your main mistakes are:
(a) You put the same string "1 2 3 4 5 6 7 8 9" into the stringstream TEN times!
(b) You specified 10 numbers and then gave it 9 as input!

Note that ALL your second line of input is taken in on line 12.

I suspect what you intended would be something like the following. Note that the input is only put into the stringstream (or "internal file") once. Make sure that you give it TEN numbers at input; thus
10
1 2 3 4 5 6 7 8 9 10


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
	int n;
	cin >> n;
	int *data = new int[n];
	string one;
	stringstream two;
	getline(cin >> ws, one);              // <=== all input taken here in one go
	two << one;                           // <=== string is only put in the stringstream ONCE
	for(int i = 0; i < n; i++){           // <=== your loop is only to fill data[] from the stringstream
		two >> *(data+i);
	}
	for(int i = 0; i < n; i++){
		cout << *(data+i) << " ";
	}
	cout << "test " << *(data+8) << endl;
	system("pause");
	return 0;
}


10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 test 9



So what happened in your original code? Well you put the same (nine) numbers in the stringstream one after another n(=10) times ... and you didn't have a following gap. So, after your first loop your stringstream called "two" actually held (!)
1 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 9

(as you could check with cout << two.str() << '\n';
Note that there is no gap between each 9 and the next one, so it will be read back as 91.

So, the ten numbers assigned to data[] were:
1 2 3 4 5 6 7 8 91 2
and the (1+8)th one of those is 91. As your program dutifully wrote out for you.
Last edited on
Thanks lastchance.
After read your answer:
I play around with the code.
I change the code to this.
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
	int n;
	cin >> n;
	int *data = new int[n];
	string one;
	stringstream two;
	getline(cin >> ws, one);
	two << one;
	two << one;
	for(int i = 0; i < 10 && (two >> *(data+i)); i++){
	    
	}
	for(int i = 0; i < n; i++){
		cout << *(data+i) << " ";
	}
	cout << "test " << *(data+8) << endl;
	system("pause");
	return 0;
}


Its actually work.

This is my execution result:

1
2
3
10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 91 2 test 91


The string is only put in the stringstream ONCE is actually the problem. The 1 appear after 9 when i read one two times. When i read one one time the 1 will not appear after 9. Cool man you really hit the point. Thanks a lot for your answer.

Ha ha I tempted do it a lot of time, since i usually do this:

1
2
3
for(int i = 0; i < n; i++){
    cin >> *(data+i);
}


Thank you a lot for helping.
Topic archived. No new replies allowed.