I'm new to coding in c++ and for a homework assignment I keep getting a Segmentation Fault: 11 and I'm not sure why. I did a little research beforehand and I tried changing all my variable names, with no difference. What should I do to fix this? Below is my code.
#include <iostream>
using namespace std;
string middle(string str);
string middle(string str){
if(str.length() % 2 == 1){
cout << str[str.length()/2] << endl;
return 0;
}
else if(str.length() % 2 == 0){
cout << str[(str.length() + 1)/2];
cout << str[((str.length() + 1)/2) - 1];
return 0;
}
return 0;
}
int main(){
string a_word;
cout << "Enter a word: ";
cin >> a_word;
middle(a_word);
}