?
Solved

evaluating a string, part 2

Posted on 2007-10-20
7
Medium Priority
?
224 Views
Last Modified: 2010-04-01
hello,

ok so im having some success with my string manipulation, but i still have a problem.

this is what i have so far:

// Start
        ifstream fInputVec(stVecFile.c_str(), std::ios::in);

        string stFileLine;

      while(getline(fInputVec, stFileLine))
      {
            string stFileWord;
            stringstream ssFileLine(stFileLine);

            while (ssFileLine.good())
            {
                  ssFileLine >> stFileWord;

                  // If string is a blank space, skip to next >>
                  if (stFileWord.length() == 0)
                        continue;
                  
                       // PROBLEM IS HERE
                  if (stFileWord.substr(1, 1) == "<")
                        stFileWord = stFileWord.substr(2, stFileWord.length() - 1);
                  

                  cout << stFileWord << endl;
            }

      }

// End

ok so the problem im having is that in my stream i will have the two '<' '>' characters either at the beginning of the stream or at the end. i need to basically delete them from my stream.

the above code was an attempt to resize my string if the '<' char was at the start of it. it doesnt work. for some reason the program just doesnt realise that '<' is the first letter of the string.

does anyone have any working and more efficient solutions?

thanks

0
Comment
Question by:gem56
  • 4
  • 3
7 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 20114515
>>                   if (stFileWord.substr(1, 1) == "<")

Indexing in strings is 0-based : the first character has index 0, not index 1. So, make that :

                  if (stFileWord.substr(0, 1) == "<")

Faster would be :

                  if (stFileWord[0] == '<')

0
 

Author Comment

by:gem56
ID: 20114694
champion, thanks again.
0
 

Author Comment

by:gem56
ID: 20114729
sorry to keep breaking b@lls mate, i've updated the code (i had an error with stFileWord[0] saying it couldnt convert between char* and int, so i used the substr from 0 index instead, works fine), ok so now im faced with another problem. the chars '<' and '>' have to be removed from my stream, so instead of doing it after i've sent my stream into a word (ssFileLine >> stFileWord;) and then checking each word, i figured it would be better to remove all instances of '<' and '>' after my getline (makes it easy and less clattered when i have to check if the length > 0)

i've looked at the strings .replace fuction, however it doesnt automatically search the string and replace what you want (like in VB :( ), the understanding i got was that you had to give it a position in the string at which to replace with your new character. would this be the easiest function/method to use to remove these two chars from my string?

.....also, how would i go about doing it?

thanks again
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 53

Expert Comment

by:Infinity08
ID: 20115364
>> (i had an error with stFileWord[0] saying it couldnt convert between char* and int, so i used the substr from 0 index instead, works fine)

Did you use the exact code I posted :

                  if (stFileWord[0] == '<')

Note the single quotes instead of the double quotes !


>> .....also, how would i go about doing it?

You can use a combination of find and erase :

        http://www.cplusplus.com/reference/string/string/find.html
        http://www.cplusplus.com/reference/string/string/erase.html

ie. you find the character you want to remove, and then erase it.


Can you give an example of a line from the file, and explain which tokens you want to get out of it ? So that I have an idea of what you want to do exactly ?
0
 

Author Comment

by:gem56
ID: 20115444
yeah my mistake with the quotes.

ok i havnt checked out the links you posted yet, however i've created something using those functions anyway (originally started with a find/replace loop)

Heres what i have:

// Start

      while(getline(fInputVec, stFileLine))
      {

//------------------------------------------------------------

            // Remove any '<' and '>' chars from string
            while (stFileLine.length() > stFileLine.find("<"))
            {
                  int iStart = stFileLine.find("<");
                  stFileLine.erase(iStart, 1);
            }

            while (stFileLine.length() > stFileLine.find(">"))
            {
                  int iStart = stFileLine.find(">");
                  stFileLine.erase(iStart, 1);
            }

//------------------------------------------------------------

            string stFileWord;
            stringstream ssFileLine(stFileLine);

                // While theres text still left in the stream....
            while (ssFileLine.good())                                       // Is there something better then '.good()' ?
            {
                  ssFileLine >> stFileWord;

                  // If string is a blank space, skip to next >>
                  if (stFileWord.length() == 0)
                        continue;
            
                  cout << stFileWord << endl;
            }

      }

// End


ok typically the file im trying to open looks like this:

//-----------------------------------------
image   BMP     300   300

colour < 255 50 0  >
draw Point <50 145   >
draw Point < 50 155>

colour <255 52 0>
draw Point <52 145>
draw Point <52 155>
//-----------------------------------------

.....without the //----- lines. so far the code i've written works fine, except for the first few lines where i have spaces between the last digit and the '>' char. in these cases the output seems to repeat the last digit as well. im not sure as to why though.

the output im getting is something like:
image
BMP
300
300
colour
255
50
0
0                 // <----- Repeated output shouldnt be there
draw
Point
50
145
145             // <----- Repeated output shouldnt be there
...etc

appreciate all your help :)
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 2000 total points
ID: 20115492
Instead of your loop :

            while (stFileLine.length() > stFileLine.find("<"))
            {
                  int iStart = stFileLine.find("<");
                  stFileLine.erase(iStart, 1);
            }

you can use :

            size_t iStart = 0;
            while ((iStart = stFileLine.find("<", iStart)) != string::npos) {
                  stFileLine.erase(iStart, 1);
            }


>>             while (ssFileLine.good())                                       // Is there something better then '.good()' ?

You can use :

        while (ssFileLine >> stFileWord)


>> in these cases the output seems to repeat the last digit as well. im not sure as to why though.

Because your code thinks there is still a word at the end of the line (because of the use of good()).

Btw, to skip whitespace, you can use :

        ssFileLine >> ws;

You can of course also combine that with actually readin the value :

        ssFileLine >> ws >> stFileWord;
0
 

Author Comment

by:gem56
ID: 20116852
excellent, made the changes and everything works perfect now.

thanks heaps
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

862 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