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);
	
	}

Recommended Answers

All 23 Replies

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++
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

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 :)

commented: Thanks for explaining it out +3

point taken sir :)

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

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, " ,.-");
    }
}

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--;
}

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[]

Post edited:: Try: strcpy(str, fileOneVector[n].c_str());

Try: str=fileOneVector[n].c_str();

i still get that error..

Sorry that was my mistake, try the following: strcpy(str, fileOneVector[n].c_str());

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) :( ...

it dint work same error

it dint work same error

You're doing something wrong, with me this compiled perfectly :)

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.