what I mean is from this code, when coder doesn't put any curly bracket({,}), what does the while loop scope will cover? already test with random bracket put on the code, but, the only "brackected code" that work the same as the original code is when I put around words.push_back(word);
got no clue what make the scope like that even thought the coder doesn't put any...
:S
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
int main()
{
// Ask for and read the words
cout << "Please enter a few words, followed by end-of-file: ";
vector<string> words;
string word;
// Invariant: words contains all of the words read so far
while (cin >> word)
words.push_back(word);
typedef vector<string>::size_type vec_sz;
vec_sz size = words.size();
// Check that the user entered some words
if (size == 0)
{
cout << endl << "You didn't enter any words. "
"Please try again." << endl;
return 1;
}
// sort the words
sort(words.begin(), words.end());
string current_word;
int count;
// Set the initial word to the first word in the vector
current_word = words[0];
// Set the initial count for the first word
count = 1;
// Invariant: we have counted current_index of the total words
// in the vector
for (vec_sz current_index = 1; current_index < size; ++current_index)
{
// Report the count for the current word if it does not match
// the word at the current index in the vector, and reset the
// count to zero so that it will one when the variable is
// incremented outside the if statement.
if (current_word != words[current_index])
{
cout << "The word \"" << current_word << "\" appears "
<< count << " times." << endl;
current_word = words[current_index];
count = 0;
}
++count;
}
// Report the count for the final word
cout << "The word \"" << current_word << "\" appears "
<< count << " times." << endl;
// We have reported the count of all the words in the vector, so exit.
return 0;
}