0

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; } 
5
  • 1
    This is probably related to your environment. Are you using an IDE? Is it Visual Studio Community on Windows? If so, you probably just need a std::cin.get(); before your program exits to keep the window open. Commented Apr 28, 2021 at 15:34
  • Hey right now im just using this site cpp.sh . Commented Apr 28, 2021 at 15:37
  • I wrote an answer that "fixes" the bug, by removing the UB, but the last nested loops are probably not doing what you want either. Could you add a description of what that code is supposed to be doing? Commented Apr 28, 2021 at 15:47
  • @cigien should I add to the code ? Or explain it on the comments? I fixed the remaining code! Thanks for the fix that was really helpful. I thought I could sort from highest to lowest. Commented Apr 28, 2021 at 19:45
  • Since the bug doesn't depend on the stuff after 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 write sort(indices_sorted.begin(),indices_sorted.end(), std::greater{}); instead. Commented Apr 28, 2021 at 20:20

1 Answer 1

2

You've written

sort(indices_sorted.end(),indices_sorted.begin()); 

which violates a pre-condition of std::sort, which is that the second iterator must be reachable from the first by incrementing it. This won't be the case in your code unless indices_sorted is empty (i.e. begin is equal to end), and so you're invoking undefined behavior.

You need to swap the arguments

sort(indices_sorted.begin(),indices_sorted.end()); 

and then your program works

Sign up to request clarification or add additional context in comments.

3 Comments

Works ... but only (it seems) with two input words. Try with input of alan kate bob dave break.
@AdrianMole Hmm, good point. I'm not sure what the last part is supposed to do, so I'll try and get some clarification.
Hey the last part is supposed to sort the words from shortest to longest. The algorithm is doing that right now be converting to the length in integers, saving that on two vectors and sorting one of them. Afterwards the last loop compares sorted and unsorted and if both have the same length passing the position word[counter] to words_sorted. In the current Code ther is a bug right now that is that indices already used still can be used. I fixed that by setting the unsorted_lengths[counter] to zero. Probably way to much computing for the simple task but I hope this gives clarity!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.