This is my implementation of a circular array so far. It is supposed to store the last 5 commands entered, by entering the 6th command in the place of the 5th and discarding the 1st. What I have managed to do so far is, to able to store the 5 commands and print them out. On the 6th command, I noticed that it goes in the 2nd position (k=1
) of the historyArray
, but when debugging, k
was equal to 0
which would at least push the last command at the top. If you can put me in the right track again, I would appreciate it. Here is part of the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
int i=0;
int j=0;
int k=0;
int tempIndex = 0;
int elementCounter = 0;
char inputString[100];
char *result=NULL;
char delims[] = " ";
char historyArray[5][20] = {0};
char tokenArray[20][20] ;
char hCommand[1][20];
do
{
j = 0;
printf("hshell>");
gets(inputString);
//skip writing "history" in historyArray
if (strcmp(inputString,"history")!= 0)
{
strcpy (historyArray[k], inputString);
}
k = (k+1) % 5;
if (elementCounter <= 5)
elementCounter++;
// Break the string into parts
result = strtok(inputString, delims);
while (result!=NULL)
{
strcpy(tokenArray[j], result);
j++;
result= strtok(NULL, delims);
}
if (strcmp(tokenArray[0], "exit") == 0)
return 0;
if (strcmp(tokenArray[0], "history") == 0)
{
if (j>1)
{
tempIndex = atoi(tokenArray[j]);
puts(tempIndex);
}
else
{
for (i=0; i<elementCounter-1;i++)
printf("%i. %s\n", i+1, historyArray[i]);
}
}
else
{
printf("Command not found\n");
}
} while (1);
}
After suggestions (still incomplete):
j = 0;
//elementCounter = 0;
printf("327>");
gets(inputString);
strcpy (historyArray[k], inputString);
k = (k+1) % 5;
if (elementCounter <= 5)
{
elementCounter++;
}
The bug you describe is occurring because of the lines:
k = (k + 1) % 5;
elementCounter++;
What I see happening:
k initial | calculation | k result | elementCounter
0 (0 + 1) % 5 1 % 5 = 1 1
1 (1 + 1) % 5 2 % 5 = 2 2
...
4 (4 + 1) % 5 5 % 5 = 0 5
0 (0 + 1) % 5 1 % 5 = 1 5
k
is behaving as it's supposed to, as far as I can see. However, when elementCounter
is 5, k = 1.
EDIT: The problem that I see is that the latest command is being added at position k
, not position 0, which based on your implementation is the most recent command entered (based on the various if
clauses, like the one that processes the "exit" and "history" commands). Try this set of commands, using your current algorithm. I expect that the contents of the [Command List] column are what you'll see...
Command # | Command Text | [Command List]
0 (null) []
1 Login [Login]
2 History [Login,History]
3 Skynet [Login,History,Skynet]
4 ps -al [Login,History,Skynet,ps -al]
5 Skynet [Login,History,Skynet,ps -al,Skynet]
6 Exit [Exit,History,Skynet,ps -al,Skynet]
What you would want to do, is copy elements 0-3, and move them to elements 1-4. Then, insert the new command at position 0 in the historyArray
. Thus, your history should look like this after adjusting your algorithm appropriately:
Command # | Command Text | [Command List]
0 (null) []
1 Login [Login]
2 History [History,Login]
3 Skynet [Skynet,History,Login]
4 ps -al [ps -al,Skynet,History,Login]
5 Skynet [Skynet,ps -al,Skynet,History,Login]
6 Exit [Exit,Skynet,ps -al,Skynet,History]