Stdin Line Parsing C++

This is a discussion on Stdin Line Parsing C++ within the C++ Programming forums, part of the General Programming Boards category; Hello all, I'm working on a simple drawing program in C++ that out puts postscript commands. Since I'm still pretty ...

  1. #1
    Registered User
    Join Date
    May 2010

    Unhappy Stdin Line Parsing C++

    Hello all, I'm working on a simple drawing program in C++ that out puts postscript commands. Since I'm still pretty new to C++, I'm not sure if there is a better and more reliable way to dissect the input that I'm getting from the user.

    The input looks like this:

    Any amount of whitespace (including 0) is allowed between a parenthesis and the start or end of a command, and between commands. At least one character of whitespace is required to separate arguments within a command.

    For example, the following would draw a thick green square, rotated 45 degrees.
    (linewidth 5)
    (color 0 0 1)
    (linear 0.7071 0.7071 -0.7071 0.7071)(rect 100 100 100 100)

    My current parse looks like this:

    while(getline(cin, s1))
    	    cstr = new char [s1.size()+1];
    	    strcpy (cstr, s1.c_str());
    	    p = strtok (cstr," ()");
                s2 = p;
    	    s2 = p;
    	    if("color") == 0){
    	    if("linewidth") == 0){
    	    if("line") == 0){
    	    if(("rect") == 0) || ("filledrect") == 0)){
    	    if(("tri") == 0) || ("filledtri") == 0)){
    	    if(("square") == 0) || ("filledsquare") == 0)){
    	}while(p = strtok (NULL," ()"));
    This works well for the one line inputs like the color or linewidth but as seen in the above example input there can be a bunch of linears on the same line followed by a shape. I know there is a better way to parse the lines i get but i can't seem to figure it out. i was messing around with while and do while loops and came up with this. Any comments/feedback would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Aug 2005
    not sure if this will work but worth a try

    first replace all '(' with spaces , then replace all ')' with new lines
    then insert into a stream and use getline on it...for each line insert it into a stream and parse it.

    #include <algorithm>
    #include <string>
    #include <sstream>
    int main()
    	string command;
    	while( getline(cin, command) )
    		replace(command.begin(), command.end(), '(', ' ');
    		replace(command.begin(), command.end(), ')', '\n');
    		istringstream iss(command);
    		string parted_command;
    		while( getline( iss, parted_command) )
    			istringstream iss2(parted_command);
    			string argument;
    			iss2 >> argument;
    			int current_number;
    			while( iss2 >> current_number)
    				//work with the number depending on the what argument is.
    Last edited by rodrigorules; 05-13-2010 at 10:41 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The line issue seems to be a red herring, since it seems that any amount of any commands can appear on the same physical line (and that it's just coincidence that some of your example commands are on a line by themselves). You can use the extra parameter to getline to say "read until this particular character" to get everything up to the closing ). (This assumes that parentheses can't appear inside a command.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 08:26 AM
  3. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 10:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21