weird symbols popping up when executing code

This question is asked already, but I still am not able to fix it. Its an simple excercise from my studybook to copy an array into a vector. I read in the studybook that C-strings always ends with a zero character. According to my study book you can just solve this by adding -1 to the member function .end() when using it in a For loop. However when I execute it, I still get weird symbols showing up at the last element of the vector and the execute window will also crash. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string addTogether (const vector <string> & v);


int main()
{

string arr [] = {"monkey", "lion", "cow"};
vector <string> vs (arr, arr+3);
cout << addTogether(vs);

}

string addTogether (const vector <string> & v){
vector<string> :: const_iterator pos, einde = v.end();

for (pos = v.begin(); pos != einde-1; ++pos){
cout << *pos << '-';
}
}
You are not using C-strings but std::strings, so there is no need to add -1.
What weird symbols do you get?
When I run the code you posted the output is:
monkey-lion-
To get all 3 names you can do it like this:
1
2
3
4
5
6
  
vector<string> ::const_iterator pos, einde = v.end();
for (pos = v.begin(); pos != einde; ++pos) 
{
    cout << *pos << '-';
}
Even if I remove the -1, I still get the weird symbols. Too bad I can't copy-past in the execute window but it looks roughly like this:
Monkey-lion- |É É É É É ■  É ■ É É É (the rest of the symbols are to difficult to find)

Perhaps its my compiler, because the correct code provided in my book also has the same problem. (and its also using the end()-1. I am using Code :: Blocks v13.12
The problem is that you do not return a string in addTogether(...).
It shouldn't compile but unfortunately it does.
What am I doing wrong?

You're not returning the string from your function like you promised, so the the cout statement in main is barfing.

Facepalm moment :/
Books code are fine, I edited the orginal code to not return a value, while forgetting to change the function into void. Thanks for all the advice.
Topic archived. No new replies allowed.