I am making a cypher code and when I am trying to execute it I get the "std::logic_error' what(): basic_string::_M_construct null not valid". what is wrong with the code.
#include <iostream>
using namespace std;
string cipher(int key,string text);
string decipher(int key,string text);
int main(int argc, char** argv) {
string type;
string text;
string dtext;
char* key1;
int key;
string s = argv[1];
if (argc != 2) {
cout << "Usage ./ceaser key" << endl;
return 1;
}
else {
for (int k = 0;k < s.length(); k++) {
if (isalpha(argv[1][k]))
{
cout << "Usage: ./caesar key" << endl;
return 1;
}
else
{
continue;
}
}
}
cout << "Type c for cipher and d for decipher: ";
cin >> type;
cout << "text: ";
getline(cin, text);
key1 = argv[1];
key = atoi(key1);
if (type == "c") {
cipher(key,text);
}
else {
decipher(key,text);
}
cout << endl;
}
string cipher(int key,string text) {
for (int i = 0; i < text.length(); i++) {
if (islower(text[i])) {
char m = (((text[i] + key) - 97) % 26) + 97;
cout << m;
}
else if(isupper(text[i])){
char a = (((text[i] + key) - 65) % 26) + 65;
cout << a;
}
else {
cout << text[i];
}
}
return 0;
}
string decipher(int key,string text) {
for (int i = 0; i < text.length(); i++) {
if (islower(text[i])) {
char m = (((text[i] - key) - 97) % 26) + 97;
cout << m;
}
else if(isupper(text[i])){
char a = (((text[i] - key) - 65) % 26) + 65;
cout << a;
}
else {
cout << text[i];
}
}
return 0;
}
How can I fix this, it worked fine when I only made it a cypher but not decipher. but when I try to decipher it stopped working.