-1

So one of the prompt of my lab is: "Find the percentage of vowels and percentage consonants in the English language. You should get a percentage of vowels = 37.4% and consonants = 62.5%."

Here is just my percentage function. I think there may be something wrong with the for loops but I can't seem to figure it out.. Thanks for the help!

int pv(string w) {
double numC=0;
double numV=0;
string fName;
ifstream inFile;
double pc;


cout << "Enter file name of dictionary (Mac users type in full path): ";
cin >> fName;

if (inFile.fail()) {
    cout << "Error opening file" << endl;
    exit(1);
}

while (!inFile.eof()) {
    getline(inFile, w);

for (int i=0; i<w.length(); i++) {
    if(w[i]==('a')||w[i]==('e')||w[i]==('i')||w[i]==('o')||w[i]==('u'))
        numV=numV+1;
    else {
        numC=numC+1;

    }
}
}
cout << numV;
return 0;
}
2lnk
  • 11
  • 3
  • Why `w[i]==('a')`? You can get rid of the parenthesis. Just do `w[i] == 'a'`. – SegFault Jul 20 '16 at 21:18
  • I just got rid of them and the problem is still there :( But, thank you for your input! Do you think there is something else that could be wrong? – 2lnk Jul 20 '16 at 21:21
  • 1
    You need to know that [`while(!stream.eof()){ ...}`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) is considered wrong – WhiZTiM Jul 20 '16 at 21:21
  • Then there could be uppercase and lowercase characters. Also something to worry about. –  Jul 20 '16 at 21:21
  • But doesn't the function of that while loop expression says that the calculations won't end until the file ends? – 2lnk Jul 20 '16 at 21:23
  • Read the link by WhiZTiM, basically your loop condition is letting you read past the end of the file, which is bad. Also, you seem to assume that all letters encountered will be either vowels or consonants, aside from being lowercase. Is this the case? What about spaces and punctuation? – alter igel Jul 20 '16 at 21:26
  • Your eof test is badly placed. The end of file bit is set when a read is attempted and there is no more data. – D Hydar Jul 20 '16 at 21:28
  • Ok.. I get it.. sorry, I'm super new to all of this. (I also didn't know it was a link lol). – 2lnk Jul 20 '16 at 21:35
  • So, should I do this? while (inFile >> data) and then do getline(data,w) ? – 2lnk Jul 20 '16 at 21:36
  • `while (inFile >> w)` will read the whole file for you, then you can loop through `w` and count consonants and vowels. I recommend making two functions, `isvowel` and `isconsonant` to determine vowels and consonants if only just because it makes your code easier to read: `if (isconsonant(w[i])) numC++; else if (isvowel(w[i])) numV++;` – user4581301 Jul 20 '16 at 22:00

1 Answers1

1

Here is one of the solutions to your problem. Keep vowels, consonants and all characters in separate sets, read one character at the time from a file. If match is found in any of the sets then increment the appropriate counter. Calculate the percentage based on those counters:

#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
using namespace std;

int main() {
    set<char> vowels = { 'a', 'e', 'i', 'o', 'u' };
    set<char> consonants = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w', 'y' };
    set<char> allchars = { 'a', 'e', 'i', 'o', 'u', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x', 'z', 'w', 'y' };
    char ch;
    int vowelsCount = 0;
    int consonantsCount = 0;
    int percentageVowels = 0;
    int percentageConsonants = 0;
    int charactersCount = 0;
    fstream fin("MyFile.txt", fstream::in);

    while (fin >> ch) {
        ch = tolower(ch);
        if (find(allchars.begin(), allchars.end(), ch) != allchars.end())
        {
            charactersCount++;
        }
        if (find(vowels.begin(), vowels.end(), ch) != vowels.end())
        {
            vowelsCount++;
        }
        if (find(consonants.begin(), consonants.end(), ch) != consonants.end())
        {
            consonantsCount++;
        }
    }
    percentageVowels = double(vowelsCount) / charactersCount * 100;
    percentageConsonants = double(consonantsCount) / charactersCount * 100;
    cout << "Vowels     %: " << percentageVowels << endl;
    cout << "Consonants %: " << percentageConsonants << endl;
    getchar();
    return 0;
}