Use istream_iterator to read various data types
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
int main()
{
int i;
double d;
string str;
vector<int> vi;
vector<double> vd;
vector<string> vs;
cout << "Enter some integers, enter 0 to stop.";
istream_iterator<int> int_itr(cin);
do {
i = *int_itr;
if(i != 0) {
vi.push_back(i);
++int_itr;
}
} while (i != 0);
cout << "Enter some doubles, enter 0 to stop.";
istream_iterator<double> double_itr(cin);
do {
d = *double_itr;
if(d != 0.0) {
vd.push_back(d);
++double_itr;
}
} while (d != 0.0);
cout << "Enter some strings, enter 'quit' to stop.";
istream_iterator<string> string_itr(cin);
do {
str = *string_itr;
if(str != "quit") {
vs.push_back(str);
++string_itr;
}
} while (str != "quit");
cout << "Here is what you entered:\n";
for(i = 0; i <vi.size(); i++)
cout << vi[ i ] << " ";
cout << endl;
for(i = 0; i <vd.size(); i++)
cout << vd[ i ] << " ";
cout << endl;
for(i = 0; i <vs.size(); i++)
cout << vs[ i ] << " ";
return 0;
}
Related examples in the same category