I am making program that reverses a string and it has to use recursion.
#include <iostream>
#include <string>
void flipper(std::string str) {
int i;
if (i == (str.size() - 1))
std::cout << str;
else {
str += str[0];
str.erase(str[0]);
i++;
std::cout << str;
flipper(str);
}
}
int main() {
std::string str;
std::cout << "What is your string?\n";
std::cin >> str;
flipper(str);
}
Whenever I run the code, even if I take out the recursion part and just run
str += str[0];
str.erase(str[0]);
std::cout << str;
It still returns:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string.
Aucun commentaire:
Enregistrer un commentaire