***please help***Counting Vowels in a string

So I'm supposed to create a program that will read off words from a .txt file,and use a function to count the number of vowels in each word and then display the specific vowels and their frequencies.
ex. if the word is: butter
the output would be:

a:0
e:1
i:0
o:0
u:1

This is what I have so far, but I'm running into some problems. Any help would be GREATLY appreciated.
Thank you in advance

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void PrintFrequencies(string str);

char vowa = 'a';
char vowe = 'e';
char vowi = 'i';
char vowo = 'o';
char vowu = 'u';

int freqa = 0;
int freqe = 0;
int freqi = 0;
int freqo = 0;
int frequ = 0;


int main()
{
ifstream infile;
string tempData;
infile.open("words.txt");

while(!infile.eof())
{
getline(infile, tempData);
PrintFrequencies(tempData);
}


infile.close();


return 0;
}


void PrintFrequencies(string str)
{
cout<<"The word is: "<<str<<endl<<endl;
cout<<"Its vowel frequencies are: "<<endl<<endl;

for(int i=0; i<=str.length(); i++)
{
if(str.at(i) == vowa)
{
freqa++;
cout<<"a: "<<freqa<<endl;
}

else if(str.at(i) == vowe)
{
freqe++;
cout<<"e: "<<freqe<<endl;
}
else if(str.at(i) == vowi)
{
freqi++;
cout<<"i: "<<freqi<<endl;
}
else if(str.at(i) == vowo)
{
freqo++;
cout<<"o: "<<freqo<<endl;
}
else if(str.at(i) == vowu)
{
frequ++;
cout<<"u: "<<frequ<<endl;
}

}




}
Last edited on
I'm running into some problems
There is some solutions.

Please state your problems clearly so we can help.

One I see: for(int i=0; i<=str.length(); i++) will lead to exception thrown by .at(). Use: i<str.length().

Second I noticed: your program does count uppercase letters. To solve this, use tolower function before comparing characters. if( tolower(str.at(i)) == //...

I would suggest to use lookup table for this. It will look cleaner, easier to adapt and less error prone.
Sorry I should have clarified, it is assumed that the words will be in all lowercase characters.

My major issue is getting the output to look right.
As it is now, the program prints this to the screen

The word is: salamander

It's vowel frequencies are:

a: 1
a: 2
a: 3
e: 1


The word is: bread

It's vowel frequencies are:

a: 4
e: 2


etc.

How do I get the counters (freqa, freqe, etc.) to reset to 0 before each word is analyzed?

Also, how do I get it to print the vowels only once? (how do I get it to look like the output in the original question?)

Thank you so much for your input!
How do I get the counters (freqa, freqe, etc.) to reset to 0 before each word is analyzed?
Assign them to zero before your for loop. Or alternatively: do not use global variables:
1
2
3
4
5
6
7
8
9
10
11
void PrintFrequencies(string str)
{
    cout<<"The word is: "<<str<<endl<<endl;
    cout<<"Its vowel frequencies are: "<<endl<<endl;
    int freqa = 0;
    int freqe = 0;
    int freqi = 0;
    int freqo = 0;
    int frequ = 0;
    for(int i=0; i<=str.length(); i++)
//... 


how do I get it to print the vowels only once?
Print them after your loop at the end of function



Also, another take on your function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void PrintFrequencies(std::string str)
{
    std::cout << "The word is: " << str << "\n\n";
    std::cout << "Its vowel frequencies are:\n";
    int freq[256] = { 0 };
    for(unsigned char c: str)
        ++freq[c];
    std::cout << "a: " << freq['a'] << '\n' <<
                 "e: " << freq['e'] << '\n' <<
                 "i: " << freq['i'] << '\n' <<
                 "o: " << freq['o'] << '\n' <<
                 "u: " << freq['u'] << '\n';
}
The word is: butter

Its vowel frequencies are:
a: 0
e: 1
i: 0
o: 0
u: 1
The word is: jelly

Its vowel frequencies are:
a: 0
e: 1
i: 0
o: 0
u: 0
Topic archived. No new replies allowed.