Question: Hello, I am writing a program to compute the frequencies of vowels and number of consonsants from a user's input. I'm encountering a logic error
Hello, I am writing a program to compute the frequencies of vowels and number of consonsants from a user's input.
I'm encountering a logic error in which items such as the space character, the exclaimation mark and others are being counted for consonants and I don't want that. Could I get some help in fixing this?
Note: everything compiles and otherwise works fine.
My program is below.







Text of Code:
#include
#include
#include
#include
using namespace std;
// FUNCTION PROTOTYPES GO HERE:
void init_vectors(vector
string read_text(const string & prompt);
bool is_alphabetic(const char character);
void create_list(const string & str_text, vector
bool is_member(const vector
int find_index(const vector
int compute_vowel_freqs(const vector
void display_characters(const vector
void display_freqs(const vector
int main()
{
// Define local variables and constants
vector
vector
string input;
vector
int consonants(0);
const int COLUMNWIDTH = 2;
// Initialize the list of vowels and vowel frequencies.
// Call function init_vectors with variables vowels and freqs
init_vectors(vowels,freqs);
// Prompt the user for the input text by calling function read_text
input=read_text("Enter your text: ");
// Copy the characters (ignoring non-alphabetic characters) in the
//input string to the vector of characters in variable text
// Call function create_list to do this
create_list(input, text);
// Compute the frequencies of vowels and consonants from the input text containing only alphabetic letters
// Call function compute_vowel_freqs to do this
consonants = compute_vowel_freqs(text, vowels, freqs);
// Display the vowels and their frequencies
// Call functions display_characters and display_freqs
display_characters(vowels, COLUMNWIDTH);
display_freqs(freqs, COLUMNWIDTH);
// Display the number of consonants. No function calls here.
cout
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
void init_vectors(vector
{
/*
PARAMETER COMMENTS: vowels is a vector holding the vowel characters
frequencies is a vector holding the integer values of the frequencies
FUNCTION PURPOSE: to initialize each according vectors with their
appropriate values/characters
*/
//add six vowels to the vowels vector
//user push_back function to insert values into vector
vowels.push_back('a');
vowels.push_back('e');
vowels.push_back('i');
vowels.push_back('o');
vowels.push_back('u');
vowels.push_back('y');
//add six zeros to the second vector
//using for loop here b/c need to push_back 6 times
//i is a loop variable start at 0, increase by 1 until 5 is reached
/ote that condition end at 5 bc vector starts at 0
for (int i=0; i
{
frequencies.push_back(0);
}
}
string read_text(const string & prompt)
{
/*
PARAMETER COMMENTS: prompt is a string PBR to hold the prompt from the main
FUNCTION PURPOSE: to prompt and read in the string of text from the user
*/
string input;
cout
getline(cin, input);
return(input);
}
bool is_alphabetic(const char character)
{
/*
PARAMTER COMMENTS: character is a character read in from another function
FUNCTION PURPOSE: to determine whether a character is alphabetic or not
*/
bool alphabetic(false);
if(((character >= 65) && (character
|| ((character >=97) && (character
{
alphabetic == true;
}
else
{
alphabetic == false;
}
return(alphabetic);
}
void create_list(const string & str_text, vector
{
/*
PARAMETER COMMENTS: str_text is a constant string PBR that holds the user's
text input
FUNCTION PURPOSE: to create a list from the user's text input that only
has alphabetic characters and add it to vec_text
*/
//for loop to iterate through the string and find the alphabetics
for (int i=0; i { if (is_alphabetic(str_text.at(i))); { //insert the character at location i in str_text to vec_text vector vec_text.push_back(str_text[i]); } } } bool is_member(const vector { /* PARAMTER COMMENTS: list is a vector which holds the user's input text character is a character value FUNCTION PURPOSE: to determine whether the character in the list at a certain location is the same as the character called or not */ //function to return true if a character appears in the vector of char. //for loop to iterate through the vector to determine if character valid for (int i=0; i { if (list[i] == character) { return(true); } } return(false); } int find_index(const vector { int indexLocation(-1); /* PARAMETER COMMENTS: list is the vector that holds the characters character is a character value */ //Function Purpose: to return index location where input char appears for (int i=0; i { if(character == list[i]) { indexLocation=i; } } return(indexLocation); } int compute_vowel_freqs (const vector { /* PARAMETER COMMENTS: text is a constant vector to hold the text from user vowels is a PBR parameter to hold the vowels freqs is a vector to hold the array */ //Function purpose: to compute the frequency of each vowel and return numConst //for loop to loop through each character int consonants(0); for (int i = 0; i { int index; // variable 'x' holds the index of the character in the vowel vector if (is_member(vowels, tolower(text[i]))) { index = find_index(vowels, tolower(text[i])); freqs[index]++; } else { consonants++; } } return(consonants); } void display_characters(const vector { /* PARAMETER COMMENTS: characters is a vector holding the characters of the user input colwidth is a constant pulled from the main function to serve for iomanip FUNCTION PURPOSE: to display the characters with appropriate spacing */ for(int i=0; i { cout } cout } void display_freqs(const vector { /* PARAMETER COMMENTS: freq is a vector holding the frequencies of vowels in the user's input colwidth is a constant pulled from the main function to serve for iomanip FUNCTION PURPOSE: to display the frequencies of vowels with appropriate spacing */ for (int i = 0; i { cout if ( i+1 { cout } } cout } Here is a picture of the outputs (top output of the solution, bottom: my program output) 
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
