hi, my desired output is something like this
eros>>./robot
>>>> >oops
error: invalid command (oops)
>>>> >print
list is empty
>>>> >add 100 100 get 5 bolts
>>>> >print
list contains 1 instruction:
100 100 get 5 bolts
>>>> >add 200 200 get 5 nuts
>>>> >add 300 300 get 5 washers
>>>> >print
list contains 3 instructions:
100 100 get 5 bolts
200 200 get 5 nuts
300 300 get 5 washers
eros>>
the program is stoped by pressing control d
heres my code, any help would be much appreciated
Code:
#include <stdio.h>
#include <stdlib.h>
#include "/home/sit172/include/list.h"
struct command
{
char control[50];
struct command *nextPtr;
};
typedef struct command COMMAND;
typedef COMMAND *COMMANDPTR;
void insert(COMMANDPTR *, char);
void printlist(COMMANDPTR);
int main(void)
{
COMMANDPTR startPtr = NULL;
char commands[10];
while(commands != "^d"){
gets(commands);
switch(commands){
case "print":
printlist(COMMANDPTR);
break;
case "add":
gets(command->control);
insert(&startPtr, command->control);
break;
case "^d":
break;
default:
printf("error: invalid command (%s)", commands);
}
}
return(0);
}
void insert(COMMANDPTR *sPtr, char value){
COMMANDPTR newPtr, previousPtr, currentPtr;
newPtr = malloc(sizeof(COMMAND));
if(newPtr != NULL){
newPtr->data = value;
previousPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL && value > currentPtr->data){
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if(previousPtr == NULL){
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else{
previousPtr->nextPtr =newPtr;
newPtr->nextPtr = currentPtr;
}
}
else{
printf("not enough memory");
}
}
void printlist(COMMANDPTR currentPtr){
if(currentPtr == NULL)
printf("the list is empty\n");
else{
while(currentPtr != NULL) {
printf("%s", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
}
}
there are to many errors to note. thanks guys.