char variables

Thread Solved
Reply

Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training

char variables

 
0
  #1
Nov 24th, 2008
Hi,
here is my problem: I'm writing a small program that needs to read a text file and write the words of this text as a list.
I could only come up with the little piece of code I wrote below and it's not even working. I believe the problem is on line 13 but I'm not so sure.
I would appreciate if you could tell me where the problem is and point me to the right direction.
Also, if you know a more cunning way to do what I intend please do tell me.

Thanks,

  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ifstream iDic("text");
  8. ofstream oDic("rawDic");
  9. char ch;
  10. while (iDic.get(ch))
  11. {
  12. ch= tolower(ch);
  13. if (ch< 'a' || ch> 'z')
  14. iDic.putback('\n');
  15. oDic << ch;
  16. }
  17. iDic.close();
  18. oDic.close();
  19. return 0;
  20. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: char variables

 
0
  #2
Nov 24th, 2008
Originally Posted by freelancelote View Post
Hi,
here is my problem: I'm writing a small program that needs to read a text file and write the words of this text as a list.
I could only come up with the little piece of code I wrote below and it's not even working. I believe the problem is on line 13 but I'm not so sure.
I would appreciate if you could tell me where the problem is and point me to the right direction.
Also, if you know a more cunning way to do what I intend please do tell me.

Thanks,

  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. ifstream iDic("text");
  8. ofstream oDic("rawDic");
  9. char ch;
  10. while (iDic.get(ch))
  11. {
  12. ch= tolower(ch);
  13. if (ch< 'a' || ch> 'z')
  14. iDic.putback('\n');
  15. oDic << ch;
  16. }
  17. iDic.close();
  18. oDic.close();
  19. return 0;
  20. }
Line 14 - don't try to put back '\n'. What assumptions can you make about your input file? What is a "word"? Something with only letters? What do you do with non-"words"? You need to decide this and you need to decide what your input file looks like and what assumptions you can make about "good " data and what to do with "bad" data. My guess is you want to to do something along these lines, but I don't know till you answer the questions about the data.

  1. string word;
  2. while (iDic >> word)
  3. {
  4. // code here?
  5. oDic << word << endl;
  6. // code here?
  7. }

Make sure to put this at the top:
  1. #include <string>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 87
Reputation: freelancelote is an unknown quantity at this point 
Solved Threads: 2
freelancelote's Avatar
freelancelote freelancelote is offline Offline
Junior Poster in Training

Re: char variables

 
0
  #3
Nov 24th, 2008
Thanks VernonDozier,
The idea was evaluate every character of a text file (say a text of a newspaper for example) and separate words. Valid words would be strings of char from 'a' to 'z' only.
At the time a space is encountered I'd make a new line. I fact if I modify the while loop and use the following code instead:
Originally Posted by c++
while (iDic.get (ch))
{
ch= tolower(ch);

if (ch == ' ')
iDic.putback('\n');
oDictionary<< ch;
}
the programme does pretty much what I want but I still don't know what to do the commas, etc (that is every thing having an ascii code outside the a-z range)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: char variables

 
1
  #4
Nov 25th, 2008
Originally Posted by freelancelote View Post
Thanks VernonDozier,
The idea was evaluate every character of a text file (say a text of a newspaper for example) and separate words. Valid words would be strings of char from 'a' to 'z' only.
At the time a space is encountered I'd make a new line. I fact if I modify the while loop and use the following code instead:


the programme does pretty much what I want but I still don't know what to do the commas, etc (that is every thing having an ascii code outside the a-z range)
Well you got rid of your 'a' through 'z' comparison. Again, I wouldn't use putback. Instead of putting something back, reading it in again, then outputting it, just go straight to outputting it. You can also use isalpha for checking whether it is a letter.

http://www.cplusplus.com/reference/c...e/isalpha.html

isspace and ispunct may be helpful too in separating words:

http://www.cplusplus.com/reference/c...e/isspace.html
http://www.cplusplus.com/reference/c...e/ispunct.html

  1. while (iDic.get (ch))
  2. {
  3. if (isalpha (ch))
  4. ch= tolower(ch);
  5. else
  6. ch = '\n';
  7.  
  8. oDictionary<< ch;
  9. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: char variables

 
1
  #5
Nov 25th, 2008
  1. //...
  2. if (ch < 'a' && ch > 'z')
  3. //...Do you understand this?
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: char variables

 
0
  #6
Nov 25th, 2008
Originally Posted by cikara21 View Post
  1. //...
  2. if (ch < 'a' && ch > 'z')
  3. //...Do you understand this?
Who and what are you referring to?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: char variables

 
0
  #7
Nov 25th, 2008
The owner of the problem..Not u..Sorry..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC