954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remove exclamation marks form a text file and add to vector

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);
	
	}
newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
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[/code]

while(delim[b]!=0)
//................
b++
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

while ( fileOne>> fileWord )
{
for(unsigned int a=0; a

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
if (delim[b]=fileWord[b])

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


A little clarification: when you write if (delim[b]=fileWord[b]) thenyou'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 :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

point taken sir :)

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
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?

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
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 :)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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
newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

for(unsigned int i=1;i
newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

char str[]=""; char *punct; string result; for(size_t n = 1; n

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

str=fileOneVector[n];
cannot convert to char[]
newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
Try: str=fileOneVector[n].c_str();


i still get that error..

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You