I am just starting to re-learn c++ having learnt the extreme basics some years ago and seem to be stumbling at the first hurdle.
I am using Code Blocks and the Ray Lischner book Exploring C++
Having typed in the very first listing it runs ok except that when I have typed in a list of words I can't figure out how to tell it that I have finished typing in and want the output to appear; the only change I made was to add using namespace std; to save typing std:: all the time; I have very carefully checked the listing and can find no errors :
Code:
nclude <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <ostream>
#include <string>
#include <vector>
using namespace std;
void read(istream& in, vector<string>& text)
{
string line;
while(getline(in, line))
text.push_back(line);
}
int main(int argc, char* argv[])
{
vector<string> text;
if (argc < 2)
read(cin, text);
else
{
ifstream in(argv[1]);
if (not in)
{
perror(argv[1]);
return EXIT_FAILURE;
}
read(in, text);
}
sort(text.begin(), text.end());
copy(text.begin(), text.end(), ostream_iterator<string>(cout, "\n"));
}