| | |
char variables
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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,
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)
#include<iostream> #include<fstream> using namespace std; int main() { ifstream iDic("text"); ofstream oDic("rawDic"); char ch; while (iDic.get(ch)) { ch= tolower(ch); if (ch< 'a' || ch> 'z') iDic.putback('\n'); oDic << ch; } iDic.close(); oDic.close(); return 0; }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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)
#include<iostream> #include<fstream> using namespace std; int main() { ifstream iDic("text"); ofstream oDic("rawDic"); char ch; while (iDic.get(ch)) { ch= tolower(ch); if (ch< 'a' || ch> 'z') iDic.putback('\n'); oDic << ch; } iDic.close(); oDic.close(); return 0; }
C++ Syntax (Toggle Plain Text)
string word; while (iDic >> word) { // code here? oDic << word << endl; // code here? }
Make sure to put this at the top:
C++ Syntax (Toggle Plain Text)
#include <string>
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)
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;
}
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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)
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)
while (iDic.get (ch)) { if (isalpha (ch)) ch= tolower(ch); else ch = '\n'; oDictionary<< ch; }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
Who and what are you referring to?
![]() |
Similar Threads
- Help referencing variables (C++)
- redundant variables (C++)
- adding data into an char array (C++)
- C++ help (C++)
- TO CHAINSAW - or anybody else who knows C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Saving memory by using constants
- Next Thread: Class declaration syntax error
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






