943,807 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 694
  • C++ RSS
Nov 24th, 2008
0

char variables

Expand 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,

cpp Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Nov 24th, 2008
0

Re: char variables

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,

cpp Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. #include <string>
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 24th, 2008
0

Re: char variables

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:
Quote 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)
Reputation Points: 34
Solved Threads: 2
Junior Poster in Training
freelancelote is offline Offline
88 posts
since Aug 2008
Nov 25th, 2008
1

Re: char variables

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

C++ Syntax (Toggle Plain Text)
  1. while (iDic.get (ch))
  2. {
  3. if (isalpha (ch))
  4. ch= tolower(ch);
  5. else
  6. ch = '\n';
  7.  
  8. oDictionary<< ch;
  9. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 25th, 2008
1

Re: char variables

c++ Syntax (Toggle Plain Text)
  1. //...
  2. if (ch < 'a' && ch > 'z')
  3. //...Do you understand this?
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 25th, 2008
0

Re: char variables

Click to Expand / Collapse  Quote originally posted by cikara21 ...
c++ Syntax (Toggle Plain Text)
  1. //...
  2. if (ch < 'a' && ch > 'z')
  3. //...Do you understand this?
Who and what are you referring to?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Nov 25th, 2008
0

Re: char variables

The owner of the problem..Not u..Sorry..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Saving memory by using constants
Next Thread in C++ Forum Timeline: Class declaration syntax error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC