I am unsure why I am reciveing a segmentation fault:
INPUT: ./a.out

Enter the output file name (maximum of 15 characters):
output.doc
Enter the number of names to sort in lexicographical order: 3
Enter a name: John Smith
Enter a name: Mel Gibson
Enter a name: Mark Anthony
Segmentation fault

--------------------------------------------------------

#include<cctype>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int size;
//string names[size];
char out_file_name[16];
ofstream out_stream;
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters);
void bubble_sort(string name);
int main()
{ 
int index;
// string names[size];
vector<string> tokens;
string str;
int i = 1;
int k = 0;
cout << "Enter the output file name (maximum of 15 characters):\n";
cin >> out_file_name;
cout << "Enter the number of names to sort in lexicographical order: ";
cin >> size;
string names[size];
out_stream.open(out_file_name);
if (out_stream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
cin.ignore(15, '\n');
for(index=0; index<size; index++)
{ cout<<"Enter a name: ";
getline(cin,str); 
Tokenize(str, tokens, " ");
names[index] = tokens[index + i] + ", " + tokens[index + k];
i++;
k++; 
}
bubble_sort(names[size]);
for(index=0; index<size; index++) 
out_stream<<names[index]<<"\n";
}
void Tokenize(const string& str, vector<string>& tokens, const string& delimiters)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
void bubble_sort(string name)
{
int i, j, flag = 1;
char temp;
int arrayLength = 3; 
for(i = 1; (i <= arrayLength) && flag; i++)
{
flag = 0;
for (j=0; j < (arrayLength - 1); j++)
{
if (name[j+1] < name[j]) 
{ 
temp = name[j]; // swap elements
name[j] = name[j+1];
name[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return; 
}

Don't you have to give a constant value for size? The program does not even compile in my compiler.

string names[size];
This article has been dead for over six months. Start a new discussion instead.