[C++] Need help with a function that converts words in an array

Discussion in 'OT Technology' started by Rick, Nov 26, 2003.

  1. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    Basically I have an array or words that I created from a file. I need to read through them and format them if they need to be formatted. If the first or last position of a word is one of the following characters: ( ) " ' , . ? ! I need to remove that character and store the new word back to the array.

    Here is the code I have for this function:
    Code:
    void convertarray(string words[], int count)
    {
      int lastpos;
      string tempword;
      int length;
      string newword;
      
      for(int i = 0; i < count; i++)
        {
          newword = "";
          tempword = words[i];
          length = tempword.length();      
          lastpos = length - 1;
          cout << "Length of " << tempword << " is " << length << endl;
          cout << "The first position of " << tempword << " is " << tempword[0] << endl;
          cout << "The last position of " << tempword << " is " << tempword[lastpos] << endl;
          if(tempword[0] == '!' || tempword[0] == '(' || ')' || tempword[0] == '"' || tempword[0] == '?' ||
    	 tempword[0] == '.' || tempword[0] == ':' || tempword[0] == ';' || tempword[0] == ',' ||
    	 tempword[0] == '\'')
    	{
    	  cout << "first position matches" << endl;
    	  for(int j = 0; j < length; j++)
    	    newword = newword + tempword[j + 1];
    	  words[i] = newword;
    	}
          if(tempword[lastpos] == '!' || tempword[lastpos] == '(' || tempword[lastpos] == '"' ||
    	 tempword[lastpos] == '?' || tempword[lastpos] == '.' || tempword[lastpos] == ';' ||
    	 tempword[lastpos] == ',' || tempword[lastpos] == '\'')
    	{   
    	  cout << "last position matches" << endl;
    	  for(int k = 0; k < length - 1; k++)
    	    newword = newword + tempword[k];
    	  words[i] = newword;
    	}
          else
    	{
    	  cout << "no position matches" << endl;
    	  words[i] = tempword;
    	}
        }
    } 
    
    Here is the output I get. None of the words should be true to either if statements, but they are for some reason. The first part is for debugging reasons. The file I am using contains this: this is for testing purposes

    --output--

    5 words appeared in the data file.
    Length of this is 4
    The first position of this is t
    The last position of this is s
    first position matches
    no position matches
    Length of is is 2
    The first position of is is i
    The last position of is is s
    first position matches
    no position matches
    Length of for is 3
    The first position of for is f
    The last position of for is r
    first position matches
    no position matches
    Length of testing is 7
    The first position of testing is t
    The last position of testing is g
    first position matches
    no position matches
    Length of purposes is 8
    The first position of purposes is p
    The last position of purposes is s
    first position matches
    no position matches
    4.8 is the average length of a word in the file.
    The largest word is 8 character(s) long and is purposes
    The smallest word is 2 character(s) long and is is

    this
    is
    for
    testing
    purposes
     
  2. Kent

    Kent Guest

    The red section is fucking you. In C, anything non-zero is evaluated as true, so it's going to take the ')' as true... since it's OR's, that is going to pass the condition regardless of the string because the ')' always evaluates as true. You need to make it || tempword[0] == ')' like the rest.

    (Multiple edits since I kept cutting out the end of the message tags)
     
  3. Kent

    Kent Guest

    And you're always hitting the "no position" code because it goes in that else if the last position fails regardless of whether the first position failed.

    it should be
    if (FirstPositionPassed)
    {
    cout << "first passed"
    }
    else if (Last PositionPassed)
    {
    cout << "last passed"
    }
    else
    {
    cout << "nothing passed."
    }


    You're missing the bold else.
     
  4. Rick

    Rick New Member

    Joined:
    Feb 20, 2002
    Messages:
    1,735
    Likes Received:
    0
    Location:
    Henderson, NV
    Thanks. I was just about to post that I was always hitting that last else, but I needed to make the second one else if.

    It's working fine now.
     
  5. Kent

    Kent Guest

    Actually, I wasn't thinking quite right when I said that. If you make it else if, it's only going to check the last character if the first character wasn't a punctuation, so you probably don't want to do that since you always want to check the first and last character.

    You may just want to set some boolean variable inside the other conditionals and just check it for that one.

    e.g.
    Code:
    NeedsFormatting = False
    If (FirstCharCheck)
    {
       NeedsFormatting = True
       blah blah
    }
    If (LastCharCheck)
    {
       NeedsFormatting = True 
       blah blah
    }
    if (!NeedsFormatting)
    {
        cout << "no chars matched" 
    }
    
    
     
  6. frinky23

    frinky23 Bangle Sackrider

    Joined:
    Sep 10, 2002
    Messages:
    11,246
    Likes Received:
    0
    Location:
    Prestigious Cook County
    Holy crap, talk about using the wrong tool for the job!

    the following line does the exact same thing in Perl:
    Code:
    foreach (@words){tr/\b[()"',.?!]//m;}
    Or something along those lines (my Perl is rusty).

    If you absolutely must use C++, consider embedding Perl commands into your program and use the right tools for the job.
     

Share This Page