Help with an error with strings and functions

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.

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

// FUNCTION PROTOTYPES GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & frequencies);
string read_text(const string & prompt);
bool is_alphabetic(const char 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, const int colwidth);
void display_freqs(const vector<int> & freqs, const int 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);
	
	const int 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.

Tom
1
2
3
4
string read_text(const string & 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 
Change your last function to:
1
2
3
4
5
string read_text(string & prompt)
{
  cout << "Enter your text: ";
  getline(cin, prompt);
}
Ah I see now, thank you very much all, I appreciate it very much!
Topic archived. No new replies allowed.