isralruval 0 Newbie Poster

i have the following function which works, its encrypts a text file, well its suppose to shift the letters of a text file by whatever the user wants, lets say the text file says "zoo" if the user wants to shift the letters by 1 to the right the output of it should say "app"
but lets say i want to shift it 13 to the right, it wont give the correct results
instead of giving me "mbb" it gives me something random it outputs "\207bb"
anyone wants to help me fix it??

void rotateEncrypt(vector<string> V1, vector<string> V2, int rotKey)
{
char ch;
int k;
string word = V1[0];
for (unsigned i=0; i<word.size();i++)
{
if(word[i]!=' '){
ch = tolower( word[i]);
ch = ch + rotKey;
if(ch > 'z')ch = 'a' + (ch - 'z' -1);
word[i] = ch;
}
}
V2.push_back(word);
ofstream fo("simpler.txt" ,ios::app);
fo<< word <<'\n';
fo.clear();
fo.close();
}

Dani AI

Generated

A short diagnosis and practical fixes for the behavior seen in 's post.

The root cause is performing the rotation by adding rotKey directly to a char. That can produce values outside the ASCII range; assigning those back into a (signed) char yields implementation-defined results and nonprintable bytes. Also, C character classification functions expect values cast to unsigned char (see std::tolower docs). Correct handling uses integer arithmetic on a letter-offset and a modulo 26 wrap, plus safe casts for isalpha/isupper.

Recommended fixes:

  • Reduce the shift: rotKey %= 26; if (rotKey < 0) rotKey += 26;
  • Work with integer offsets from 'a'/'A' and apply (offset + rotKey) % 26.
  • Use unsigned char (or cast to unsigned char) when calling std::isalpha/std::isupper and when doing arithmetic to avoid sign-extension issues.
  • Change the function API so output vectors are passed by reference (avoid copying).
  • Open the output file once (outside per-string loops) unless appending each call is intentional.

Example implementation pattern:

void rotateEncrypt(const std::vector<std::string>& in,
                   std::vector<std::string>& out,
                   int rotKey)
{
    rotKey %= 26;
    if (rotKey < 0) rotKey += 26;
    for (const auto& s : in) {
        std::string t; t.reserve(s.size());
        for (unsigned char uc : s) {
            if (std::isalpha(uc)) {
                unsigned char base = std::isupper(uc) ? 'A' : 'a';
                t.push_back(static_cast<char>((uc - base + rotKey) % 26 + base));
            } else {
                t.push_back(static_cast<char>(uc));
            }
        }
        out.push_back(std::move(t));
    }
}

Also avoid calling character functions with plain char if it can be negative; prefer static_cast<unsigned char>(c) before std::isalpha/std::tolower. This prevents the garbage characters seen when large shifts cross the signed char boundary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.