Hi everyone, I'm writing a project for college that requires us to translate an input file (read directly using the < unix symbol, not in the program itself) char-by-char using a translation table. I pretty much have it all done, but I'm running into a rather strange problem. Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream keyFile("key.txt");
string key;
getline(keyFile,key);
string curLine;
while(getline(cin, curLine))
{
int lineLength = curLine.length();
char decrypted[lineLength];
for( int i=0; i < lineLength; i++)
{
char current = curLine.at(i);
if(!((current>=97 && current <=122) || (current>=65 && current <=90)))
{
decrypted[i] = current;
continue;
}
bool lowercase = current>=97 && current <=122;
if(lowercase) current-=32;
char real = key.find_first_of(current, 0)+65;
if(lowercase) real+= 32;
decrypted[i] = real;
}
cout << decrypted << endl;
}
}
|
Everything works, and to test everything else I'm just using the alphabet for the key. However, with an input file containing:
Lorem ipsum
dolor
Sit Amet |
I get the following printed out:
_orem ipsum
dolor
Sit Amet |
(It's not actually a _ character, but rather a space).
For some reason, it seems to be replacing the very first character with a space or some other "blank" character. I thought maybe it's a problem in the decrypted[] array, but I printed
decrypted[0] and it printed the missing L just fine. So if there's nothing wrong with the array itself, why is it not printing with the cout command?