Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing value of array to the start of the loop
    primarykey
    data
    text
    <p>My goal is to have the user view the history of entered commands (<code>historyArray</code> - done) and allow him to re-run any command in history, by entering <code>history 1</code>, <code>history 2</code> where <code>1</code> and <code>2</code> is the number of the list of commands as printed-out from <code>historyArray</code>. I have managed to obtain the index from the second parameter (<code>history 1</code>) of the user input. My question is now, how to execute that specific command obtained from <code>history N</code>?</p> <p>So, for example:</p> <pre><code> hshell&gt; test [Enter] Command not found hshell&gt; history 1 Command not found </code></pre> <p>Here is my progress:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; int main (int argc, char *argv[]) { int i=0; int j=0; int k=0; int elementCounter = 0; char inputString[100]; char *result=NULL; char delims[] = " "; char historyArray[30][20] = {0}; char tokenArray[20][20] ; int tempIndex = 0; char hCommand[2][20]={0}; do { j = 0; printf("hshell&gt;"); gets(inputString); strcpy (historyArray[k], inputString); k = (k+1) % 20; if (elementCounter &lt;= 20) { elementCounter++; } if (elementCounter == 21) { k = 20; for (i=0; i&lt;20; i++) { strcpy(historyArray[i], historyArray[i+1]); } strcpy (historyArray[19], inputString); } // 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; } else if (strcmp(tokenArray[0], "history") == 0) { if (j&gt;1) { tempIndex = atoi(tokenArray[1]); strcpy(hCommand,historyArray[tempIndex-1]); puts(hCommand); // tempIndex = atoi(tokenArray[j]); //puts(tempIndex); } else { //print history array for (i=0; i&lt;elementCounter-1;i++) printf("%i. %s\n", i+1, historyArray[i]); } } else { printf("Command not found\n"); } }while (1); } </code></pre> <ul> <li><code>hCommand</code> is where I store the command as obtained from <code>historyArray</code>.</li> <li>I am using a Windows machine.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload