0

This is my code:

//Declare variables for location of spaces
    int i = 0;
    char ignore;
    vector<int> spaces;
    int location;
    int x = 0;

    //Begin finding the spaces
    while (!inData.eof())
    {
        inData >> location;
        i = location - 1;
        i++;
        spaces.push_back(i);
        inData >> ignore;
    }
    for (int x = 0; x <= spaces.size(); x++)
    {

    output[spaces[x]] = ' ';

    }

It's not the full code but it's where the problem originates.
Output is a string of words that are encrypted. My vector holds the locations to where the spaces are located in the string and so I'm using those numbers in the vector to replace the letters with a space.

To better explain, I had an encrypted word and I've already shifted the letters back to where they should be.
output = "HellopWorldqBye"

The spaces are located at position 5 and 11. The vector holds those numbers.

output[spaces[x]] = ' ' should replace those letters with spaces to make output = "Hello World Bye" but instead it crashes the program.

I can't figure out what my issue would be rather than because the vector is inside an array?

The only other way I know how to do it without it crashing my program is to store the space locations inside of an array instead and it works but then I have to declare the size of my array as well as make the for loop <= the exact size of the array or else it crashes and the amount of spaces in the output string will not ever be the same unless someone inputs 3 words everytime. "Hello World Bye" was just an example but the reality is that it won't ever be the same string.

Any help is greatly appreciated to solve my problem.

Baldrickk
  • 3,827
  • 1
  • 12
  • 25
Cush
  • 67
  • 7
  • 1
    first you are setting `i` to `location-1`, then you are incrementing `i` . This does not seem right. May I suggest using a debugger, and stepping throw you code step by step to see if it does what you expect. – sp2danny Oct 22 '14 at 07:46
  • @sp2danny `location` is being set by reading it from `inData`. Without error checking, but that's another story. – Thomas Oct 22 '14 at 07:47
  • yes, what sp2danny said. I wasn't displaying the full code, just enough to see and understand the problem within my code. Do I have to check for errors everytime I read something from a file? I have one for when I open the file initially but that's at the beginning of my code. – Cush Oct 22 '14 at 07:55
  • 1
    `while (!inData.eof())` is broken - see [here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Tony Delroy Oct 22 '14 at 08:08
  • This code has several issues...the reading of that char into `ignore` is a bad name because [there is an ignore() method on the stream itself](http://en.cppreference.com/w/cpp/io/basic_istream/ignore) which is what you should use if you want to ignore characters. But secondly there is no clear motive for ignoring, as this would result in reading "5" and the second 1 in "11" to put spaces at 5 and 1. Also, TonyD just added the other comment I was about to make. – HostileFork says dont trust SE Oct 22 '14 at 08:09

1 Answers1

4
for (int x = 0; x <= spaces.size(); x++)
                   ^ problem is here

Indices into arrays, strings, vectors and such are always numbered from 0 to size-1, inclusive, but your for loop is accessing the vector at size, one beyond the end of the vector. This results in undefined behaviour: in this case, a crash.

Change it to < instead.

Thomas
  • 150,847
  • 41
  • 308
  • 421
  • You are a life saver!!!!! I had originally had it as an array like our teacher told us but it wasn't working correctly so I just read up on vectors tonight so I hadn't had a full understanding on how they worked. A bunch of trial and error. So I also had to make location -2 instead of -1. Thank you so much. – Cush Oct 22 '14 at 07:52