I have attached a screenshot of the error on line 64 sort. The error states: identifier "sort" is undefined.
Had to remove some info. due to word count.
2) Sort the 2 or 3 parallel vectors in ascending order of all_words vector.
Need to remove unnecessary vector from the code which is not used.
And also need to sort the 2 03 3 parallel vectors which has been used in the code.
Using Sort on 2nd Vector
As 3rd vector is removed 2nd vector is left to be sorted, so revised code along with sorted values of all_words_counts.
#include #include #include #include #include #include #include #include using namespace std;const string empty_string = "";vector getWords(const string& text);int findWord(const vector& words, const string& word);int main(){ vector all_words; vector all_word_counts; //vector<>> all_line_numbers; string input_file_name; cout < "source="" file="" name?=""> getline(cin, input_file_name); ifstream finp; finp.open(input_file_name); assert(finp.good()); string line; int word_counter = 0; int max_word_length = 0; int max_word_count = 0; int line_number = 0; getline(finp, line); while (!finp.eof()) { line_number++; vector words = getWords(line); word_counter += words.size(); for (int idx = 0; idx < words.size();=""> { int widx = findWord(all_words, words[idx]); if (widx == -1) { cout < words[idx]=""><> all_words.push_back(words[idx]); all_word_counts.push_back(1); vector word_line_numbers; word_line_numbers.push_back(line_number); // all_line_numbers.push_back(word_line_numbers); if (words[idx].length() > max_word_length) max_word_length = words[idx].length(); } else { all_word_counts[widx]++; // all_line_numbers[widx].push_back(line_number); if (all_word_counts[widx] > max_word_count) max_word_count = all_word_counts[widx]; } } getline(finp, line); } sort(all_word_counts.begin(),all_word_counts.end()); finp.close(); string output_file_name = input_file_name; string time_as_string = to_string(static_cast(time(nullptr))); int period_pos = input_file_name.find_last_of('.'); if (period_pos == -1) output_file_name = output_file_name + time_as_string + ".txt"; else output_file_name = output_file_name.replace(period_pos, output_file_name.length() - period_pos + 1, time_as_string + ".txt"); ofstream fout; fout.open(output_file_name); assert(fout.good()); cout < word_counter=""><> const int word_counts_fwidth = static_cast(log10(max_word_count)) + 1; const int line_number_fwidth = static_cast(log10(line_number)) + 1; const int words_per_line = 36; //using new variable for reference int newindex = 0; for (int idx = 0; idx < all_words.size();=""> { fout < setw(max_word_length)="">< left="">< all_words[idx]="">< '="" '="">< right="">< setw(word_counts_fwidth)="">< right="">< all_word_counts[idx]=""><> //for (int idx2 = 0; idx2 < all_line_numbers[idx].size();=""> for (int idx2 = 0; idx2 < all_word_counts[idx];=""> { if (idx2 != 0 && idx2 % words_per_line == 0) fout <> fout < setw(line_number_fwidth)="">< right="">< ++newindex="">< '=""> } fout <> fout <> } fout.close(); system("pause"); return 0;}int findWord(const vector& words, const string& word){ for (int idx = 0; idx < words.size();=""> if (words[idx] == word) return idx; return -1;}vector getWords(const string& text){ vector words; int start_pos = 0; while (start_pos <> { while (start_pos < text.length()="" &&=""> start_pos++; string word = empty_string; while (start_pos < text.length()="" &&=""> { word += static_cast(toupper(text[start_pos])); start_pos++; } if (word.length() > 0) words.push_back(word); } return words;}
using namespace std;
const string empty_string = "";vector getWords(const string& text);int findWord(const vector& words, const string& word);int main(){ vector all_words; vector all_word_counts; //vector<>> all_line_numbers; string input_file_name; cout < "source="" file="" name?=""> getline(cin, input_file_name); ifstream finp; finp.open(input_file_name); assert(finp.good()); string line; int word_counter = 0; int max_word_length = 0; int max_word_count = 0; int line_number = 0; getline(finp, line); while (!finp.eof()) { line_number++; vector words = getWords(line); word_counter += words.size(); for (int idx = 0; idx < words.size();=""> { int widx = findWord(all_words, words[idx]); if (widx == -1) { cout < words[idx]=""><> all_words.push_back(words[idx]); all_word_counts.push_back(1); vector word_line_numbers; word_line_numbers.push_back(line_number); // all_line_numbers.push_back(word_line_numbers); if (words[idx].length() > max_word_length) max_word_length = words[idx].length(); } else { all_word_counts[widx]++; // all_line_numbers[widx].push_back(line_number); if (all_word_counts[widx] > max_word_count) max_word_count = all_word_counts[widx]; } } getline(finp, line); } sort(all_word_counts.begin(),all_word_counts.end()); finp.close(); string output_file_name = input_file_name; string time_as_string = to_string(static_cast(time(nullptr))); int period_pos = input_file_name.find_last_of('.'); if (period_pos == -1) output_file_name = output_file_name + time_as_string + ".txt"; else output_file_name = output_file_name.replace(period_pos, output_file_name.length() - period_pos + 1, time_as_string + ".txt"); ofstream fout; fout.open(output_file_name); assert(fout.good()); cout < word_counter=""><> const int word_counts_fwidth = static_cast(log10(max_word_count)) + 1; const int line_number_fwidth = static_cast(log10(line_number)) + 1; const int words_per_line = 36; //using new variable for reference int newindex = 0; for (int idx = 0; idx < all_words.size();=""> { fout < setw(max_word_length)="">< left="">< all_words[idx]="">< '="" '="">< right="">< setw(word_counts_fwidth)="">< right="">< all_word_counts[idx]=""><> //for (int idx2 = 0; idx2 < all_line_numbers[idx].size();=""> for (int idx2 = 0; idx2 < all_word_counts[idx];=""> { if (idx2 != 0 && idx2 % words_per_line == 0) fout <> fout < setw(line_number_fwidth)="">< right="">< ++newindex="">< '=""> } fout <> fout <> } fout.close(); system("pause"); return 0;}int findWord(const vector& words, const string& word){ for (int idx = 0; idx < words.size();=""> if (words[idx] == word) return idx; return -1;}vector getWords(const string& text){ vector words; int start_pos = 0; while (start_pos <> { while (start_pos < text.length()="" &&=""> start_pos++; string word = empty_string; while (start_pos < text.length()="" &&=""> { word += static_cast(toupper(text[start_pos])); start_pos++; } if (word.length() > 0) words.push_back(word); } return words;}
} else { all_word_counts[widx]++; // all_line_numbers[widx].push_back(line_number); if (all_word_counts[widx] > max_word_count) max_word_count = all_word_counts[widx]; } } getline(finp, line); } sort(all_word_counts.begin(),all_word_counts.end()); finp.close(); string output_file_name = input_file_name; string time_as_string = to_string(static_cast(time(nullptr))); int period_pos = input_file_name.find_last_of('.'); if (period_pos == -1) output_file_name = output_file_name + time_as_string + ".txt"; else output_file_name = output_file_name.replace(period_pos, output_file_name.length() - period_pos + 1, time_as_string + ".txt"); ofstream fout; fout.open(output_file_name); assert(fout.good()); cout < word_counter=""><> const int word_counts_fwidth = static_cast(log10(max_word_count)) + 1; const int line_number_fwidth = static_cast(log10(line_number)) + 1; const int words_per_line = 36; //using new variable for reference int newindex = 0; for (int idx = 0; idx < all_words.size();=""> { fout < setw(max_word_length)="">< left="">< all_words[idx]="">< '="" '="">< right="">< setw(word_counts_fwidth)="">< right="">< all_word_counts[idx]=""><> //for (int idx2 = 0; idx2 < all_line_numbers[idx].size();=""> for (int idx2 = 0; idx2 < all_word_counts[idx];=""> { if (idx2 != 0 && idx2 % words_per_line == 0) fout <> fout < setw(line_number_fwidth)="">< right="">< ++newindex="">< '=""> } fout <> fout <> } fout.close(); system("pause"); return 0;}int findWord(const vector& words, const string& word){ for (int idx = 0; idx < words.size();=""> if (words[idx] == word) return idx; return -1;}vector getWords(const string& text){ vector words; int start_pos = 0; while (start_pos <> { while (start_pos < text.length()="" &&=""> start_pos++; string word = empty_string; while (start_pos < text.length()="" &&=""> { word += static_cast(toupper(text[start_pos])); start_pos++; } if (word.length() > 0) words.push_back(word); } return words;}
for (int idx2 = 0; idx2 < all_word_counts[idx];=""> { if (idx2 != 0 && idx2 % words_per_line == 0) fout <> fout < setw(line_number_fwidth)="">< right="">< ++newindex="">< '=""> } fout <> fout <> } fout.close(); system("pause"); return 0;}int findWord(const vector& words, const string& word){ for (int idx = 0; idx < words.size();=""> if (words[idx] == word) return idx; return -1;}vector getWords(const string& text){ vector words; int start_pos = 0; while (start_pos <> { while (start_pos < text.length()="" &&=""> start_pos++; string word = empty_string; while (start_pos < text.length()="" &&=""> { word += static_cast(toupper(text[start_pos])); start_pos++; } if (word.length() > 0) words.push_back(word); } return words;}
}
I am able to run this program without making any changes successfully without any error or issue.
the red line under sort is not an error, if you will run the program it will run successfully without any error. possible reason of the line under the sort word is because you might be using old version of c++ compiler. you can suppress these warning in IDE or you can ignore this one altogether.
if you still face the issue. please post the screenshot of the error also.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here