Okay so i seperated the 2 functions but no history is printing. Why?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 200
#define HISTORY_SIZE 10 /*the maximum size of the history */
//#define MAX_STRING_LEN 100 /*the maximum size of strings*/
#define MAX_CMD_LEN 10 /*the maximum size of a command */
void getString(char *);
void printString(const char *);
void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
void addHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
int main(void)
{
char command[MAX_CMD_LEN];
char history_table[HISTORY_SIZE][MAX_CMD_LEN];
char string[SIZE];
int history_size = 0;
char *p;
int flag = 1;
do {
printf("cmd> ");
if (fgets(command, MAX_CMD_LEN, stdin) != NULL) {
if ((p = strchr(command, '\n')) != NULL)
*p = '\0';
}
if (strcmp(command, "new") == 0) {
getString(string);
}
if (strcmp(command, "list") == 0) {
printString(string);
}
if (strcmp(command, "hist") == 0) {
printHistory(history_table, history_size);
}
}
while (flag == 1);
return 0;
}
void getString(char *string)
{
printf("Please enter your string");
fgets(string, SIZE, stdin);
}
void printString(const char *string)
{
printf("\n\nHere is the text you entered:\n%s\n", string);
}
void printHistory(char history[][MAX_CMD_LEN], const char *cmd, int size)
{
int i;
if (size < HISTORY_SIZE)
{
//Put the commands in the history table.
strcpy(history[size], cmd);
printf("%s", history[size]);
}
void addHistory(char history[][MAX_CMD_LEN], const char *cmd, int size);
//Make space if size exceeds the HISTORY_SIZE.
{
if (size > HISTORY_SIZE - 1) {
for (i = 0; i < 9; i++)
{
strcpy(history[i], history[i + 1]);
}
strcpy(history[HISTORY_SIZE - 1], cmd); //Place the last element in last spot.
}
}