Hey newer programmer here need a bit of help.

The direction are bascially to read in from a file some words in the form of a paragraph with no punctuations and no commas etc.

The file will look similar to

When promulgating your esoteric cogitations and articulating your
Let your conversational communications possess a clarified
a compact comprehensibleness and a concentrated cogency
Sedulously avoid all polysyllabic profundity pompous prolixity and
psittaceous vacuity Shun double entrendres prurient jocosity and profanity And above all do not use big words


The actual file is larger but this is a sample of it. But this so you can see how it looks.

I need to read in each line and seperate into individual words, then seperate each word into individual letters. After seperateing the letter I need to one by one check to see if they are vowels, or consenants, every time one occurs i need to add one to the counter for that particular type of letter.

As of right now I can read in the lines, and seperate the lines into words and then seperate into letters. the biggest trouble im having is to do this one line at a time. Any help on how to seperate and do these manipulations one line at a time would be a big help.

Also how do I declare vowels for the count.
i know i will compare each letter to vowel and add one to counter but if not add one to consanent.

Any help for now would be great.

Thanks so much.

Recommended Answers

All 15 Replies

I don't see any need to separate the lines into words, just look at the characters after you read the line and if you see a space just ignore it.

Easiest way I can think of is to set up a 256 integer array and for each character increment the location using the character as a subscript. Then after reading the file, look at the array locations for 'aeiouAEIOU' and add them up.

Alternatively, since the only non-alphabetic chars are SPACE and '\n', set up a switch statement that tests
1) SPACE and '\n', and ignore them
2) All vowels, and add to a vowel counter
3) default, add to a consonant counter.

You don't even need to read lines. Just read characters and test them because all you're doing is dropping letters into categories.

#include <cctype>
#include <fstream>
#include <iostream>
#include <map>
#include <string>

using namespace std;

enum {CONSONANT, VOWEL};

bool isvowel( char ch ) {
  static string vowels( "aeiou" );
  return vowels.find( tolower( ch ) ) != string::npos;
}

int main() {
  ifstream ifs( "myfile" );
  unsigned long categories[2] = {0};
  char ch;

  while ( ifs.get( ch ) ) {
    if ( isalpha( ch ) ) {
      ++categories[isvowel( ch )];
    }
  }

  cout<<"Consonants: "<< categories[CONSONANT] <<endl;
  cout<<"Vowels:     "<< categories[VOWEL] <<endl;

  return 0;
}

Thank you both for your input,

My teacher just changed the program a bit and said he also wants us to output like this;

total word count = #####
Vowel count = #####
Consonant count = ######
Longest word= word that is longest
Average word length = ##.##

He is requesting all of these outputs.

I know it must be done a bit differently now, but any help would be great.

Also I tried to use what you have have me on the second pust but i keep getting errors. any help would be great

errors like
F:\C++\parser2.cpp `ch' undeclared (first use this function)
F:\C++\parser2.cpp expected primary-expression before "int"


Thanks for the help, our teacher said best way to work problem is to read in all of line one then seperate it into first word, and then seperate that into letters then see which are vowels, and which are not, then when finished with word go to next word, and when done with line go to next line.

Thanks, for the help

our teacher said best way to work problem is to read in all of line one then seperate it into first word, and then seperate that into letters then see which are vowels, and which are not, then when finished with word go to next word, and when done with line go to next line.

That's too much work. You can use cin because it reads words. Then you can go over each word and get the statistics.

string word;

while ( cin >> word ) {
  if ( word.length() > longestWord ) {
    longestWord = word.length();
  }

  totalWordLength += word.length();
  ++wordCount;

  for ( int i = 0; i < word.length(); ++i ) {
    if ( isalpha( word[i] ) ) {
      ++categories[isvowel( word[i] )];
    }
  }
}

cout<<"total word count = "<< wordCount <<endl;
cout<<"vowel count = "<< categories[VOWEL] <<endl;
cout<<"consonant count =  "<< categories[CONSONANT] <<endl;
cout<<"longest word = "<< longestWord <<endl;
cout<<"average word length = "<< totalWordLength / wordCount <<endl;

Hamrick,

Thanks for the quick reply, tried that help in my program but keep getting several error. Not sure what is wrong, you are using C++ right, hmm.. not sure whats wrong, but i will continue to work on it, let me know if you have any other suggestions.

Thanks

Post your code and the errors, and I'll try to help.

Well this is what i had first, but all this does is break up the first word into letters which is perfect, but i need it to do this same thing but line by line.

 string line;
 int linecount = 0;
 int linenum = 0;

      while (! infile)
 {
        cout << "Error While Attempting to Open File  " << endl;
        exit(1);
  }

  while (getline (infile, line))
  {
        string space;
        string word;
        int len;
        if (linenum < linecount)
        { 
        linecount ++;       
        word = line.substr(0, line.find(' '));
        string letter1 = word.substr(0,1);
        string letter2 = word.substr(1,1);
        string letter3 = word.substr(2,1);
        string letter4 = word.substr(3,1);
        string letter5 = word.substr(4,1);
        string letter6 = word.substr(5,1);
        string letter7 = word.substr(6,1);
        string letter8 = word.substr(7,1);
        string letter9 = word.substr(8,1);
        string letter10 = word.substr(9,1);
        string letter11 = word.substr(10,1);
        string letter12 = word.substr(11,1);
        string letter13 = word.substr(12,1);

        cout << letter1 << endl;  // i just put this here to make sure  it was pulling the first letter only, and it was

        }
  }

I need it to do only one line at a time, i didnt compare to see if each letter was a vowel or not i just wanted it to do one line at a time.

I hope you can help.

After your assistance this is what i have now:

string word; 
string longestWord;
int totalWordLength = 0;
int wordCount = 0;
int categories = 0;
while ( cin >> word ) 
{  
      if ( word.length() > longestWord ) 
      {    
      longestWord = word.length();
      }   
      totalWordLength += word.length();
      ++wordCount; 

      for ( int i = 0; i < word.length(); ++i )
      {    if ( isalpha( word[i] ) ) {      
          ++categories[isvowel( word[i] )];
          } 
        }
      } 
   cout<<"total word count = "<< wordCount <<endl;
   cout<<"vowel count = "<< categories[VOWEL] <<endl;
   cout<<"consonant count =  "<< categories[CONSONANT] <<endl;
   cout<<"longest word = "<< longestWord <<endl;
   cout<<"average word length = "<< totalWordLength / wordCount <<endl;

I am very new to programming so I hope you can help.
Thanks,

commented: And still you fail to grasp the concept of [ code ][ /code ] tags -2
commented: equaliser +12

By longestWord I thought you wanted the length of the longest word, not the word itself. That changes the code a little. And categories is an array of 2. This works.

string word;
  string longestWord;
  int totalWordLength = 0;
  int wordCount = 0;
  int categories[2] = {0};
  while ( cin >> word )
  {
    if ( word.length() > longestWord.length() )
    {
      longestWord = word;
    }
    totalWordLength += word.length();
    ++wordCount;

    for ( int i = 0; i < word.length(); ++i )
    { if ( isalpha( word[i] ) ) {
      ++categories[isvowel( word[i] )];
    }
    }
  }
  cout<<"total word count = "<< wordCount <<endl;
  cout<<"vowel count = "<< categories[VOWEL] <<endl;
  cout<<"consonant count = "<< categories[CONSONANT] <<endl;
  cout<<"longest word = "<< longestWord <<endl;
  cout<<"average word length = "<< totalWordLength / wordCount <<endl;

I just put that in my compiler just how you had it and it is giving me the following errors;

62 F:\C++\parser2.cpp `isvowel' undeclared (first use this function)
67 F:\C++\parser2.cpp `VOWEL' undeclared (first use this function)
68 F:\C++\parser2.cpp `CONSONANT' undeclared (first use this function)

You didn't add the other parts from my other post. Here's the whole thing.

#include <cctype>
#include <fstream>
#include <iostream>
#include <map>
#include <string>

using namespace std;

enum {CONSONANT, VOWEL};

bool isvowel( char ch ) {
  static string vowels( "aeiou" );
  return vowels.find( tolower( ch ) ) != string::npos;
}

int main() {
  string word;
  string longestWord;
  int totalWordLength = 0;
  int wordCount = 0;
  int categories[2] = {0};
  while ( cin >> word )
  {
    if ( word.length() > longestWord.length() )
    {
      longestWord = word;
    }
    totalWordLength += word.length();
    ++wordCount;

    for ( int i = 0; i < word.length(); ++i )
    { if ( isalpha( word[i] ) ) {
      ++categories[isvowel( word[i] )];
    }
    }
  }
  cout<<"total word count = "<< wordCount <<endl;
  cout<<"vowel count = "<< categories[VOWEL] <<endl;
  cout<<"consonant count = "<< categories[CONSONANT] <<endl;
  cout<<"longest word = "<< longestWord <<endl;
  cout<<"average word length = "<< totalWordLength / wordCount <<endl;

  return 0;
}

Hey Buddy Thanks for the help, its giving me no errors, just that way but isnt kicking out any responce.

Do you know what the prob, could be.

thanks

If it's just hanging there without printing the output, you're stuck in the loop. You have to signal EOF to get the loop to stop, and you can do that with ctrl+z on Windows or ctrl+d on Linux.

Hey Friend,

I changed a bit to,

 string word;
 string line1;  
 string longestWord;  
 int totalWordLength = 0;  
 int wordCount = 0;  
 int categories[2] = {0};  
 while ( getline (infile, line1) )  
 {    
      word = line1.substr(0, line1.find(' '));

      if ( word.length() > longestWord.length() )    
      {      
          longestWord = word;    
      }    
      totalWordLength += word.length();    
      ++wordCount;     
      for ( int i = 0; i < word.length(); ++i )    
      { 
          if ( isalpha( word[i] ) ) 
          {      
              ++categories[isvowel( word[i] )];    
          }    
          }  
          }  

 cout<<"total word count = "<< wordCount <<endl;  
 cout<<"vowel count = "<< categories[VOWEL] <<endl;  
 cout<<"consonant count = "<< categories[CONSONANT] <<endl;  
 cout<<"longest word = "<< longestWord <<endl;  
 cout<<"average word length = "<< totalWordLength / wordCount <<endl; 

I changed b/c it didnt seem to not kick out the vowels and was reading the whole line. I changed it but now its looking at only the first word of each line. I am not sure what the problem is. It just isnt oing the exact things it should be, any suggestions would be great.

Member Avatar for iamthwee

I have already shown you how to read lines before...

string line;
ifstream read ( "c:\\...path_to_file" );
while ( getline ( read, line, '\n' ) )
{
//do stuff with lines
}
read.close();

hi friends , i was encountering a similar kind of question but a little more twisting . i need a program to read a text from any file which includes atleast two paragraphs.. and i need to print out the number of words which do not contain vowels finally ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.