Hello everyone,
I'm writing some code for an assignment that reads in a line of characters into a STL list, sorts them alphabetically and then has to reorganize them vowels to the front.
Right now I'm stuck on the step of writing a function that accepts the list as a parameter. Without the function, I can just write the line
charList.sort();
and it works great. However, one of the requirements is that I have a sort function and I'm confused on how to pass the charList into the sort function. Here's what I have so far:
//function prototypes
void sort (list);
int main()
{
char ch;
list <char> charList;
//declare file pointer for input
fstream infile;
//open file for input
infile.open("charFile.txt", ios::in);
//if statement to check if file exists
if (!infile)
cout<<"File not found."<<endl;
infile.get(ch);
charList.push_back(ch);
while (!infile.eof())
{
cout<<ch;
infile.get(ch);
charList.push_back(ch);
}
infile.close();
//charList.sort();
sort(charList);
list<char>::iterator listIt;
listIt = charList.begin();
while(listIt!= charList.end()){
cout<<*listIt;
cout<<" ";
listIt++;}
return 0;
}
//functions
void sort(list ch)
{
ch.sort();
}
Among the compiler's many errors are "missing template arguments before "ch" " so I'm assuming I need to put more into the passing parameters then just list ch. I'm also thinking its a void function because I just want to sort the list and then print it out in main.
I've already tested it and it reads in the list normally from the file and sorts it without the function.
Thanks.