How to add array elements to stringstream?
Oct 17, 2013 at 4:49am UTC
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 25 26 27 28 29 30
#include <iostream>
#include <string>
#include <istream>
#include <sstream>
using namespace std;
int main()
{
string groups[3] = {};
int groupSize[3] = {}, total = 0;
stringstream output;
for (int a=0; a<3; a++)
{
cout << "Give group name :" ;
cin >> groups[a];
cout << "Give population size :" ;
while (!(cin >> groupSize[a]))
{
cout << "Invalid input. Only integers allowed. " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
}
output << groups[a] << ' ' << groupSize[a] << '\n' ;
}
cout << output;
cin.sync();
cin.get();
return 0;
}
It outputs jibberish. I can do what I need to do but I would need to declare more variables and write more cout's, isn't there a way to add these elements to a stringstream or streambuffer? My goal is to write this program and make it as comprehensive as possible but also with very few lines.
Oct 17, 2013 at 5:02am UTC
#include <limits>
line 10:
1 2
// stringstream output;
ostringstream output ;
line 25:
1 2
// cout << output;
cout << output.str() ;
line 26:
// cin.sync(); // avoid
Topic archived. No new replies allowed.