Page 2 of 2 FirstFirst 12
Results 11 to 11 of 11

Thread: [C++] getting multiple arguments

  1. #11
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: [C++] getting multiple arguments

    Quote Originally Posted by yun4 View Post
    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()
    {
      
    vectorData dataVec;

      
    cout << "Enter your data, and then a blank line to mark the end..." << endl;

      while ( 
    true )
      {
        
    string input;
        
    getlinecininput );

        if ( 
    input == "" )
        {
          break;
        }
        else
        {
          
    istringstream istrinput );

          
    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);
        }
      }

      for ( 
    int i 0dataVec.size(); ++)
      {
        const 
    Data &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
    Last edited by dwhitney67; April 14th, 2008 at 08:27 PM.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •