DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   char variables (http://www.daniweb.com/forums/thread159311.html)

freelancelote Nov 24th, 2008 7:05 pm
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,

#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;
}

VernonDozier Nov 24th, 2008 7:41 pm
Re: char variables
 
Quote:

Originally Posted by freelancelote (Post 743859)
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,

#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;
}

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.

   string word;
  while (iDic >> word)
  {
        // code here?
        oDic << word << endl;
        // code here?
  }

Make sure to put this at the top:
#include <string>

freelancelote Nov 24th, 2008 8:37 pm
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)

VernonDozier Nov 25th, 2008 12:27 pm
Re: char variables
 
Quote:

Originally Posted by freelancelote (Post 743910)
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

while (iDic.get (ch))
{
    if (isalpha (ch))
        ch= tolower(ch);
    else
        ch = '\n';

    oDictionary<< ch;
}

cikara21 Nov 25th, 2008 1:02 pm
Re: char variables
 
//...
if (ch < 'a' && ch > 'z')
    //...Do you understand this?

VernonDozier Nov 25th, 2008 1:18 pm
Re: char variables
 
Quote:

Originally Posted by cikara21 (Post 744485)
//...
if (ch < 'a' && ch > 'z')
    //...Do you understand this?

Who and what are you referring to?

cikara21 Nov 25th, 2008 1:39 pm
Re: char variables
 
The owner of the problem..Not u..Sorry..


All times are GMT -4. The time now is 12:23 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC