1

Sorry for the title, but I really have no idea what the problem is. The code looks like that (here it has no sense, but in the bigger project is has, so please, do not ask "why do you want to do....")

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

string sort (string slowo){
  string litery = slowo;

  for (int i=0; i<litery.length()-1; i++)
     for (int j=0; j<litery.length()-1; j++)
        if (litery[j]>litery[j+1])
             swap(litery[j], litery[j+1]); // (3)

  return litery;
}

int main()
{

    fstream wordlist;
    wordlist.open("wordlist_test",ios::in);
    vector<string> words;

    while (!wordlist.eof()){ // (4)
      bool ok = true;
      string word;
      getline(wordlist,word);
      string sorted = sort(word);

      if (ok){
        cout<<word<<endl; // (1)
        words.push_back(word);
     }
  }

  for (int i = 0; i<words.size(); i++){
    cout<<words[i]<<endl; // (2)
  }

}

There are for words in file "wordlist_tests". Program at the end should just write them to vector and write what's in vector into standard output. The problem is:

  • however line(1) proves that all words are ok
  • vector appears to be empty in line (2)

now iteresting (probably just for me) part:

there are two ways to make it right:

  • I can just remove line(3) (however, if I am right, as the variable is passed to sort function through the value, it just swap two letters in independent variable; it has nothing to do with my vector), or:
  • I can change condition in while loop (4).

for example just like this:

int tmp = 0;
while (tmp < 5){
tmp++;
/..../

What is wrong with this code? What should I do write these words down to vector but still sort them and using this while loop? I cannot find the connection between this things (ok, I see that connection is variable word, but I do not know in what way). Any help appreciate.

Malvinka
  • 778
  • 1
  • 7
  • 25
  • 2
    Is this the actual code or just an approximation? – molbdnilo Jan 25 '15 at 20:47
  • You forgot to include the __actual output__ and the __intended output__. Currently, I'm unable to reproduce your error as well. – Bill Lynch Jan 25 '15 at 20:51
  • this is actual code - you can compile it (however written just for showing the problem so without practical sense). Actual output is empty. Intended output are words from file. – Malvinka Jan 25 '15 at 20:54
  • @Malvinka: I've changed your code to use a `stringstream` instead of a `fstream` so there's no external file necessary. But I can't reproduce this error: http://ideone.com/LIOyOD – Bill Lynch Jan 25 '15 at 20:55
  • @BillLynch can you please check one more time? I changed 27th and 28th lines (I copied version for tests not original one and did not notice that before your answer - I am really really sorry and really appreciate your interest) – Malvinka Jan 25 '15 at 20:58
  • [Don't use eof() to detect end of file](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). One of the side-effects of this mistake is that your program can behave as if the file had an extra blank line or garbage line on the end. – M.M Jan 25 '15 at 22:59
  • what can I use instead? – Malvinka Jan 26 '15 at 11:34

1 Answers1

3

What happens in swap() if one of the words is the empty sting ""?

  1. If this happens, litery = "".
  2. The condition in the loops will be to iterate from 0 to (unsigned) 0 - 1, which is a very large number.
  3. You'll then execute if (litery[0] > litery[1])
  4. litery[1] will access beyond the end of the empty string, which causes undefined behavior.

Let's fix this:

The common fix for this, is to iterate from 1 to string.length(). Here's an example:

string sort (string litery){
    for (int i=1; i<litery.length(); i++)
        for (int j=1; j<litery.length(); j++)
            if (litery[j-1]>litery[j])
                swap(litery[j-1], litery[j]); 

    return litery;
}
Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
  • Wow. The simple if (word.compare("")) before sort(word) is enough... But I dont get it. Neither in your example nor in my file there is no empty string. So why that still doesnt work? Why does it influent my vector at all? – Malvinka Jan 25 '15 at 21:08
  • 1
    In my example, there are 5 lines in the string stream: `"hello"`, `"dog"`, `"cat"`, `"mouse"`, `""`. Note that most text editors will insert a newline at the end of the file. – Bill Lynch Jan 25 '15 at 21:09
  • Now I know what is going on. Thanks a lot! – Malvinka Jan 25 '15 at 21:13