Originally Posted by
yun4
i wrote a simple program to see if it works
Code:
#include <iostream>
#include <string>
#include <list>
using namespace std;
int main() {
string arguments;
list<double> x,y;
list<string> name;
while(getline(cin,arguments)) {
istringstream iss(arguments);
while(iss) {
x.push_back(iss);
y.push_back(iss);
name.push_back(iss);
}
}
cout << x;
cout << y;
cout << name;
}
I keep getting long error msg when i compile am i doing it wrong ?
Yep, your program does generate a lot of compile errors. Here's a little program I threw together the other day when I first read your post. I'm sure it could be done better! Take a look at how I use the istringstream. That will at least explain why your program is generating compile errors.
PHP Code:
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;
struct Data
{
double value1;
double value2;
string name;
};
int main()
{
vector< Data > dataVec;
cout << "Enter your data, and then a blank line to mark the end..." << endl;
while ( true )
{
string input;
getline( cin, input );
if ( input == "" )
{
break;
}
else
{
istringstream istr( input );
Data d;
istr >> d.value1 >> d.value2;
// allow for multiple-word phrases (i.e. strings)
while ( !istr.eof() )
{
string temp;
istr >> temp;
d.name += (temp + " ");
}
dataVec.push_back( d );
}
}
for ( int i = 0; i < dataVec.size(); ++i )
{
const Data &d = dataVec[i];
cout << "value 1 = " << d.value1 << ", value2 = " << d.value2 << ", name = " << d.name << endl;
}
return 0;
}
P.S. This program will parse inputs such as:
2.2 4.4 string
5.6 7.8 string1 string2
Bookmarks