(1) Deal with "times" or "time", i.e. if <= 1, then choose "time", and else choose "times". I tried conditional operator, (a>b)? c : d..., but somehow it gives errors all the time
That has issues with precedence with surrounding <<s. Put the whole ternary thing in brackets.
(2) Does this program meet the requirements of the exercise (see the verbatim description of the exercise at the top of the program)
Doesn't seem to do so.
Write a function that reads words from an input stream and stores them in a vector. - Your function does much more than that.
Use that function BOTH to write programs that count the number of words in the input and to count how many times each word occurs - This seems to indicate that you write two programs?
(3) I don't understand much about istream& thing that the textbook uses to read words... Why is it used instead of the usual cin>>sth?
std::cin is a global instance of istream. What you are expected to write is a function that might look like this:
Code:
std::vector<std::string> read_words(std::istream& is);
which can be used to read from std::cin, a file or a stringstream
Code:
std::vector<std::string> from_console = read_words(std::cin);
std::ifstream fin("input_file.txt");
std::vector<std::string> from_file = read_words(fin);
std::string s("one two three");
std::stringstream ss(s);
std::vector<std::string> from_string = read_words(ss);
(4) How can I initialize a vector? I've read and tried all possiblities: vector<int> vec(10, 1) for 10 elements, each = 1. But it doesn't work here somehow. Error warnings immediately. In this program, I've tried
wp.count(wp.v.size(), 1) to inititiaze every element of the vector count to 1. Errors always!
You can't initialize a vector after it's been already created. You could only initialize it in the constructor of the struct you have.
But you can resize it:
Code:
wp.count.resize(wp.v.size(), 1);
(5) Lastly, if possible, please give me a hint/suggestions on how to improve this program.
Do you like the output if words occur more than once?