I want to take an input as a integer and then concatenate it with a string. This will be for multiple times. The output will be the previous string with this new integer value. After taking input of an integer number I used stringstream object for converting it. Then I concatenate it. But I’ve got expected output for the first time. But the next time when I take input from the user and try to concatenate it with the previous string output string in the concatenated part is the same of my first input integer. So how can I use this stringstream object for further use.
Here is my code:
string s = "Previous Choices : ";
int n;
string d;
stringstream ss;
while(1) {
cin>>n;
ss << n;
ss >> d;
s += d;
cout<<s<<” ”<<endl;
}
My inputs
10
20
30
My expected output is
Previous Choices : 10
Previous Choices : 10 20
Previous Choices : 10 20 30
But the output is coming like this:
Previous Choices : 10
Previous Choices : 10 10
Previous Choices : 10 10 10