I am new to C++. I am having trouble outputting data to a file. I am using an iterator to print out a map. The print method takes i, a key value, and prints out it’s corresponding vector. Now, this works perfectly fine when I output normally using cout<< but when I try to put the same output into a file, my program crashes. I know it is the *it that in the outfile<< line that is crashing it, because if I replace it with some random string it outputs it to the file fine. Also, I know the parameters in the print method are not causing any problems, because I can transfer that method directly to the main function of the program and get the same error. Any help would be greatly appreciated on how to fix this, thank you!
Here is my print method where the error is happening:
public: void print(int i, vector<string> in, ostream& outfile) // print method for printing a vector and it's key
{
sort(in.begin(), in.end()); // sort the vector alphabetically first
vector<string>::iterator it;
it= unique(in.begin(), in.end()); // makes sure there are no duplicate strings
in.resize( distance(in.begin(),it) );
for( it = in.begin(); it != in.end(); it++ ) // iterate through it
cout << i << ": "<< *it<<endl; // and print out the key value and each string in the vector
// outfile<< i << ":" << *it<< endl; // prints to file
}
Are you using the
coutline at the same time? If so, I think I know what it is.The
forloop, without braces will execute the next statement as its loop body. If you use both thecoutline and theoutfileline, you will print everything, then after the loop,itwill be located just past the end of the array. You then try and dereference this and write it to a file, which of course fails since you’re dereferencing an invalid iterator.Short answer, wrap the statements in your for loop with braces.
For example, you have the following (when indented properly):
On the last line of that,
it = in.end(), wherein.end()is the element just past the end of the vector. You then try and access the element at that location which doesn’t exist (and is invalid), and hence it fails. Instead, you need to move that inside the loop, which should read