954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ encryption text file help

i have the following code which works, well its suppose to shift the letters of a text file by whatever the user wants, lets say the text file says "hi" if the user wants to shift the letters by 1 to the right it will become "ij"
ok but the problem is when i want to shift a letter that goes over the alphabet, like lets say the word is "zoo" if i want to shift it by one the letter z will give me a random character that i dont want, the whole word should become "app"
anybody wants to help me fix this??

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;
          word[i] = ch;
          if(ch > 'z');
          k = ch -'z';
          ch = 'a'+k - 1;
          word[i] = ch;
        }
    }
  V2.push_back(word);
  ofstream fo("simpler.txt" ,ios::app);
  fo<< word <<'\n';
  fo.clear();
  fo.close();
}
isralruval
Newbie Poster
14 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
$ g++ -W -Wall -O2 -c foo.cpp
foo.cpp: In function ‘void rotateEncrypt(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, int)’:
foo.cpp:21: warning: suggest braces around <strong>empty body</strong> in an ‘if’ statement


Of course, you could have simply stepped this through the debugger and seen that your if statement was useless.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You