I have a break statement to exit the loop in:
while (cin >> text){ if (text == "break"){ break; }; cout << text << endl; words.push_back(text); } The problem is that program just stops afterward with
Exit code: 0 (normal program termination).
Here is my full program:
// Example program #include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; int main() { string text; vector<string> words; vector<int> indices; vector<int> indices_sorted; vector<string> words_sorted; cout << "type in you're desired Text: "; while (cin >> text){ if (text == "break"){ break; }; cout << text << endl; words.push_back(text); } int size = words.size(); for (int i =0; i < size; i++){ indices_sorted.push_back(words[i].size()); } indices = indices_sorted; sort(indices_sorted.end(),indices_sorted.begin()); cout << "you typed: " << words.size() << " words!"; int counter = 0 ; for (int i =0; i < size; i++){ while (indices[i] != indices[counter])counter++; words_sorted.push_back(words[counter]); counter = 0; cout << words_sorted[i]<< endl; } return 0; }
std::cin.get();before your program exits to keep the window open.sort, simply fix that part of the code (and do it in the question, not comments). BTW, you can definitely sort from highest to lowest, but you would writesort(indices_sorted.begin(),indices_sorted.end(), std::greater{});instead.