Thread: Parsing commands , istringstream

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Parsing commands , istringstream

    Hi,


    I got this coding working but i want to improve it.


    I want to make sure that the frist word i read is a string without numbers.


    Then i want to make sure the second / third word are int / floats...

    I was thinking in using isalpha but i donīt know if is the best way.

    And maybe change the code to switchs so i donīt have 20 (If elses)...


    Code:
    int main() {
    
    
    	//// The Map will be 60x60 (Example)
    	//Mundo myMundo(60);
    	////  going to focus the board on 15 / 15
    	//myMundo.drawTerrain(60, 60);
    
    
    
    
    	string command, parse;
    	istringstream is; // Going to get the input from the user. (String)
    
    
    	// While i got things to processsing i will do it one by one from the object not from the keyboard
    	do {
    
    
    		cout << "Command: ";
    		getline(cin, command); //Get the full string
    
    
    		// Need to convert the object before use it.
    		is.str(command);
    
    
    		// Going to transfer everything i receive in lowercase
    		transform(command.begin(), command.end(), command.begin(), tolower);
    
    
    		// If the string is diferent from "exit" i will go execute the commands of the strings.
    		if (command != "sair") {
    			while (is >> parse) {
    				if (command == "defmundo") {
    					cout << "defmundo" << endl;
    					defmundo(3);
    				}
    				else if (command == "defen")
    				{
    					cout << "defen" << endl;
    				}
    				else if (command == "defpc")
    				{
    					cout << "defpc" << endl;
    				}
    				else {
    					cout << "Command not valid" << endl;
    				}
    			}
    			// I just finished to use my string i need to clean it up to i can receive other clean one.
    			is.clear();
    		}
    		// ... Other 20 options....		
    	} while (command != "sair");
    
    
    
    
    	cout << "exit of the cycle" << endl;
    
    
    	getchar();
    	return 0;
    }
    Last edited by thinkabout; 12-10-2017 at 07:04 AM.

  2. #2
    Registered User
    Join Date
    May 2017
    Posts
    61
    I want to treat the sentence now, i need to extract the next values...

    Example: defmundo 5 2 hard (int int string)
    Example: defxpto hard 2 ( string int)
    Example: defasta hada ( string)


    Code:
    // If the string is diferent from "exit" i will go execute the commands of the strings.
            if (command != "sair") {
                while (is >> parse) {                
                    if (parse == "defmundo") {
                        cout << "defmundo is going to being call..." << endl;
                        // Now i need to extract the nexts values.
                        defmundo(value1,value2,string);
                    }
                    else if (parse == "defen")
                    {
                        cout << "defen" << endl;
                    }
                    else if (parse == "defpc")
                    {
                        cout << "defpc" << endl;
                    }
                    else {
                        cout << "Command not valid" << endl;
                    }
                }
                // I just finished to use my string i need to clean it up to i can receive other clean one.
                is.clear();
            }
            // ... Other 20 options....        
        } while (command != "sair");
    Last edited by thinkabout; 12-10-2017 at 07:52 AM.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    Work in progress...

    Code:
    using namespace std;
    
    
    bool defmundo(int size);
    bool defen(int energia);
    bool defpc(int defpc);
    int treat_command(string line);
    
    
    
    
    int main() {
    
    
    	// Need to be in the class
    	bool sair = false;
    
    
    	string line;
    
    
    	do {
    		cout << "Command: ";
    		getline(cin, line); //Get the full string	
    		sair = treat_command(line);
    	} while (!sair);
    
    
    	cout << "i just exit from the cylce";
    
    
    	getchar();
    	return 0;
    }
    
    
    int treat_command(string line) {
    
    
    	istringstream is; // Going use it so i can parse the (String).
    	string parse;
    
    
    	// Need to convert the object before use it.
    	is.str(line);
    
    
    	// Going to transfer everything i receive in lowercase
    	transform(line.begin(), line.end(), line.begin(), tolower);
    
    
    	// Getting the frist word then i am going to see if is a valid command.
    	is >> parse;
    
    
    		if (parse == "defmundo") {			
    			int size;			
    
    
    			if (is >> size) { // The reading of the int went well.
    				defmundo(size);
    			}
    			else {
    				// The read dindīt go well
    				cout << "Not the parameter i was expecting, i want a int" << endl;
    			}						
    		}
    		else if (parse == "defen") {
    			cout << "defen" << endl;
    			int energia;
    			is >> energia;
    			defen(energia);
    		}
    		else if (parse == "defpc") {
    			cout << "defpc" << endl;
    			int percentage;
    			is >> percentage;
    			defpc(percentage);
    		}
    		else if (parse == "sair") {
    			return true;
    		}
    		else {
    			// Command not valid, donīt want to process more words so i am going to return and donīt wait for other word
    			cout << "Command not valid" << endl;
    			return false;
    		}
    	// It is a valid command so i will not put sair as true.
    	return false;
    }
    
    
    
    
    
    
    bool defmundo(int size) {
    	cout << "defmundo call" << "value of size ->" << size << endl;
    	return true;
    }
    
    
    bool defen(int energia) {
    	cout << "defen call" << "value of energia" << energia << endl;
    	return true;
    }
    
    
    bool defpc(int defpc) {
    	cout << "defen decpc" << "value of percetage" << defpc << endl;
    	return true;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,489
    Start with a parse class to take a string and produce a list of tokens.
    With member functions like
    - getNumberOfTokens
    - getToken
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    130
    If you want to parse parameters passed with program execution, I'd suggest taking argc and argv into account using a for-loop:

    Code:
    int value = 0;
    
    if (argc > 1)
    {
      for (int i = 1; i < argc; i++)
      {
        if (strcmp("-foo", argv[i]) == 0 && i < (argc - 1))
        {
          value = atoi(argv[i + 1]);
          i++;
        }
        else if // ... the rest
        else
          printf("Unknown parameter: %s\n", argv[i]);
        }
      }
    }
    Last edited by Chris87; 12-12-2017 at 03:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf vs istringstream
    By KIBO in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2010, 03:00 AM
  2. istringstream
    By wind_lark in forum C++ Programming
    Replies: 6
    Last Post: 08-31-2006, 03:47 PM
  3. istringstream
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2004, 10:10 PM
  4. istringstream error
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2003, 10:03 AM
  5. istringstream.get me something!!
    By Luigi in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2003, 06:02 PM

Tags for this Thread