Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

counting syllables

This topic is 2102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to count the number of syllables in the words in a text file. In my code I am getting the count of words contained in the text file but am getting one more than present.

#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main()
{
double index=0;
int syllabus,words=0,sentences;
string filename,word;
cout << "Enter the data file name: ";
cin >> filename;
ifstream infile;
infile.open(filename.c_str());
if(infile.fail())
{
cout << "Error opening " << filename << endl;
system("pause");
return 1;
}
while(!infile.eof())
{
cout << word << endl;
infile >> word;
words++;
}
cout << words << endl;
infile.close();
system("pause");
return 0;
}

I think my problem is at the !infile.eof line of code.

Share this post


Link to post
Share on other sites
Advertisement

How do you define a syllable via programming?  How does a program know what a syllable is?  It's virtually impossible.  For example, the word "deliberate" has 2 pronunciations with different syllables and 2 meanings:

DE-lib-ERIT (3 syllables)

DE-lib-ER-ate - (4 syllables)

 

You should abandon this path of finding syllables, it's not really possible for you and has no real benefit.  Stick to words (defined as separated by spaces). 

Share this post


Link to post
Share on other sites

while(!infile.eof())
{
cout << word << endl;
infile >> word;
words++;
}

 

Why do you output the previous word instead of outputting the word that came from the file?

 

I am not very familiar with the C++ stream classes, but it looks like, when you are at the end of the file, it needs to try to read a word before finally setting the "eof" flag to true. So, you have to account for that one failure at the end. You could try to make sure the contents of "word" look like a real word before increasing your count. The fanciness of your filter is up to you. I'll start with size.

 

while(!infile.eof())
{
    std::string word;
    infile >> word;
    cout << word << endl;
    if (word.size() > 0)
        words++;
}

Share this post


Link to post
Share on other sites

It's worth noticing browsers have given up this problem. They do only word-wrap by default, unless they find the soft-hypen (U+00AD, also "shy")  character and only when appropriate CSS property is set. I expect you to go uphill.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!