Search code examples
c++caesar-cipher

Undefined identifier/ undeclared


I recently started C++ and I wanted to create a simple ceaser cipher function that could be called on, however because I copied the coding structure form my python program I seem to be getting 4 errors and 2 warnings. The mode bit is a bool where true is to encrypt and false is to decrypt (it worked on python so hey) :).

First one on the line of me creating the function where "int" is, its saying "identifier "in" undefined"

Second one in the same line saying "expected a ')'"

Third one is after the 3 if statements, saying "identifier "CharPos" undefined" even though it is defined

And Forth on the same line saying "'CharPos': undeclared identifier"

#include <iostream>
#include <fstream>
#include <string>

std::string Encryption(std::string Password, int Key, bool Mode) {
    std::string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
    std::string EncryptPass = "";
    if (Key > 36) {
        Key = Key % 36;
    }
    for (int X = 0; X < Password.length(); X++) {
        if (Password.at(X) == ' ') {
            EncryptPass = EncryptPass + " ";
        }
        else {
            for (int Y = 0; Y < 36; Y++) {
                if (Password.at(X) == Alphabet.at(Y)) {
                    if (Mode == true) {
                        int CharPos = Y + Key;
                        if (CharPos > 35) {
                            CharPos = CharPos - 36;
                        }
                    }
                    if (Mode == false) {
                        int CharPos = Y - Key;
                        if (CharPos < 0) {
                            CharPos = CharPos + 36;
                        }
                    }
                    if (Mode != true and Mode != false) {
                        int CharPos = 0;
                    }
                    char CharPos2 = CharPos;
                    char EncryptChar = Alphabet.at(CharPos2);
                    EncryptPass = EncryptPass + EncryptChar;
                }
            }
        }
    }
    return EncryptPass;
}

Any help would be appreciated


Solution

  • As noted above, the major issue you have in your code is that CharPos is redefined in each if clause.

    Every place you put int CharPos = ..., you create a new variable, and even though its name is similar for you, for the compiler, there are three unique variables of this name. In order to use the same variable in all of the scopes - the environment of variables, you should define it once in the first common scope of all, that means, in the for loop before of the if - else clauses.

    Also, as noted above Mode != true || Mode != false is equivalent to true!

    I rewrote your code to be safe, achieve the wanted encryption, and more readable (IMO).

    
    std::string encrypt(const std::string& word, std::size_t shift_amount, bool should_shift_up)
    {
        static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
    
        assert (shift_amount <= alphabet.size()); // makes no sense having shift > alphabet size!
    
        std::string encrypted_word(word.size(), '\0');
    
        for (std::size_t idx = 0; idx < word.size(); ++idx)
        {
            char original_char = word[idx];
            std::size_t pos_in_alphabet = alphabet.find(original_char);
    
            std::size_t shifted_char = 'a';
            if (should_shift_up)
            {
                shifted_char = (pos_in_alphabet + shift_amount) % alphabet.size();
            }
            else
            {
                shifted_char = (pos_in_alphabet > shift_amount)  
                             ? pos_in_alphabet - shift_amount
                             : alphabet.size() - shift_amount + pos_in_alphabet;
            }
    
            encrypted_word[idx] = alphabet[shifted_char];
        }
    
        return encrypted_word;
    }