#include <iostream>
#include <string>
int main(){
char p;
bool show_cleartext;
std::string input;
std::cout << "Would you like the output to show your original text? [y/n] \n";
while(true){
std::cin >> input;
if(input == "y"){show_cleartext = true; break;}
else if(input == "n"){show_cleartext = false; break;}
else std::cout << "[y/n] ? \n";
}
while(true){
// Promts for 'input' then displays input, reversed
std::getline(std::cin,input);
if(input == "quit")break;
for(int i = 0; i < (input.length()-1)/2; ++i){
p = input[input.length()-i-1];
input[input.length()-i-1] = input[i];
input[i] = p;
}
std::cout << input+'\n';
}
return 0;
}
Jede While-Schleife ist in Ordnung. Jedoch schließt das Einschließen von beiden das Programm ab, sobald es über das erste hinausgeht, d. H. Durch Eingabe von "y" oder "n".Warum verursachen zwei while() - Schleifen einen Absturz?
Meine Vermutung: In der zweiten Schleife ist 'input' eine leere Zeichenfolge,' input.length() 'ist null,' input.length() - 1' wird zu einer sehr großen Zahl umgebrochen, und Sie greifen darauf zu Index außerhalb der Grenzen. –
Ich dachte, du wärst richtig, aber initialisiere Eingabe als "asdasd" oder etwas hilft nicht. EDIT: Die Änderung zwischen den While-Schleifen macht nichts – basket
[Works für mich] (http://rexttester.com/ESZT28102) –