Weird issue with ascii codes

Hi guys,

so im having a weird issue when im running my cypher program when it shifts the alphabet it adds a random char to the end which im guessing is because there isn't a null terminator at the end of the cypherbet array.
http://i47.tinypic.com/152kwfd.jpg
so i was wondering if anyone could help me out and point out if that is indeed the reason why its happening and how i could add a terminator to the end of the array.

i've tried cypherBet[27] = '/0'; but apparently that corrupts the array.
so im a little stuck.

code below:
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
char* makeCypher(int shiftVal)
{
	
	char alphabet[27] = {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
							109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, '/0'};
	char cypherBet[27];
	int loopNum;
	cout << "The plain text alphabet is: \n";
	for(int g = 0; g < 26; g++)
	{
		cout << alphabet[g];
	}
	cout << endl;
	cout << "This is the Cypertext alphabet: \n";
	for(int h = 0; h < alphabet[h] && alphabet[h] != '/0'; h++)
	{
		if((alphabet[h] +shiftVal) > 122)
		{
			loopNum = ((alphabet[h]+shiftVal)-122);
			cypherBet[h] = alphabet[loopNum-1];
		}
		else
		{
			cypherBet[h] = alphabet[h] + shiftVal;
		}
		cout << cypherBet[h];
	}
	cout<<endl;
	return cypherBet;
}

thanks in advance guys.
Alan
Last edited on
You have to use the backslash when writing the null character '\0'.

What exactly is it that you are trying to test with h < alphabet[h]? I don't think it should be there.

cypherBet is a local variable and will no longer exist when the function returns. It's not a good idea to return a pointer to it because any attempt to read the array outside the function would be an error because the array does no longer exists.
ahh okay thanks, the reason h < alphabet[h] is there is because i kept getting access violation errors without it for some reason and that seemed to fix it without causing other problems.

i havn't set up the return bit properly yet i just needed it to return something whilst i finished the rest of the function


edit : i changed the direction of the slash but it still corrupts and ive changed my function so its void and doesn't return anything, but it still happens.
Last edited on
Did you change both places where you had '/0' to '\0'?
If you define alphabet like this, the compiler will add the null terminator, so that takes care of one issue.
 
    char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";


If you code the for loop like this, it takes care of the other one:
 
        for (int h=0; alphabet[h]; h++)
yeh i changed both, but its the cypherbet array thats getting corrupted when i try that
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
void makeCypher(int shiftVal)
{

	char alphabet[27] = "abcdefghijklmnopqrstuvwxyz";
	char cypherBet[27];

    const char z = 'z';

	int loopNum;
	cout << "The plain text alphabet is: \n";
	for(int g = 0; g < 26; g++)
	{
		cout << alphabet[g];
	}
	cout << endl;
	cout << "This is the Cypertext alphabet: \n";
	for (int h=0; alphabet[h]; h++)
	{
		if((alphabet[h] +shiftVal) > z)
		{
			loopNum = ((alphabet[h]+shiftVal)-z);
			cypherBet[h] = alphabet[loopNum-1];
		}
		else
		{
			cypherBet[h] = alphabet[h] + shiftVal;
		}
		cout << cypherBet[h];
	}
	cout<<endl;

}
that works perfectly thanks alot now to make sure i fully understand what went wrong before i carry on with the rest of my program
Topic archived. No new replies allowed.