0

I'm having trouble implementing a feature that counts and displays the number of vowels from a file.

Here is the code I have so far.

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>

using namespace std;

int main(void)
{int i;
 string inputFileName;
 string s;
 ifstream fileIn;
 char ch;
 cout<<"Enter name of file of characters :";
 cin>>inputFileName;
 fileIn.open(inputFileName.data());
 assert(fileIn.is_open() );
 i=0;
 while (!(fileIn.eof()))
  {
  ????????????
  }
 cout<<s;
 cout<<"The number of vowels in the string is "<<s.?()<<endl;
 return 0;
}

Note the question marks in the code. Questions: How should I go about counting the vowels? Do I have to convert the text to lowercase and invoke system controls (if possible)? Also, as for printing the number of vowels in the end, which string variable should I use, (see s.?)?

Thanks

csheroe
  • 77
  • 1
  • 10
  • You should start with the line above: `while (!(fileIn.eof()))`. If you're planning to read the file and just assume the reading succeeds and that the contents are what you expect, you should rethink that decision. If you aren't planning to do that, then `eof` is the wrong condition unless you're going to ignore every single invalid input. – chris Mar 13 '15 at 19:41
  • You're going to need to learn how to think about solving problems with a computer. So, how would you count the number of vowels? You need to know what a vowel is. Is that a library function? Read the list of library functions. There isn't a library function? Make yourself a list of vowels and put it in your program. Now you need to go to each character in your string and test if it is a vowel by looking for it in your list of vowels. If it is in the list then add 1 to your vowel count. See how thinking it through can work? – Zan Lynx Mar 13 '15 at 20:00

3 Answers3

4
auto isvowel = [](char c){ return c == 'A' || c == 'a' ||
                                  c == 'E' || c == 'e' ||
                                  c == 'I' || c == 'i' ||
                                  c == 'O' || c == 'o' ||
                                  c == 'U' || c == 'u'; };

std::ifstream f("file.txt");

auto numVowels = std::count_if(std::istreambuf_iterator<char>(f),
                               std::istreambuf_iterator<char>(),
                               isvowel);
David
  • 25,830
  • 16
  • 80
  • 130
  • I'd personally go with a lookup array or string for the vowels, it is more flexible, and it is not like there is 1 language in the world... – dtech Mar 13 '15 at 19:56
  • @ddriver `isvowel` can easily be changed to use a lookup, or to support different languages, etc, without effecting the rest of the code – David Mar 13 '15 at 20:23
  • Thanks, but as I'm a beginner and a little slow to grasp this, where should I insert this so it syncs up with my code? And I thought C++ didn't have an isvowel function? – csheroe Mar 13 '15 at 21:10
  • @csheroe to understand `isvowel` in the code above look up what lambdas are and how they work. Short story is that in the code above it's a shorthand way to write a function. The rest is easily searchable just by the names "istreambuf_iterator", "count_if", "ifstream", etc – David Mar 13 '15 at 21:15
2

You can using <algorithm>'s std::count_if to achieve this :

std::string vowels = "AEIOUaeiou";

size_t count = std::count_if
       (
            std::istreambuf_iterator<char>(in),
            std::istreambuf_iterator<char>(),
            [=]( char x) 
            {
                return   vowels.find(x) != std::string::npos  ;
            }
        );

Or

size_t count = 0;
std::string vowels = "AEIOUaeiou";
char x ;
while ( in >> x )
{
  count += vowels.find(x) != std::string::npos ;
}

Also read Why is iostream::eof inside a loop condition considered wrong?

Community
  • 1
  • 1
P0W
  • 42,046
  • 8
  • 62
  • 107
0

Could this help?

char c;
int count = 0;
while(fileIn.get(c))
{
    if ((c == 'a') || (c=='e') || .......)
    {
        count++;
    }
}
4386427
  • 33,845
  • 4
  • 32
  • 53