I don't understand/know how to change my if else statements to become switch statement with cases. Please help! I just need to change my if else statements to a switch and my assignment will be complete! Its fully running and functioning!
This C program provides a main function that supports a bash-like history capability; The main objective for the circular buffer is to repeatedly display a prompt to the user, and each prompt assigns an input number starting at 1. Each line inputted into the circular buffer is stored until it gets overwritten, the buffer only allows up to 5 lines of stored input, FIFO(First in, first out).
Giving the user 4 command options: !x: With x meaning the line number selected, and the exclamation point("!") meaning repeat the (absolute) input line numbered x. This only works if this line is one of the saved commands. This command will display the original input line as well as storing a copy. If the argument x is invalid, an error message will be displayed.
exit(case sensitive): Terminates the program.
history(case sensitive): Prints the saved commands
parse(case sensitive): Tokenize input line number x, This input line shouldn't be stored in the buffer, but instead display each word on a seperate line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CIRCULAR_HISTORY_BUFFER 5
#define CHARACTER_LENGTH 128
void CircularHistoryBuffer()
{
char memory[CIRCULAR_HISTORY_BUFFER][CHARACTER_LENGTH];
char command_line[CHARACTER_LENGTH];
int data, n, i;
int number = 0;
while(1)
{
printf("%d>", number + 1);
fgets(command_line, CHARACTER_LENGTH - 1, stdin);
for(i = 0; i < CHARACTER_LENGTH; i++)
{
if(command_line[i] == '\n')
{
command_line[i] = '\0';
break;
}
}
if(command_line[0] == '!')
{
n = atoi(command_line + 1);
if(n < number - CIRCULAR_HISTORY_BUFFER + 1 || n > number || n <= 0)
{
printf( "%d: Not found\n", n);
}
else
{
data = (n - 1) % CIRCULAR_HISTORY_BUFFER;
printf("%s\n", memory[data]);
strcpy(memory[number % CIRCULAR_HISTORY_BUFFER], memory[data]);
number++;
}
}
else if(strcmp(command_line, "exit") == 0)
{
exit(0);
}
else if(strcmp(command_line, "history") == 0)
{
if(number <= CIRCULAR_HISTORY_BUFFER)
{
for(i = 0; i < number; i++)
{
printf("%d\t%s\n", i + 1, memory[i]);
}
}
else
{
n = number - CIRCULAR_HISTORY_BUFFER + 1;
data = number % CIRCULAR_HISTORY_BUFFER;
for(i = data; i < CIRCULAR_HISTORY_BUFFER; i++)
{
printf("%d\t%s\n", n++, memory[i]);
}
for(i = 0; i < data; i++)
{
printf("%d\t%s\n", n++, memory[i]);
}
}
}
else if(strncmp(command_line, "parse", 5) == 0)
{
n = atoi(command_line + 5);
if(n < number - CIRCULAR_HISTORY_BUFFER + 1 || n > number || n <= 0)
{
printf("%d: event not found\n", n);
}
else
{
data = (n - 1) % CIRCULAR_HISTORY_BUFFER;
for(i = 0; i < strlen(memory[data]); i++)
{
if(memory[data][i] == ' ')
{
printf("\n");
}
else
{
printf("%c", memory[data][i]);
}
}
printf("\n");
}
}
else
{
strcpy(memory[number % CIRCULAR_HISTORY_BUFFER], command_line);
number++;
}
}
}
int main()
{
CircularHistoryBuffer();
return 0;
}