Determining the number of vowels in each word from a file I'm 5 weeks into my first C++ class. I'm curious about how this code could be improved, more streamlined or simply things that I could/should have done better. 

The project was:


  Read from a .txt file that has different words on separate lines. 
  Determine how many vowels each word has, and print each word AND vowel count to the console. You must write and use a function outside of main(). 


Here's the code I wrote: 

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
using namespace std;

//prototypes
void readFile();
void printWords(string);

int main()
{
    readFile();
    return 0;
}//end main

void readFile()
{
    ifstream inFile;
    string words;

    inFile.open("words.txt");

    while(!inFile.eof())
    {
        inFile&gt;&gt;words;
        printWords(words);
    }//end while loop

    inFile.close();
}//end readFile function


void printWords(string str)
{
    char a = 'a', e = 'e', z = 'i', o ='o', u ='u';
    int num_A = 0, num_E = 0, num_I = 0, num_O = 0, num_U = 0;

    cout&lt;&lt; str &lt;&lt;endl;

    for(int i = 0; i &lt; str.length(); ++i)
    {
        if(str.at(i) == a)
        {
            num_A++;
        }
        if(str.at(i) == e)
        {
            num_E++;
        }
        if(str.at(i) == z)
        {
            num_I++;
        }
        if(str.at(i) == o)
        {
            num_O++;
        }
        if(str.at(i) == u)
        {
            num_U++;
        }
    }//end for loop
    cout&lt;&lt; "a: " &lt;&lt; num_A &lt;&lt;endl;
    cout&lt;&lt; "e: " &lt;&lt; num_E &lt;&lt;endl;
    cout&lt;&lt; "i: " &lt;&lt; num_I &lt;&lt;endl;
    cout&lt;&lt; "o: " &lt;&lt; num_O &lt;&lt;endl;
    cout&lt;&lt; "u: " &lt;&lt; num_U &lt;&lt; "\n" &lt;&lt;endl;

}//end printWord function


I feel like there should be a smoother function to extract, count and return the vowels. Then a function to print. Also, I tried to use a switch statement because there were so many if statements, but I couldn't get it to work.
Don't do this:

using namespace std;


See: Why is “using namespace std;” considered bad practice?

Pass by value?

void printWords(string);


You are not planning on modifying so pass by const reference in preference. This will prevent an extra copy. This also tells the compiler you don't want to modify the word and thus the compiler can potentially do optimizations as it does not need to worry about mutations.

Main is special.
You do not need an explicit return. If non is defined then an implicit return 0; is panted for you. In the case where there is no chance of failure I prefer not to use an explicit return.

int main()
{
    readFile();


    return 0;
}//end main


May as well create and open file in a single statement.

    ifstream inFile;
    string words;

    inFile.open("words.txt");

    // I would have used:

    std::ifstream  inFile("words.txt");
    std::string    words;


This is always wrong:

    while(!inFile.eof())


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

It should be re-written as:

 while(inFile &gt;&gt; word)
 {
     // STUFF
 }


But you can go further. You can use iterators and loops to achieve the same affect.

 for(std::istream_iterator&lt;std::string&gt; loop(inFile); loop != std::istream_iterator&lt;std::string&gt;(); ++loop)
 {
     // STUFF
 }


Now that you can see a loop using iterators you can look at using some of the standard algorithms.

 std::for_each(std::istream_iterator&lt;std::string&gt;(inFile),
               std::istream_iterator&lt;std::string&gt;(),
               [](std::string const&amp; word){ /* STUFF */ }
              );


Don't manually close a file:

    inFile.close();


see:  My C++ code involving an fstream failed review

As noted above:

void printWords(std::string cosnt&amp; str)
{


If variables do not change then mark them constant.

    char a = 'a', e = 'e', z = 'i', o ='o', u ='u';


I would have used an array here:

    int num_A = 0, num_E = 0, num_I = 0, num_O = 0, num_U = 0;


Prefer not to use std::endl  unless you want to force a flush (you usually don't as it is very costly). Prefer to use "\n" this has the same effect but with no flush.

    cout&lt;&lt; str &lt;&lt;endl;


If you know your access is in bounds then prefer to use operator[] it does no checking and is thus faster. Prefer .at() if you want to force the check.

        if(str.at(i) == a)


Saying all that I would have use a standard function to count vowels.

std::count_if(std::begin(word), std::end(word), [](char l){return isVowel(l);});

bool isVowel(unsigned char l)
{
    static char vowel[] = 
                    {    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, // aeio
                         0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // u
                         0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, // AEIO
                         0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // U
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    return vowel[l];
}


My solution would be:

#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;


WordInfo class

class WordInfo
{
    std::string     word;
    int             count;
    public:
        WordInfo(std::string const&amp; word)
            : word(word)
            , count(0)
        {
            count   = std::count_if(std::begin(word), std::end(word),
                                    [](char l) {return isVowel(l);});
        }
        friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; str, WordInfo const&amp; data)
        {
            return str &lt;&lt; data.word &lt;&lt; " " &lt;&lt; data.count;
        }
};


main.cpp

int main(int argc, char* argv[])
{
    if (argc &lt; 2)
    {
        std::cerr &lt;&lt; "Error: Wrong usage\n";
        return -1;
    }
    std::ifstream   infile(argv[1]);

    std::copy(std::istream_iterator&lt;std::string&gt;(infile),
              std::istream_iterator&lt;std::string&gt;(),
              std::ostream_iterator&lt;WordInfo&gt;(std::cout, "\n")
             );
}


Note: The std::copy above acts like this:

    std::string     word;

    while(infile &gt;&gt; word)
    {
        std::cout &lt;&lt; WordInfo(word) &lt;&lt; "\n";
    }