Remove exclamation marks form a text file and add to vector

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Remove exclamation marks form a text file and add to vector

 
0
  #1
May 9th, 2009
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.

  1. char delim[100]=",.`~!@#$%^&*()_+=-{}][:';?><|";
  2. ifstream fileOne("file.txt");
  3. string fileWord;
  4.  
  5. while ( fileOne>> fileWord )
  6. {
  7. for(unsigned int a=0; fileWord.size ();a++)
  8. {
  9. for(unsigned int b=0;b<100;b++)
  10. {
  11. if (delim[b]=fileWord[b])
  12. fileWord.erase)(i);
  13. }
  14. }
  15. fileOneVector.push_back( fileWord);
  16.  
  17. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Remove exclamation marks form a text file and add to vector

 
0
  #2
May 9th, 2009
  1. if (delim[b]=fileWord[b])

Firstly Assignment "=" is not equal to Equal to "==".
  1. fileWord.erase)(i);
This would report a syntax error.

  1. for(unsigned int a=0; fileWord.size ();a++)
Where is the condition in there?
  1. for(unsigned int a=0;a< fileWord.size ();a++)
You should also try using iterators to go through arrays.

Another code improvment would be .
  1. char delim[]=",.`~!@#$%^&*()_+=-{}][:';?><|";
And in the second for loop
[/code]
  1. while(delim[b]!=0)
  2. //................
  3. b++
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Remove exclamation marks form a text file and add to vector

 
0
  #3
May 9th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Remove exclamation marks form a text file and add to vector

 
1
  #4
May 9th, 2009
Originally Posted by Sky Diploma View Post
  1. 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 ) the if statement will treat the expression as false , in any other case the expression will be evaluated as true
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Remove exclamation marks form a text file and add to vector

 
0
  #5
May 9th, 2009
point taken sir
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Remove exclamation marks form a text file and add to vector

 
0
  #6
May 9th, 2009
is it possible to remove the punction marks with "ispunct"
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Remove exclamation marks form a text file and add to vector

 
0
  #7
May 9th, 2009
Originally Posted by newcook88 View Post
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?
Last edited by tux4life; May 9th, 2009 at 10:21 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Remove exclamation marks form a text file and add to vector

 
0
  #8
May 9th, 2009
Originally Posted by tux4life View Post
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Remove exclamation marks form a text file and add to vector

 
0
  #9
May 9th, 2009
Originally Posted by newcook88 View Post
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
Last edited by tux4life; May 9th, 2009 at 11:20 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Remove exclamation marks form a text file and add to vector

 
0
  #10
May 9th, 2009
Originally Posted by tux4life View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC