Thread: set manipulation 0

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    set manipulation 0

    As part of my assignment I have to create an array of ints based on user input. lets say that the user types in the following command:
    Code:
    > create 3 5 6 1 8
    I have to make an array with those ints in it and of precisely that size. The way my program is set up is that I input the comman word into a string, compare it, and then depending on what it is do further things (look at code below). After that I have to parse the next input taking out the ints seperated by whitespace...I think you know what I mean.

    I really don't want to use strtok(), but it looks like I will have to, any other ideas?

    Code:
    void getCommand()
    {
    	int firstVal = 0;	//these will hold the values following each command 
    	int secondVal = 0;	//except create, setids, and quit
    	Sets object;
    	string command;		//holds the first word of the user inputed command
    	string set;
    	
    	cout << ">  ";
    	cin >> command;
    
    	//create a set
    	if( command == "create" )
    	{
    		cout << endl << command;
    		//problem lies here
    	}
    	//destroy set
    	else if( command == "destroy" )
    	{
    		cin >> firstVal;
    		object.set( firstVal );
    	}		
    ........... and so on for other set manipulating functions
    thanks

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You could get the entire line into a string, then construct an istringstream object to use.
    Code:
    #include <iostream>
    #include <sstream>
    #include <strstream>
    using namespace std;
    
    int main()
    {
        istringstream in("1 2 3 4 5 6 7 8 9");
    
        int i;
        while(in.good())
        {
            in >> i;
            cout << i << " ";
        }//while
    
        return 0;
    }//main
    gg

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    thanks codeplug, I'm not familiar with those libraries but I'll try looking them up because your solution looks much nicer then mine.... I ended up using strtok() for now until I think of something else, and I also ended up writting partially in C....why is it that some stuff is so much easier in C????? anyways here is what I did:

    Code:
    void getCommand()
    {
    	int firstVal = 0;	//these will hold the values following each command 
    	int secondVal = 0;	//except create, setids, and quit
    	int x, i=0;			//simple counter
    	char buff[256];		//buffer for creation of a set
    	char *p;
    
    	Sets object;		
    	string command;		//holds the first word of the user inputed command
    	string set;
    	
    	cout << ">  ";
    	cin >> command;
    
    	//create a set
    	if( command == "create" )
    	{
    		cout << endl << command;
    		
    		gets( buff );
    		p = strtok( buff, " " );
    
    		while( p != NULL )
    		{
    			if( sscanf( p, "%d", &x ) )
    				cout << p;
    			else
    				printf( "error on token %d: integer expected" );
    
    			i++;
    			p = strtok( NULL, " " );
    		}
    	}
    any other suggestions are more then welcome!

    edit:: the above code is partially made for debugging; the actual int is sent to an array in the program

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    codeplug I've run your code, and you either misuderstood what I ment or there is something wrong with the code itself.

    When I (user) input any set of ints, it still prints out 1,2,.....9

    can you explain the consept of your code for me a bit?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another way
    Code:
    #include <iostream>
    #include <vector>
    
    int main(void)
    {
      std::vector<int> myArray;
      std::string command;
      int i;
      
      std::cin >> command;
      std::cout <<"Running command: " <<command <<std::endl;
      while (std::cin >> i && i != -1)
      {
        myArray.push_back(i);
      }
    
      for (std::vector<int>::iterator it = myArray.begin(); it != myArray.end(); ++it )
      {
        std::cout <<*it <<" ";
      } 
      
      std::cout <<std::endl;
      
      return(0);
    }
    
    
    /* 
    My input/output:
    
    blahblah 1 2 3 4 5 -1
    Running command: blahblah
    1 2 3 4 5
    
    */
    With the stringstreams, you have to read the input in a string first, then pass that string to a stringstream. This might help (loosely!)
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Thanks Hammer, I've also seldom worked with vectors but they are much more familiar to me then what codeplug showed. Anyways, is there a way of setting the limit to the while loop to be a return (newline) rather then a '-1'?

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM
  5. quick question about bit manipulation
    By PJYelton in forum C++ Programming
    Replies: 7
    Last Post: 01-10-2003, 01:18 AM