I'm really not too sure what I'm doing wrong as I have been following my course notes for the function itself. The function prototypes and main algorithm have been given to me and I need to follow the instructions to fill in the program, but I can't seem to find what my error is here and I can't really move on until I figure it out.
The program is supposed to read in a string from the user and then output the number of each vowel that the string has. My first function is initializing the vectors, and the one that I'm having trouble with is the function used to read the string from the user and save it.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
usingnamespace std;
// FUNCTION PROTOTYPES GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & frequencies);
string read_text(const string & prompt);
bool is_alphabetic(constchar character);
void create_list(const string & str_text, vector<char> & vec_text);
bool is_member(const vector<char> & list, char character);
int find_index(const vector<char> & list, char character);
int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs);
void display_characters(const vector<char> & characters, constint colwidth);
void display_freqs(const vector<int> & freqs, constint colwidth);
int main()
{
// Define local variables and constants
vector<char> vowels;
vector<int> freqs; //Hold the count of each vowel
string input; //Holds the text originally typed by the user
vector<char> text;
int consonants(0);
constint COLUMNWIDTH = 2;
// Initialize the list of vowels and vowel frequencies.
// Call function init_vectors with variables vowels and freqs
// Prompt the user for the input text by calling function read_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
// Compute the frequencies of vowels and consonants from the input text containing only alphabetic letters
// Call function compute_vowel_freqs to do this
// Display the vowels and their frequencies
// Call functions display_characters and display_freqs
// Display the number of consonants. No function calls here.
return 0;
}
// FUNCTION DEFINITIONS GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & frequencies)
{
vowels.push_back('a');
vowels.push_back('e');
vowels.push_back('i');
vowels.push_back('o');
vowels.push_back('u');
for(int i = 0; i<6; i++)
{
frequencies.push_back(0);
}
}
string read_text(const string & prompt)
{
cout << "Enter your text: ";
getline(cin, prompt);
}
and I'm getting the error:
freq.cpp: In function ‘std::string read_text(const std::string&)’:
freq.cpp:74: error: no matching function for call to ‘getline(std::istream&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
I'm not too sure if I can't use the function getline here or if there is something wrong with the function prototype itself but I'm pretty sure there isn't an error there as it was given to me. Any help here is much appreciated.
The problem is that you are trying to change a constant variable. If you notice, the parameter for the read_text function is a "const" which means that it cannot be changed. If you remove the "const" keyword then the code should work considering that you call all of your functions from your main function.
string read_text(conststring & prompt) //const means that string should not be changed
{
cout << "Enter your text: ";
getline(cin, prompt); //getline changes string by definition, so you can not use it on constants