An incomplete C++ program is given designed to read the non-whitespace character
ID: 3684509 • Letter: A
Question
An incomplete C++ program is given designed to read the non-whitespace characters in a file and count the number of consonants (upper and lowercase) and the number of lowercase vowels (a, e, i, o, and u).
In order to count the consonants, the program is designed to call a value-returning function that is passed a character as a parameter and returns true if the character is an upper or lowercase consonant, false otherwise. This function needs to be defined.
Lowercase vowels are determined by calling a value-returning function that receives a character and returns true if it is a lowercase vowel, false otherwise. This function needs to be defined.
Comments have been placed in the file that describe what needs to be done to complete the program. Add code to do the following:
Include any header files needed for the library functions you decide to use in the program.
Do not use Fstream, String, or Vectors in your solution, as the program can function without. Input will be taken through cin.
Provide the function prototypes for the 2 functions. (You may want to wait until you have completed the function definitions to write them.)
Add the function calls that will provide the logical expression parts of the if statements. (You may want to wait until you have completed the function definitions to write them.)
Write the function definitions for the 2 functions. See descriptions above.
DO NOT RE-WRITE/CHANGE THE FUNDAMENTAL STRUCTURE OF THE PROGRAM.
ASSUMPTIONS
The input file will not be empty.
A lowercase vowel is an a, e, i, o, or u.
A consonant is a letter that is not an upper or lowercase vowel.
NOTES:
If you use library functions, make sure you include the appropriate header files.
Sample terminal session:
[keys]$ more data4nine
Test Data:
Here is a SMALL test
file TO SEE if my
exERCise 9 program
is WorkIng. The End!
[keys]$ g++ ex9.cpp
[keys]$ ./a.out < data4nine
There were 41 consonants found in the file.
There were 19 lowercase vowels found in the file.
Here is the incomplete code:
#include <iostream>
//include any additional header files needed by program
using namespace std;
// insert function prototype for consonant checking function
// insert function prototype for lowercase vowel checking function
int main()
{
int consonants = 0; //consonant counter
int vowels = 0; //vowel counter
char inputch; //current input character
// insert print statement to display name, lec and lab #s, exercise#
cin >> inputch;
while (cin)
{
if (/* insert function call for consonant checker */)
consonants++;
if (/* insert function call for vowel checker */)
vowels++;
cin >> inputch;
}
cout << "There were " << consonants
<< " consonants found in the file." << endl;
cout << "There were " << vowels
<< " lowercase vowels found in the file." << endl;
return 0;
}
// Place function definition for consonant checker here.
// Place function definition for vowel checker here.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const string FILEPATH = "EnglishWords.txt"; //path of file containing words
int getVowelCount(string word);
int getConsonantCount(string word);
int main(){
int vowels = 0;
int totalVowels = 0;
double vowelPercentage;
int consonants = 0;
int totalConsonants = 0;
double consonantPercentage;
double totalLetters = 0; //number of letters in text
string inputWord;
ifstream inputFile;
cout << fixed << showpoint << setprecision(1);
inputFile.open(FILEPATH.c_str()); //Open file with words
if(!inputFile){ //Error if no file
cout << "File not found" << endl;
return 1;
}
inputFile >> inputWord;
while(inputFile){ //iterate through all words in file
vowels = getVowelCount(inputWord);
totalVowels = totalVowels + vowels; //add vowel count to running total
consonants = getConsonantCount(inputWord);
totalConsonants = totalConsonants + consonants; //add consonant count to running total
inputFile >> inputWord; //get next word
}
totalLetters = totalVowels + totalConsonants; //add vowels and consonants together to get total letter count
vowelPercentage = (totalVowels / totalLetters) * 100.0; //divide and multiply by 100 to get percentage
consonantPercentage = (totalConsonants / totalLetters) * 100.0;
//cout << "Number of vowels: " << totalVowels << endl;
//cout << "Number of consonants: " << totalConsonants << endl;
cout << "Percentage of vowels: " << vowelPercentage << endl;
cout << "Percentage of consonants: " << consonantPercentage << endl;
inputFile.close(); //close file
char reply;
cout << "Press q to quit" << endl;
cin >> reply;
return 0;
}
/*
Functions to determine vowels and consonants. This does not determine if y is a vowel or consonant and
is treated as a consonant.
*/
int getVowelCount(string word){ //function to determine number of vowels in word
char letter;
int count = 0;
int wordLength = word.length();
for(int i = 0; i < wordLength; i++){ //iterate through all letters of word
letter = word.at(i); //read letter at position i
letter = tolower(letter); //make letter lowercase
switch(letter){ //determine if vowel
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++; //increase counter of vowels in word
default:
break;
}
}
return count;
}
int getConsonantCount(string word){ //function to determine number of consonants in word
char letter;
int count = 0;
int wordLength = word.length();
for(int i = 0; i < wordLength; i++){ //iterate through all letters of word
letter = word.at(i); //read letter at position i
letter = tolower(letter);
switch(letter){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
break; //break statement if letter is vowel
default:
count++; //if letter is consonant, increase count
}
}
return count;
}
EnglishWords.txt
ant
bird
apple
sample
Percentage of vowels: 33.3
Percentage of consonants: 66.7
Press q to quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.