0
#include<iostream>
#include<string>
using namespace std;
string generateKey(string str, string key)
{
    int x = str.size();
    for(int i=0; ;i++)
    {
        if(x==i)
            i=0;
        if(key.size() == str.size())
            break;
        key.push_back(key[i]);
    }
    return key;
}
string cipherText(string str, string key)
{
    string cipher_text;
    for(int i=0; i<str.size(); i++)
    {
        int x=(str[i]+key[i])%26;
        x+='A';
        cipher_text.push_back(x);
    }
    return cipher_text;
}
string originalText(string cipher_text, string key)
{
    string orig_text;
    for(int i=0;i<cipher_text.size(); i++)
    {
        int x=(cipher_text[i]-key[i] +26)%26;
        x +='A';
        orig_text.push_back(x);
    }
    return orig_text;
}
int main()
{
    int x;
    cout<<"Enter your choice"<<endl;
    cout<<"1. Mono-alphabetic\n2. Poly-alphabetic\n";
    cin>>x;
    switch(x)
    {
    case 1:
        cout<<"Mono-alphabetic cipher"<<endl;
        cout<<"Enter your choice \n 1.Encryption\n 2.Decryption\n";
        int b;
        cin>>b;
        switch(b)
        {
        case 1:
            int k,i;
            char plainText[100];
            cout<<"Encryption\n";
            cout<<"Enter Plain text"<<endl;
            cin>>plainText;
            cout<<"Enter Encryption key"<<endl;
            cin>>k;
            for(i=0;(i<100 && plainText[i]!='\0');i++)
            plainText[i]=plainText[i]+k;
            cout<<"\n Encrypted text:"<<plainText<<endl;
            break;
        case 2:
            int d;
            char encrypedText[100];
            cout<<"Decryption\n";
            cout<<"Enter Encrypted text"<<endl;
            cin>>encrypedText;
            cout<<"Enter Decryption key"<<endl;
            cin>>d;
            for(i=0;(i<100 && encrypedText[i]!='\0');i++)
            encrypedText[i]=encrypedText[i]-d;
            cout<<"\n Decrypted text:"<<encrypedText<<endl;
            break;
        default:
            cout<<"WRONG Choice Entered!!!\n";
            break;
        }
    case 2:
        {
    cout<<"Poly-alphabetic"<<endl;
    string str ="PLANTTHATBOMBTHERE";
    string keyword="AMAZON";
    string key =generateKey(str,key);
    string cipher_text = cipherText(str,key);
    cout<<"Ciphertext:"<<cipher_text<<endl;
    cout<<"Original text:"<<originalText(cipher_text, key);
    break;
        break;
        }
    }

}

This code is for substitution cipher and the poly-alphabetic cipher part is not running which is showing std::logic_error error and what(): basic_string::_M_construct null not valid error please help the first part is running ok without any error but the polyalphabetic cipher part does not show any compilation error it only shows run time error

  • 3
    In a function declared to return a `std::string` object, what do you think happens when you `return 0;` (remembering that `0` is the null pointer)? – Some programmer dude Jan 28 '20 at 18:18
  • 4
    On another couple of (unrelated) notes: Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jan 28 '20 at 18:19
  • `#include using namespace std;` - No. *Never* do that. I stopped reading your question at that point of idiocy (possibly ignorance). – Jesper Juhl Jan 28 '20 at 18:19
  • 1
    You changed your posted code twice. Did the behavior of the program change due to that or did it remain the same? – R Sahu Jan 28 '20 at 18:48
  • 1
    Run your program through a debugger and it will tell you exactly where the exception was thrown. – Kevin Jan 28 '20 at 18:50
  • Please don't change your code in response to comments, especially if it happens to be the problem. Respond to the comments by replying to them (use the `@` followed by the posters nick) and tell them about it. Then they can write a proper answer instead. If you fix the problem in the code the question becomes worthless and it no longer have any problem. Remember that this site isn't only to help you right now, but also future people who might have the same or similar problem. – Some programmer dude Jan 28 '20 at 18:52
  • @RSahu it did not change – Achal Chauhan Jan 28 '20 at 18:52
  • @Kevin I debugged it, its showing problem with string key =generateKey(str,key); , it says program received signal SIGSEGV, segme in std::basib_strin or >::basic_string(std::string c) and i'm not able to understand this can please explain – Achal Chauhan Jan 28 '20 at 19:07
  • generateKey second parameter is key or keyword? It might be keyword. – Build Succeeded Jan 28 '20 at 19:09
  • @Mannoj its key – Achal Chauhan Jan 28 '20 at 19:11
  • What the use of keyword=AMAZON? – Build Succeeded Jan 28 '20 at 19:13
  • @Mannoj Oh! yes second parameter is keyword, Now its running perfectly thanks. – Achal Chauhan Jan 28 '20 at 19:17

1 Answers1

0
string keyword="AMAZON";
string key =generateKey(str,keyword);
string cipher_text = cipherText(str,key);
cout<<"Ciphertext:"<<cipher_text<<endl;

You are using the key variable as parameter to function before it get memory. Try above code.

Build Succeeded
  • 1,097
  • 1
  • 7
  • 21