Error implementing Caesar Cipher Using C++ -


i trying implement caesar cipher using c++. directions use file encrypted:

5 asi ymj rtrjwfymjx tzylwfgj. aqq rnrxd bjwj ymj gtwtlwtajx dni ldwj fsi ldrgqj ns ymj bfgj. tbfx gwnqqnl fsi ymj xnymjd ytajx 

the number 5 represents shift applied text. have decode caesar ciphered text , reverse lines in put line 4 in line 1's position , line 3 in line 2's. first letter of each line not need decoded (the uppercase letters).

the text should after running program:

twas brillig , sithey toves did gyre , gymble in wabe. mimsy borogroves , momerathes outgrabe. 

as of right now, have code:

#include <iostream> #include <vector> #include <string> #include <fstream>  using namespace std;  char decipher (char c, int shift);  int main(){     //declare variables     char c;     string deciphered = "";     int shift;     vector <string> lines;      //ask filename , if not found, keep trying     ifstream infile;     string filename;     cout << "what name of file? ";     cin >> filename;     infile.open(filename);     while (!infile){         cout << "file not found. try again: ";         cin >> filename;         infile.open(filename);     }      //find shift file     infile >> shift;      //get lines file     infile >> noskipws;     while (infile >> c){         char decipheredchar = decipher (c, shift);         deciphered += decipheredchar;     }     cout << deciphered;  }  char decipher (char c, int shift){     string letters = "abcdefghijklmnopqrstuvwxyz";     if (c == 't'){         return c;     }     else if (c == 'd'){         return c;     }     else if (c == 'a'){         return c;     }     else if (c == ' '){         return c;     }     else {         int currentposition = letters.find(c);         int shiftedposition = currentposition - shift;         if (shiftedposition < 0){             shiftedposition = 26 + shiftedposition;         }         char shifted = letters[shiftedposition];         return shifted;     } } 

the result i'm getting this:

uand momerathes outgrabeuuall mimsy borogrovesudid gyre , gymble in wabeuutwas brillig , sithey tovesu 

how rid of u's , separate words line? have idea of reversing lines using vector , using loop counting backwards i'm not sure how there yet. please help. thank you.

to answer question, 'u's newlines. read them in , decipher them, change , result pulled letters. should able add case decipher() leave newlines alone:

char decipher (char c, int shift){     string letters = "abcdefghijklmnopqrstuvwxyz";     if(c == '\n'){  // not modify new lines.         return c;     }     else if (c == 't'){         return c;     }     // ... } 

probably cleanest way reverse lines parse them while read characters. can them pop them vector in reverse order. working (but not robust) example add following while loop:

while (infile >> c){     char decipheredchar = decipher (c, shift);     deciphered += decipheredchar;     if(decipheredchar=='\n'){        //if full line         lines.push_back(deciphered); //push line         deciphered = "";             //start fresh next line     } } lines.push_back(deciphered+'\n'); //push final line (if no newline)  while(!lines.empty()){     cout << lines.back(); //prints last line     lines.pop_back();     //removes last line } 

i not robust because there minor things may still need watch out for. instance, reads stores newline after 5, , if file ends in newline i've added empty 1 on end... i'll leave minor details clear up.


Comments