Hi every one I have a big problem, I need help with creating a program which reads every third word in a text file and excludes puncuation like -,?.!'" etc.. so far none of my work need code or suggestions just plain help(newb)

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
   ifstream input ( "text.txt" );

   string my_string;
   int counter = 0;
   while (!input.eof)
  {
      if (count ==2 )
      {
         cout<<"hey you hit the third line " << my_string;
      }
     count += 1;
 }
  read.close();
  cin.get();
}

of

#include <iostream>

#include <fstream>

#include <string>

using namespace std;



int main()

{   string filename

    ifstream input;

    input.open("sh_text.txt");

    string word;

    int count=0;

    if(input.fail())

    {

       cout << "error";

    }// end of if loop

    else

    {

        while(!input.eof())

        {

            input >> word;

            count++;

            if(count%3==0);

            {

                count=0;

                cout << "word" <<endl;

            }// end of if loop

        }// end of while loop

    }// end of else loop      

    return (0);  

}// end of program

:)

After you read a word, check each character and if you see a non-letter or number, it must be punctuation. Do what you need to do with it.

try to search string functions most especially strtok

ok thank you for the strtok i see it removes punctuation, but the problem is i have the input file txt stored in a string variable. how do i make strtok read the string and remove punctuation. and in the case that it does encounter punctuation advance to the next word
ex
foot-ball is? a fun' sport.

---would display

foot is a fun sport

ultimataly i want the function to be able to skip the words two times when spaces, punctuation occurs

using the strcode i got

char str[] ="- This, a sam-ple string.";
      char * pch;
      printf ("Splitting string \"%s\" into tokens:\n",str);
      pch = strtok (str, " ,.;:?!-");
      {
            while (pch != NULL)
            {
               printf ("%s\n",pch);
               pch = strtok (NULL, " ,.;:?!-");
            }
      }

the easiest way that i know is to use the boost tokenizer library.

#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
#include <fstream>

int main()
{
   using namespace std;
   using namespace boost;
   ifstream file( __FILE__ ) ;
   string str ;
   while( getline( file, str ) )
   {
        tokenizer<> toker( str ) ;
        for( tokenizer<>::iterator beg = toker.begin(); 
              beg != toker.end() ; ++beg ) cout << *beg << '\n' ;
   }
}

the easiest way that i know is to use the boost tokenizer library.

#include<iostream>
#include<boost/tokenizer.hpp>
#include<string>
#include <fstream>

int main()
{
   using namespace std;
   using namespace boost;
   ifstream file( __FILE__ ) ;
   string str ;
   while( getline( file, str ) )
   {
        tokenizer<> toker( str ) ;
        for( tokenizer<>::iterator beg = toker.begin(); 
              beg != toker.end() ; ++beg ) cout << *beg << '\n' ;
   }
}

I dont think this works please be more specific

I dont think this works please be more specific

here is the output.
g++ -std=c++98 -Wall -I/usr/include/boost-1_33_1 tokenize.cpp; ./a.exe

include
iostream
include
boost
tokenizer
hpp
include
string
include
fstream
int
main
using
namespace
std
using
namespace
boost
ifstream
file
FILE
string
str
while
getline
file
str
tokenizer
toker
str
for
tokenizer
iterator
beg
toker
begin
beg
toker
end
beg
cout
beg
n

I dont think this works please be more specific

You need to have the Boost API installed on your system -- only then you would be able to sun the mentioned example.

I can't use that method, my teacher might not have the boost api and i'll get an F

You should avoid using !eof it has problems. How about the following?

#include <iostream>
#include <fstream>

using namespace std;

class Thwee
{
//define public member functions
public:
   bool isPunctuation ( char c )
   {
      if ( c == '.' )
         return true;
      else if ( c == ',' )
         return true;
      else if ( c == '?' )
         return true;
      else if ( c == '\'' ) //escape character needed
         return true;
      else if ( c == '!' )
         return true;
      else if ( c == '\"' ) //escape character needed
         return true;
      else if ( c == '-' )
         return true;
      else if ( c == ';' )
         return true;
      else
         return false;
   }
};


int main()
{
   ifstream read ( "test.txt" );

   string word;
   int counter = 1; //initialise counter
   while ( read >> word ) //read in a word till end of file
   {
      Thwee test; //create a test object
      for ( int i = 0; i < word.length(); i++ )
      {
         if ( counter % 3 == 0 )
         {
            if ( test.isPunctuation ( word[i] ) == false )
            {
               //if the character isn't a punctuation character
               //print it
               cout << word[i];
            }
         }

      }
      counter = counter + 1; //increment counter
      if ( counter % 3 == 0 )
         cout << "\n";
   }

   read.close(); //close file
   return 0;
}


test.txt

Two households, both alike in dignity,
In fair Verona, where we lay our scene,
From ancient grudge break to new mutiny,
Where civil blood makes civil hands unclean.
From forth the fatal loins of these two foes
A pair of star-cross'd lovers take their life;
Whole misadventured piteous overthrows
Do with their death bury their parents' strife.
The fearful passage of their death-mark'd love,
And the continuance of their parents' rage,
Which, but their children's end, nought could remove,
Is now the two hours' traffic of our stage;
The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend.

My output

thwee@thwee-desktop:~$ g++ -Wall pedantic.cc
pedantic.cc: In function ‘int main()’:
pedantic.cc:42: warning: comparison between signed and unsigned integer expressions
thwee@thwee-desktop:~$ ./a.out

both
dignity
Verona
lay
From
break
mutiny
blood
hands
forth
loins
two
pair
lovers
life
piteous
with
bury
strife
passage
deathmarkd
the
their
Which
childrens
could
now
hours
our
which
with
attend
shall
toil

You might need to put the counter in the while loop actually to read the last word...

int counter = 0; //initialise counter
while ( read >> word ) //read in a word till end of file
   {
      counter = counter + 1; //increment counter
      Thwee test; //create a test object
      for ( int i = 0; i < word.length(); i++ )
      {
         if ( counter % 3 == 0 )
         {
            if ( test.isPunctuation ( word[i] ) == false )
            {
               //if the character isn't a punctuation character
               //print it
               cout << word[i];
            }
         }

      }   
      if ( counter % 3 == 0 )
         cout << "\n";
   }

You should avoid using !eof it has problems.

Why?!? You should explain what those problems are. See this (.eof() is the same as feof())

*Yawn* and another explanation...
http://www.daniweb.com/techtalkforums/thread38426.html

The point being...

Now, you can use eof() (or feof()) for a loop condition if you carefully add a kludge to the loop that protects you from the bug, but that's extra work for no real gain.

Plus I don't like explainin, i'll leave it to you...You seem to enjoy it, that's why you've written all those tutorials. WOW.

This question has already been answered. Start a new discussion instead.