First character not being printed?

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?
UPDATE

I tried a different input file to see if I would get the same problem, and I actually got something much stranger. The input was:

1
2
3
4
5
THIS
IS
MY
TEST
FILE

The output was:

1
2
3
4
5
 HIS
u
u
 EST
FILE

What's even more concerning is that after re-compiling and testing again with the same input file, I got this:

1
2
3
4
5
 HIS
Þ
Þ
 EST
FILE

What could be causing this?
You never initalized decrypted.
Did I not on line 15?
char decrypted[lineLength];
char decrypted[lineLength];


Above only say you reserve space for variable decrypted. But the contents inside each space is not initialized yet.
What compiler do you use? line 15 is normally not allowed in C++.
I'm... not sure, I'm using PuTTY to connect to my university's unix environment, but the compiler executable is called c++. How should this be initialized?
You can use the easy way memset(decrypted,0,lineLength) C string function.
My compiler (visual C++) complains about line 15. It's supposed to be char *decrypted = new char[lineLength]; (don't forget to delete it when you're done with it)

to initialize it with 0: memset(decrypted, 0, lineLength * sizeof(char)); but that's not supposed to be your problem since (as far as i see) you initialize it in your loop

line 30 is a problem if you don't have a 0 at the end of your string
1
2
3
string curLine = "Hello World!\n";
int lineLength = curLine.length();
char decrypted[lineLength];


Above compile fine for me using g++ version 3.4.6 20060404 (Red Hat 3.4.6-10)
Thanks for the help so far guys, but I'm still having issues. I tried using memset but I don't think it can find where it's defined. I even included stdio.

I did, however, take coder777's advice and made the char *decrypted = new char[lineLength]; change and I delete it after every loop, but the problem still remains. Another test sample:

Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ABCDEF GHIJKLMNO PQRSTUVW XYZ .,;' 0192837465
THfdf sfiwf HDHDFD

abcdefghijklmnopqrstuvwxyz

t

g
g
y
D
EE
C
T
t

.,
!~ 

Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
0BCDEF GHIJKLMNO PQRSTUVW XYZ .,;' 0192837465
0Hfdf sfiwf HDHDFD
0
DEF G0ghijklmnopqrstuvwxyz
0
0
0
0
0
0
0
0E
0
0
0
0
0,
!~ 

I'll have to work on it more tomorrow.
Last edited on
memset is in string.h.
In C++ that's <cstring>.
Topic archived. No new replies allowed.