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.