title: iostream reads a couple of lines correctly then fails on subsequent lines
body: <p>I am working on creating a program for class that both encrypts and decrypts a simple text file. Before setting up iostreams, I was able to get the program to work with simple user input (cin/cout). I struggled to get iostreams working and finally did figure it out - mostly. Where I am having trouble is when my test file has multiple lines. It reads and translates the first two lines fine, but after the second line, all i get is jibberish. I am thinking that it may have to do with some sort of quirk with the carriage return &amp; Linefeed at the end of the line, but then am confused why it works from line one to two but not on line two to three and beyond. My code is below. My best guess at this moment is that the problem exists in the looping starting at line 123. But I haven't been able to isolate just yet. </p>
  
  <p>If anyone does have a few minutes to spare to look this over, I very much appreciate your time! Please, though, try to only point me in the right direction without suggesting too many changes to the rest of my code. This is Homework and needs to be my work as much as possible. I will be showing that I received help to get this to work.</p>
  
  <p>If you want to test this, create a file in the directory this solution runs from called C++Test.txt and put some test text in it to read. Again, It seems to work just fine for lines 1 and two, but fails after line two. </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;iomanip&gt;
  #include&lt;cstring&gt;
  #include&lt;fstream&gt;
  using namespace std;
  inline void keep_window_open() { char ch; cin&gt;&gt;ch; }
  
  
  char cipher(char, int, char, int);
  char calculation(int);
  void encrypt();
  void decrypt();
  void displayMenu(void);
  void getMenuSelection(int);
  
  int main (void)
  {
  bool running=true;
  char answer=' ';
  
  cout&lt;&lt;
      "Use this program to encrypt/decrypt text.\nPlease select an option\n";
  while (running==true)                   //allows program to loop until user
  {                                       //user selects to exit
      displayMenu();                      //function call
      cout&lt;&lt;
          "\nWould you like to start over?\n(enter Y for Yes or N for No)\n";
      cin&gt;&gt;answer;
      if (answer=='Y'||answer=='y')
      {
          running=true;
      }
      else
      {
          running=false;
      }
  }
  cout&lt;&lt;"\nGoodbye.";
  
  
  
  //The next two lines stop the Command Prompt window from closing
  //until the user presses a key, including Enter.
  cout    &lt;&lt; "\nPress enter twice to exit." &lt;&lt; endl;
  cin.ignore(2);
  }
  ///////////////////////////////Width Guage//////////////////////////////////////
  ///////////////////////////////displayMenu//////////////////////////////////////
  //This function simply displays the menu and calls getMenuSelection()
  void displayMenu(void)
  {
  int userInput=0;
  cout&lt;&lt; "\nPlease select an option:\n(Enter 1, 2, or 3)\n" ;
  cout&lt;&lt; "1. Encrypt\n" ;
  cout&lt;&lt; "2. Decrypt\n" ;
  cout&lt;&lt; "3. Exit\n";
  cin&gt;&gt;userInput;
  if (userInput==1||userInput==2)
  {
      getMenuSelection(userInput);            //function call
  
  }
  else
  {
  
  }
  
  
  }
  ///////////////////////////////Width Guage//////////////////////////////////////
  ///////////////////////////////getMenuSelection/////////////////////////////////
  void getMenuSelection(int userInput)
  {
     switch (userInput)
     {
      case 1:
            {
                   encrypt();
                    break;
            }
      case 2:
            {
                   decrypt();
                    break;
            }
     }
  }
  ///////////////////////////////Width Guage//////////////////////////////////////
  ///////////////////////////////encrypt//////////////////////////////////////////
  void encrypt()
  {
  char key[128]={' '}, mess[256]={' '};
  string newMess;
  int keyLength=0, messLength=0, switchChar=0, i=0;
  cout&lt;&lt;"What is the encryption key?\n(128 characters or less only)";
  cin&gt;&gt;key;
  keyLength=strlen(key);
  cin.ignore(300,'\n');
  cout&lt;&lt;"Getting message file C++Test.txt\n";
  ifstream message;                   //Opens input stream to get C++Test.txt
  message.open("C++Test.txt", ifstream::in);
  int j=0;                            //loop counter
  do                                  //loop to input from file
  {
      mess[j]=message.get();
      j++;
  }
      while (message.good());
  message.close();                    //closes input stream
  
  
  messLength=strlen(mess);
  int k=0;
  for (int counter=0;counter&lt;messLength;counter++)
  {
      if (isalpha(mess[counter]))
      {
          if (i&gt;keyLength)
          {
              i=0;
          }
          else if (i==keyLength-1)
          {
              newMess.push_back(cipher(mess[counter],keyLength,key[i],
                  switchChar));
              i=0;
          }
          else
          {
              newMess.push_back(cipher(mess[counter],keyLength,key[i],
                  switchChar));
              i++;
          }
      }
      else
      {
          newMess.push_back(mess[counter]);
          i++;
      }
  
  
  
  }
  filebuf newMessagefb;
      newMessagefb.open("C++TestDecrypt.txt", ios::out);
      ostream newMessage (&amp;newMessagefb);
      newMessage&lt;&lt;newMess;
      newMessagefb.close();
      k++;
  cout&lt;&lt;"The encrypted message is:\n\n"&lt;&lt;newMess;
  
  }
  ///////////////////////////////Width Guage//////////////////////////////////////
  ///////////////////////////////cipher///////////////////////////////////////////
  char cipher (char origMessChar, int keyLength, char origKeyChar, int switchChar)
  {
  origMessChar=tolower(origMessChar);
  origKeyChar=tolower(origKeyChar);
  switch (switchChar)
  {
  case 0://for encryption
      {
          int messNum=origMessChar-'a';
          int keyNum=origKeyChar-'a';
          char newMessChar=((messNum+keyNum)%26)+'a';
          return (newMessChar);
      }
  case 1://for decryption
      {
          int messNum=origMessChar-'a';
          int keyNum=26-(origKeyChar-'a');
          char newMessChar=((messNum+keyNum)%26)+'a';
          return (newMessChar);
      }
  }
  }
  ///////////////////////////////Width Guage//////////////////////////////////////
  ///////////////////////////////decrypt//////////////////////////////////////////
  void decrypt()
  {
  char key[128]={' '}, mess[256]={' '};
  string newMess;
  int keyLength=0, messLength=0, switchChar=1, i=0;
  cout&lt;&lt;"What is the encryption key?\n(128 characters or less only)";
  cin&gt;&gt;key;
  keyLength=strlen(key);
  cin.ignore(300,'\n');
  cout&lt;&lt;"Getting message file C++TestDecrypt.txt\n\n";
  ifstream message;                   //Opens input stream to get C++Test.txt
  message.open("C++TestDecrypt.txt", ifstream::in);
  int j=0;                            //loop counter
  do                                  //loop to input from file
  {
      mess[j]=message.get();
      j++;
  }
      while (message.good());
  message.close();                    //closes input stream
  messLength=strlen(mess);
  for (int counter=0;counter&lt;messLength;counter++)
  {
      if (isalpha(mess[counter]))
      {
          if (i&gt;keyLength)
          {
              i=0;
          }
          else if (i==keyLength-1)
          {
              newMess.push_back(cipher(mess[counter],keyLength,key[i],
                  switchChar));
              i=0;
          }
          else
          {
              newMess.push_back(cipher(mess[counter],keyLength,key[i],
                  switchChar));
              i++;
          }
      }
      else
      {
          newMess.push_back(mess[counter]);
          i++;
      }
  }
  cout&lt;&lt;"The encrypted message is:\n\n"&lt;&lt;newMess;
  }     
  </code></pre>