i want to read words form a text file and remove the comars and fullstops and then put the single word to a vector. but when i try the method below i get an error can some one please help me.

char delim[100]=",.`~!@#$%^&*()_+=-{}][:';?><|";
ifstream fileOne("file.txt");
string fileWord;
	
	while ( fileOne>> fileWord )
	{
		for(unsigned int a=0; fileWord.size ();a++)
		{
			for(unsigned int b=0;b<100;b++)
			{
				if (delim[b]=fileWord[b])
					fileWord.erase)(i);
			}
		}
		fileOneVector.push_back( fileWord);
	
	}
if (delim[b]=fileWord[b])

Firstly Assignment "=" is not equal to Equal to "==".

fileWord.erase)(i);

This would report a syntax error.

for(unsigned int a=0; fileWord.size ();a++)

Where is the condition in there?

for(unsigned int a=0;a< fileWord.size ();a++)

You should also try using iterators to go through arrays.

Another code improvment would be .

char delim[]=",.`~!@#$%^&*()_+=-{}][:';?><|";

And in the second for loop

while(delim[b]!=0)
//................
b++

Edited 3 Years Ago by mike_2000_17: Fixed formatting

while ( fileOne>> fileWord )
    {
        for(unsigned int a=0; a<fileWord.size ();a++)
        {
            for(unsigned int b=0;b<100;b++)
            {
                if (delim[b]==fileWord[b])
                    fileWord.erase(b);
            }
        }
        fileOneVector.push_back( fileWord);

    }

i corrected it. but when i compile and run i get an debug assertion edrror

Edited 3 Years Ago by Reverend Jim: Fixed formatting

if (delim[b]=fileWord[b])

Firstly Assignment "=" is not equal to Equal to "==".

A little clarification: when you write if (delim[b]=fileWord[b]) then you're assigning the value at fileWord[b] to delim[b] , as a result the assignment operator always returns the assigned value what means that if the assigned value isn't NULL or 0 (it's the same :P) the if statement will treat the expression as false , in any other case the expression will be evaluated as true :)

Comments
Thanks for explaining it out

is it possible to remove the punction marks with "ispunct"

No, the ispunct function is just checking whether a character is a punctuation mark or not :)
(you can find more information on 'ispunct' here)

By the way: Why don't you just use the strtok function?

No, the ispunct function is just checking whether a character is a punctuation mark or not :)
(you can find more information on 'ispunct' here)

By the way: Why don't you just use the strtok function?

yes so i can say that if the char is equal to ispuntc then remove the character

yes so i can say that if the char is equal to ispuntc then remove the character

Yes you can, but there's an easier way:

  • Declare a string (called 'result' for example) to store the 'stripped' string in.
  • Just loop through the string (containing the punctuation marks) and every time you evaluate whether the character is a punctuation mark or not, if the character is not a punctuation mark, you add it to your 'result' string, after the loop has finished you've a string which contains an 'exact' copy of the other one, but without the punctuation marks :)

To repeat what I've already said: the strtok function might be a lot easier for you, but it's your choice :)

Yes you can, but there's an easier way:

  • Declare a string (called 'result' for example) to store the 'stripped' string in.
  • Just loop through the string (containing the punctuation marks) and every time you evaluate whether the character is a punctuation mark or not, if the character is not a punctuation mark, you add it to your 'result' string, after the loop has finished you've a string which contains an 'exact' copy of the other one, but without the punctuation marks :)

To repeat what I've already said: the strtok function might be a lot easier for you, but it's your choice :)

could be be kindful to show me the sample code using strtok

could be be kindful to show me the sample code using strtok

There's an example provided here, just scroll the page a bit down and you'll see it :P

i got an error when i excute this code. because i have to get the string form da vector and then set it.

char str[]=" ";
char *punct;
string result;
for(size_t n = 1; n <fileOneVector.size(); n++)
{
    str=fileOneVector[n];
    punct=strotok(str," .,:'!@#");
    while(punct !=NULL)
    {
        result=("%s\n",punct);
        punct = strtok (NULL, " ,.-");
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Could you please post using code tags? punct=str[B]o[/B]tok(str," .,:'!@#"); , it isn't 'strotok', but strtok :P

how ever when i use the below code it works but not all comars get eliminated

for(unsigned int i=1;i<fileOneVector.size ();i++)
{
    string strOne=fileOneVector[i];
    for(string::size_type ix = 0 ;ix < strOne.size();++ix)   
{   
if(ispunct(strOne[ix])) 
{           
    strOne.erase(ix,1); 
    ix--;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I'm not repeating it anymore :angry:: Post using code tags !!

>how ever when i use the below code it works but not all comars get eliminated
That's just logical because you only check whether the character is a punctuation mark, and only in that case you remove the character from the string :)

char str[]="";
char *punct;
string result;
for(size_t n = 1; n <fileOneVector.size(); n++)
{
str=fileOneVector[n];
punct=strtok(str," .,:'!@#");
while(punct !=NULL)
{
result=("%s\n",punct);
punct = strtok (NULL, " ,.-");
}
}

sorry about the quotations
i changed the code.. i get an error
1)

str=fileOneVector[n];

cannot convert to char[]

i get the same error.. isnt there any other way that i can use to like read the text file remove the exclamation marks and save the text file and then i can add it to da vector seperateley

No wonder you get an error: char str[]=""; , you actually have to create an array with more than 1 element, you can't store much else in it than just the null-terminator: \0 :P

Try char str[80]; instead of char str[]=""; Some little clarification: when you write char str[]=""; this actually does the same as char str[1]; , but only one element is not enough space to hold a whole string, it's just enough space for holding the null-terminator (which is obligatory for each c-string: if a c-string doesn't contain a null-terminator and you're performing operations on it your program will crash in the best case, or your whole operating system in the worst case, but today's operating systems normally offer memory protection so it's only your application which will crash) :( ...

Sorry, I've found a typo:

A little clarification: when you write if (delim[b]=fileWord[b]) then you're assigning the value at fileWord[b] to delim[b] , as a result the assignment operator always returns the assigned value what means that if the assigned value is NULL or 0 (it's the same :P) the if statement will treat the expression as false , in any other case the expression will be evaluated as true :)

This article has been dead for over six months. Start a new discussion instead.