Hello,
I am trying to learn the C++ standard library and I am having trouble with this program.
In the indicated for loop, if I change the middle part to i >= 0, then I get an error for being out of range. If i doesn't go to 0 then it doesn't take the last character. Why do I get an out of range error for going to 0? Any suggestions?Code://cin with strings #include <iostream> #include <string> #include <vector> #include <cstdio> using namespace std; vector<string> reverse(const string& s) { vector<string> return_s; typedef string::size_type string_size; string_size i = 0; for(i = s.size() - 1; i > 0; i--)//Here is the problem return_s.push_back(s.substr(i, 1)); return return_s; } int main () { string s; vector<string> v; cout << "Enter a string: " << endl; while(getline(cin, s)) v = reverse(s); cout << "Your string in reverse: " << endl; for(vector<string>::size_type i = 0; i < v.size(); i++) cout << v[i]; cout << endl; return 0; }