Segmentation Fault?

I keep getting a segmentation fault whenever I try to run my program. It compiles and everything okay, but when I run a.out and I input a string, it returns a segmentation fault. So I would think that it occurs somewhere after the function call for read_text()

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

// FUNCTION PROTOTYPES GO HERE:
void init_vectors(vector<char> & vowels, vector<int> & freqs);
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;
	string input;
	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

	init_vectors(vowels, freqs);
	
	// Prompt the user for the input text by calling function read_text	

	input = read_text(input);
	
	// Copy the characters (ignoring non-alphabetic characters) in the input string to the vector of characters in variable text
	create_list(input, 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

	compute_vowel_freqs(text, vowels, freqs);
	
	// Display the vowels and their frequencies

	display_characters(vowels, COLUMNWIDTH);
	
	// Call functions display_characters and display_freqs	
	
	display_freqs(freqs, COLUMNWIDTH);

	// Display the number of consonants. No function calls here.
  
  return 0;
}
		 
// FUNCTION DEFINITIONS GO HERE:

void init_vectors(vector<char> & vowels, vector<int> & freqs){
  vowels.push_back('a');
  vowels.push_back('e');
  vowels.push_back('i');
  vowels.push_back('o');
  vowels.push_back('u');
  vowels.push_back('y');

  for (int i = 0; i < 6; i++){
    freqs.push_back(0);
  }
}

string read_text(const string & prompt){
  string input;
  cout << "Enter your text: ";
  getline(cin, input);

  return(input);
}

bool is_alphabetic(const char character){
  if (character >= 65 & character <= 122){
    return(true);
      }
  else {
    return(false);
      }
}

void create_list(const string & str_text, vector<char> & vec_text){
  for (int i = 0; i < str_text.length(); i++){
    if (is_alphabetic){
      vec_text[i] = str_text[i];
    }
  }
}

bool is_member(const vector<char> & list, char character){
  for (int i = 0; i < list.size(); i++){
    if (list[i] == character){
      return(true);
    }
  }
}

int find_index(const vector<char> & list, char character){
  for (int i = 0; i < list.size(); i++){
    if (list[i] == character){
      return(i);
    }
  }
}

int compute_vowel_freqs(const vector<char> & text, const vector<char> & vowels, vector<int> & freqs){
  char character;
  for (int i = 0; i < text.size(); i++){
    if (is_member(text, character)){
      find_index(text, character);
    }
  }
}

void display_characters(const vector<char> & freqs, const int colwidth){
  cout << setw(colwidth) << freqs[0] << setw(colwidth) << freqs[1] <<
    setw(colwidth) << freqs[2] << setw(colwidth) << freqs[3] << setw(colwidth)
       << freqs[4] << setw(colwidth) << freqs[5];
}

void display_freqs(const vector<int> & freqs, const int colwidth){
  cout << setw(colwidth) << freqs[0] << setw(colwidth) << freqs[1] << setw(colwidth) << freqs[2]
       << setw(colwidth) << freqs[3] << setw(colwidth) << freqs[4] << setw(colwidth) << freqs[5];
}


The vector of chars named text is never made to be any particular size (starts out at 0), yet in create_list, you assume that vec_text.size() >= str_text.size().

Other things:
* You can finish the program without text. input is already a list-like structure of chars.
* vowels is only written to, never read from. This is a bug.
A few issues:
is_member should return false if character is not found
find_index will crash or cause undefined behavior if the character is not found (it doesn't return a value in that case)
(You should look into using std::find for both of these operations http://www.cplusplus.com/reference/algorithm/find/)

compute_vowel_freqs doesn't return anything (why aren't you getting a compiler error?)
on line 97 you're not actually calling is_alphabetic I assume it should be is_alphabetic(str_text[i])?
use push_back instead of vec_text[i] = (this is what's causing your error)
You're not initializing character in compute_vowel_freqs (you're using a garbage value resulting in undefined behavior)
Topic archived. No new replies allowed.