char array problem

Hi,

I'm having some problems with modifying a char array.
At first, I merely define it, i.e.
char modAlpha[65];

And then after that comes a user input loop, that modifies this char array.
So what happens is that the code asks you to type whatever you want, and then from that input it produces a sort of shuffled alphabet (hence the name modAlpha -- mod as in modified). Ideally, this process should be repeatable, which is where the problem kicks in.
Now, what confuses me is that the first time, it works perfectly, while the second or third or whatever time around, modAlpha becomes a zero-length array.

In general terms, this is the code:

- Define array: char modAlpha[65];

- Loop
- Take input (works every time)
- Use that input to modify modAlpha
[repeat]

The first time this loop runs, it works, meaning that the letters are shuffled around correctly, while any run after the first reduces modAlpha to nothing.

Here's the actual code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if(chKey==1||chKeyCount==0)
{
	char midTestAlphabet[65]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUVWXYZ0123456789,. ";
	//char modAlpha[65]="\0"
	char modAlpha[65];
	if(TEST==1)
	{
	cout << "\n	  Testing: Checkpoint 1\n" << endl;
	}
	int keyCheck = inputKey(Key, FUNC, ERR);
	if(TEST==1)
	{
	cout << "Key test: " << Key << endl;
	}
	int scndKeyCheck = key(Key, midTestAlphabet, modAlpha, FUNC);
	if(TEST==1)
	{
	cout << "Testing alphabet: " << modAlpha << endl;
	}
}


And the functions here do the following:
- inputKey just takes in a user input string and checks its length. Nothing special, and works. All it does is checking length and creating a char array out of user input.
- key is where something fishy is going on. It looks like this:
[Just a quick explanation of what this function should do: it takes a normal sort of extended alphabet -- lowercase letters, uppercase letters, numbers and some symbols -- and compares this with a short user input char array. The deal is that it inserts every unique element of this extended alphabet into a new array (modAlpha), but the order depends on the input. It starts with the first character of the input, and then runs through it, and finally the rest of the available characters until modAlpha contains all the same elements as the extended alphabet, but in a different order. Sorry if that's just confusing. I guess an important note here is that it does work the first time. It's only the second and so forth that doesn't work.]
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int key(char mystring[1000],char alphabet[100], char modAlpha[100], int FUNC)
{
  const char final[]="\0";			// The end point character
  int count=1; int i;int j;int k;		// Various counters
  int length = strlen(mystring);		// Length of the input string
  int alphalength;				// Length of the to-be-modified alphabet
  int alphabetlength = strlen(alphabet);	// Length of the default alphabet
  modAlpha[0]=mystring[0];			// Start the whole thing be putting in the first character

  for(i=1;i<=length;i++)
    {
      k=0;
      alphalength = strlen(modAlpha);
      for(j=0;j<=alphalength;j++)
	{
	  if(modAlpha[j]==mystring[i])
	      k=k+1;
	}
      if(k==0)
	{
	  modAlpha[count]=mystring[i];
	  count=count+1;
	}
      else
	  count=count;
    }

for(i=0;i<=alphabetlength;i++)
    {
      k=0;
      alphalength = strlen(modAlpha);
      for(j=0;j<=alphalength;j++)
	{
	  if(modAlpha[j]==alphabet[i])
	      k=k+1;
	}
      if(k==0)
	{
	  modAlpha[count]=alphabet[i];
	  count=count+1;
	}
      else
	  count=count;
    }

 modAlpha[count]=final[0];
 modAlpha[count+1]=final[1];

return strlen(modAlpha);
}


So to just repeat the problem:
- First time the loop runs, it works, and the length of modAlpha is 65 (Good)
- Every run after this, the length reduces to 0 or 1, or something way way less than 65, which is bad.

If anyone sees what's wrong here, I greatly appreciate any help!

Thanks!
Are you ensuring that the zero character in location 64 is still a zero character after the array has been swapped around?
Ah! That seems to do the trick! Thanks! :)
Topic archived. No new replies allowed.