Learn how to a build a cloud-first strategyRegister Now


evaluating a string

Posted on 2007-10-19
Medium Priority
Last Modified: 2010-04-01
hey guys,

i have a function that opens a text file and reads off one line at a time. the line of text is stored in a string.

an example of a line within the file is:

paint box 4 <110 110> <110 190> <190 110> <110 110>

however the problem is that not all values/words will be separated by one white space, there can be an unknown number of them at once.

the aim is to verify the first two words, then stick the rest of the numbers into variables.

whats the easiest way i can do this?

Question by:gem56
  • 3
  • 2
LVL 53

Expert Comment

ID: 20110537
Is this a different question than this one :


? If not, can you say which part you're still having trouble with ?

Author Comment

ID: 20114059
yeah sorry champ, its the same sort of question, but with those solutions im having too many problems trying to make it work with what i need. mainly the solutions are too complex and the output isnt helping me too much.

im just looking for something simple (simpler) that i can use to evaluate the string sequentially.


Author Comment

ID: 20114240
LVL 53

Accepted Solution

Infinity08 earned 1000 total points
ID: 20114270
Did you read the tutorial on file intput/output that I posted in the other post :


That's quite basic C++, and is something that you really have to understand if you want to get into C++. So, let me know if there's something that is unclear there. Especially take a look at the code samples in that tutorial.

>> im just looking for something simple (simpler) that i can use to evaluate the string sequentially.

It can't get much simpler than what I suggested :

        1) read a line from the file into a string (using getline)
        2) read each token (word) from the line one at a time, using stream operations. For example,

                  string line;        // <--- filled up from the file by getline
                  stringstream ss(line);               // <--- create a stringstream for the line

                  string word;
                  ss >> word;           // <--- read the first word from the line and store it in word
                  // <--- do something with the word

                  ss >> word;           // <--- read the second word from the line and store it in word
                  // <--- do something with the word

                  etc. ...

        3) go back to 1) for the next line

Does that make it clearer ? I would suggest to just read the tutorial and on-line documents I posted earlier, and then just play with it a bit to see if you can get things to work. Start with basic things, like reading a line from the file, and printing it, and then start expanding the code to do more complex things ...

Author Comment

ID: 20114293

looks like i'll have to find a tutorial that explains stringstream's better (the one i have has about 3 sentences on it).

thanks a lot mate, exactly what i needed :)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question