• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

evaluating a string, part 3

hey again,

ok this is what i have:

// Start

stringstream ssFileLine;
string stFileWord;
string stFileLine;

// ...a whole bunch of other code....

// ssFileLine contains all the text in a file that its read. its been formatted so that theres a word, space, word.
// example: 'colour 255 52 0 draw Point 52 145 draw Point 45 154


      // Keep outputing word by word
      while(ssFileLine >> stFileWord)
      {

            // If the outputted word is either 'colour' or 'draw'
            if (stFileWord == "colour" || stFileWord == "draw")
                  
                  // New line with command word
                  stFileLine = stFileWord;

            else
            {

                  // Otherwise, keep concating command
                  stFileLine += " " + stFileWord;


                        // PROBLEM AREA
                  stFileWord = ssFileLine.peek();
                  cout << "-- " << stFileWord;


                  // If the next word is a command
                  if (stFileWord == "colour" || stFileWord == "draw")
                  {

                        // Command Line is finished, do something
                        //.....

                        cout << stFileLine;         // For debugging to check that string is complete
                  }
            }
      }

// End

The problem im having is that im trying to get an entire command into a string (a command is a line that begins with either colour or draw. it last until the next colour or draw occurs. eg, 'draw Point 52 145').

i cant figure out how to check the next word in the stream without actually extracting that word. originally i tried extracting the word, checking it, if it was a 'command' word, then 'ssFileLine.str = stFileWord + " " + stFileLine.str' .....didnt work so well as i had errors about conversion types or something.

does anyone have any ideas about how i can go about this?

thanks
0
gem56
Asked:
gem56
  • 3
  • 2
  • 2
1 Solution
 
Infinity08Commented:
>> does anyone have any ideas about how i can go about this?

Why didn't you keep the input line based like you had in your earlier questions ? Then you wouldn't need to detect where a new line starts etc.
0
 
lucky_jamesCommented:
why r u using peek??

your algorithm shud be something like :

while (reading the new word)
{
 if its command
{
  bconcatenate  = false;
}
else
{
  bconcatenate  = true;
}

if(bconcatenate)
{
         //implement your concatenation logic here.
}
}


regards,
..
0
 
lucky_jamesCommented:
<<<<<<while (reading the new word)
this loop shud break only when you have reached the end of the stream buffer i.e. there is nothing more to read.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
gem56Author Commented:
the reason why i've changed from my earlier code is becuase i have to cater for when a single command line is split over multiple lines in my text file. therefore i would have to do a mini concating loop anyway until i find the start of the next command line.

thanks for that suggestion james, give me a sec, let me see if i can write something up....
0
 
Infinity08Commented:
>> becuase i have to cater for when a single command line is split over multiple lines in my text file.

What's the reason that that is possible ? Why not keep all commands on one line ? Makes things a lot easier ...

In any case, it's still better to use the line-based approach, because you then only have to check the first word of the line to see if it is the start of a command or not. If it is, then you start a new command interpretation. If it's not, then you know you have to continue processing the previous command ...
0
 
gem56Author Commented:
ok, i've written the following:

// Start

      bool bNeedConcat;                        // For wheather we need to concat the string

      // Reset string variables
      stFileLine = "";
      stFileWord = "";

      while (ssFileLine >> stFileWord)
      {

            if (stFileWord == "colour" || stFileWord == "draw")
                  bNeedConcat = false;
            else
                  bNeedConcat = true;

            if (bNeedConcat)
            {
                  stFileLine += stFileWord + " ";      
            }
            else
            {
                  if (stFileLine.length() > 0)
                  {
                        // Command is complete, do something
                        cout << stFileLine << endl;

                        // Reset string for next command
                        stFileLine = "";
                  }

            }

      }

// End

It seems to work quite well, and its a lot more clearer code. awesome.


the reason why a single command can be split over multiple lines is just so i can add 'flexibility' to the solution. im more rusty with exceptions etc, so i figured this would be the easier way at the moment.

thanks to both of you guys :)
0
 
Infinity08Commented:
Then there's still no need to check every word ... You just need to check the first word of the line as I said in my previous post. Your code will be a lot faster, and a lot clearer.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now