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;
ofstream fo("simpler.txt" ,ios::app);
fo<< word <<'\n';
8 Years
Discussion Span
Last Post by NathanOliver

That's easy you are saying your program to go 13 to the right but you're missing the point where it pass through 'z' and needs to restart back to 'a' you just have to tell the program that.

So ascii code of 'z' is 122, so you must tell your program when it gets to that he should go back to 'a' which ascii code is 97. Same goes for capital letters just different ascii codes :).

See Example Below

#include <iostream>

using namespace std;

int main()
	char keepAscii;

	for(int x=97; x<=122; x++)
		keepAscii = x;
		cout << keepAscii;

		if(x == 122)
			x = 96;
			cout << endl;

	return 0;

This will be continuously printing the letters from 'a' to z'.

Hope it helped.

Edited by Jiwe: n/a


i striped down your function just to walk it through my debugger and i had to change char ch; to unsigned char ch; in order to get it to work. what was happening was it was using a signed char which has a max value of 128 or DEL. so it was rolling over into the negatives and messing up the end result. i would give it a try and see what happens

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.