samedi 29 juillet 2017

Why I am getting Compile Error?

I am coding for a contest and I got compile error for this code but when I use some other ide I got correct output.I am using C++ language for my code. I used http://ift.tt/2vg9XkT IDE. Please help me. code

#include <iostream>
#include <algorithm>
std::string decryptRailFence(std::string cipher, int key)
{
    char rail[key][cipher.length()];

    for (int i=0; i < key; i++)
        for (int j=0; j < cipher.length(); j++)
            rail[i][j] = '\n';
    bool dir_down;

    int row = 0, col = 0;

    for (int i=0; i < cipher.length(); i++)
    {
        // check the direction of flow
        if (row == 0)
            dir_down = true;
        if (row == key-1)
            dir_down = false;

        // place the marker
        rail[row][col++] = '*';
        dir_down?row++ : row--;
    }
    int index = 0;
    for (int i=0; i<key; i++)
        for (int j=0; j<cipher.length(); j++)
            if (rail[i][j] == '*' && index<cipher.length())
                rail[i][j] = cipher[index++];

    std::string result;

    row = 0, col = 0;
    for (int i=0; i< cipher.length(); i++)
    {
        // check the direction of flow
        if (row == 0)
            dir_down = true;
        if (row == key-1)
            dir_down = false;

        // place the marker
        if (rail[row][col] != '*')
            result.push_back(rail[row][col++]);

        // find the next row using direction flag
        dir_down?row++: row--;
    }
    return result;
}

//driver program to check the above functions
int main()
{
    int key;
    char c = 'X';

    std::string str1;
    //Now decryption of the same cipher-text
    std::cin>>key;
    std::cin>>str1;
    std::string s= decryptRailFence(str1,key);

    char endch = s.back();

    if(endch == c)
    {
        s.erase(std::remove(s.begin(), s.end(), c), s.end());
    }
    std::cout<<s<<std::endl;
    return 0;
}

Input

5

WTXHUTXAOHXTBIXAS

Output

WHATABOUTTHIS

While using their compiler i get compile time error

Aucun commentaire:

Enregistrer un commentaire