Caesar-Verschlüsslung
-
Hey ich wollte eine Caesar-Verschlüsslung in C++ schreiben doch ich bekomme ganze zeit die Fehlermeldung "String subscript out of Range" was es heißt weiß ich doch wie löse ich das Problem?
#include <iostream> #include <string> using namespace std; string crypt(string text,int chiffre) { for(unsigned int i = 0; i <= text.length(); i++) { text[i] = text[i] + chiffre; } return text; } string decrypt(string text,int chiffre) { for(unsigned int i = 0; i <= text.length(); i++) { text[i] = text[i] - chiffre; } return text; } int main() { string textc = ""; string textd = ""; char input = '0'; int chiffre = 0; bool exit = false; do{ cout << "Caesar-Chiffre" << endl << endl; cout << "<1> Crypt" << endl; cout << "<2> Decrypt" << endl; cout << "<3> Exit" << endl; cout << endl << "Input : "; cin >> input; switch(input) { case '1' : cout << endl << "Text : "; cin >> textc; cout << endl << "Chiffre : "; cin >> chiffre; cout << endl << endl << crypt(textc,chiffre) << endl; fflush(stdin); getchar(); break; case '2' : cout << endl << "Text : "; cin >> textd; cout << endl << "Chiffre : "; cin >> chiffre; cout << endl << endl << decrypt(textd,chiffre) << endl; fflush(stdin); getchar(); break; case '3' : exit = true; break; default : cout << endl << "Fuck off choose one option from the list!" << endl << endl; break; } }while(exit == false); return 0; }Danke im vorraus!
-
Das Problem liegt hier:
for(unsigned int i = 0; i <= text.length(); i++) {Gültige Indizes sind nur 0 bis text.length()-1
Eine echte Caesar-Verschlüsselung ist es auch nicht, weil es passieren kann, dass bei Deinem Code keine gültigen Buchstaben rauskommen.
-
Chrome schrieb:
for(unsigned int i = 0; i <= text.length(); i++) for(unsigned int i = 0; i <= text.length(); i++)text.length()+1 ist größer als text.length(). Ergo, was musst du tun?
