Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a C++ program that will be given a text file as input (Do not use fstream

ID: 3762160 • Letter: D

Question

Design a C++ program that will be given a text file as input (Do not use fstream.) The program must find and display the average number of consonants per "word" in the file. A consonant is a letter that is not a vowel (A, E, I, O, U). Include both upper and lowercase letters in the calculation. A "word" is a sequence of consecutive non-whitespace CHARACTERS. Set precision does not have to be used.. At least one function (in addition to main) must be used.

For example, if the input file is:
The data type string is
a programmer-defined
type and is not part of
the C++ language.

The output should be: Average consonants per word is 2.6875

I do have a code from a previous exercise that may benefit this, but of course it has to be modified complete this exercise.

Explanation / Answer

#include <iostream>

#include <cctype>

using namespace std;

bool ConsonantSlayer(char inputCh);

int main()

{

int consonants = 0;

char inputch;

cin >> inputch;

while (cin)

{

if (ConsonantSlayer(inputch))

consonants++;

cin >> inputch;

}

cout << "There were " << consonants << " consonants found in the file." << endl;

  

char fileName[255];

cout<<"Enter filename: ";

cin>>fileName;

FILE *fp;

fp=fopen(fileName, "r");

  

double words=0;

double cons=0;

if (fp != NULL) {

char c;

while ((c = fgetc(fp)) != EOF) {

if (c == ' ') {

words++;

} else if (ConsonantSlayer(c)) {

cons++;

}

}

} else {

cout<<"Could not open file !!!";

}

double avg = cons/words;

cout<<"Average consonants per word is "<<avg<<" ";

return 0;

}

bool ConsonantSlayer(char inputCh){

  

inputCh = toupper(inputCh); // uppercase inputCh

  

if (inputCh >= 'A' && inputCh <= 'Z' ) { // if inputCh is between ascii value 65 and ascii value 90 execute

// the following statements

  

if (inputCh == 'A' || inputCh == 'E' || inputCh == 'I' || inputCh == 'O' || inputCh == 'U') {

return false; // inputCh is a vowel return false

}

else{

return true; // inputch is not a vowel return true

}

}

return false; // if inputCh was not between ascii values 65 and 90 then we dont care what it is, return

// return false

}