1

I'm learning to create multi-file programs for one of my classes. Ultimately I need to implement a stack and do some stuff with the stack. Before I began implementing the stack I wanted to make sure my files were all linked together properly with a header file. For some reason when the user inputs "pop" or "print" the conditional statement is not triggered and the method in stack.c is not called. I've been looking at this for awhile and haven't gotten anywhere. Thank you for the help

MAIN.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
void pop(char list[]);
void print(char list[]);
void push(char list[]);   
int main(void)
 {
    char input[5];
    char test[5];
    while( strcmp("exit",input) != 0)
    {
             printf("Please enter a command. \n");
            fgets(input,sizeof(input),stdin);
                    if(strcmp("pop",input)==0)
                    {
                            pop(test);
                    }
                     else if(strcmp("push",input)==0)
                    {
                            push(test);
                    }
                    else if (strcmp("print", input)==0)
                    {
                            print(test);
                    }
    }
     return 0;
   }

STACK.c

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"

void pop(char list [])
{
    printf("This is in the stack file in pop\n");
}
void push(char list [])
{
    printf("This is in the stack file in push\n");
}
void print(char list[])
{
    printf("This is in the stack file in print\n");
}

Console Output

Please enter a command.
push
This is in the stack file in push
Please enter a command.
Please enter a command.
pop
Please enter a command.
print
Please enter a command.
Please enter a command.
exit
Plasmeious
  • 23
  • 2
  • Your 4+ char commands aren't consuming the newline in your input. `input` is too short. – WhozCraig Nov 19 '16 at 22:06
  • Read the [fgets man page](https://linux.die.net/man/3/fgets): " If a newline is read, it is stored into the buffer". That is, your `input` buffer will contain a newline (ignoring the fact that the buffer is not actually big enough). So you either need to strip off the newline character or your `strcmp` need to include it. – kaylum Nov 19 '16 at 22:08
  • Possible duplicate of https://stackoverflow.com/questions/2404794/strcmp-on-a-line-read-with-fgets – WhozCraig Nov 19 '16 at 22:10
  • 1
    Also *undefined behaviour* with the first `strcmp("exit",input)` since `input[]` being a local variable is not automatically initialised. – Weather Vane Nov 19 '16 at 22:10
  • First, make the buffer bigger, e.g. `char input[100];`. Second, [remove the newline from the buffer](https://stackoverflow.com/questions/2693776). – user3386109 Nov 19 '16 at 22:13
  • Thank you guys it was an undefined behavior. I made the arrays bigger and nothing happened – Plasmeious Nov 19 '16 at 22:14

2 Answers2

1

I will suggest use of strstr() instead of strcmp(). If you use strstr() then there is no need to mention '\n' in the string to be searched.

The strstr() function finds the first occurrence of the substring needle in the string haystack. For better understanding you can visit, http://man7.org/linux/man-pages/man3/strstr.3.html

Code will look like,

while( strstr(input,"exit") == NULL)
{
    printf("Please enter a command. \n");
    memset(input,0,sizeof(input));
    fgets(input,sizeof(input),stdin);
    if(strstr(input,"pop"))
    {
        printf("pop\n");
    }
    else if(strstr(input,"push"))
    {
        printf("push\n");
    }
    else if (strstr(input,"print"))
    {
        printf("print\n");
    }
}

I agree with @Govind Parmar that 5 bytes are not sufficient for input buffer. You need to declare input buffer with 7 bytes.

Akshay Patole
  • 454
  • 3
  • 14
0

Three things:

  1. The line read by fgets() will include \n at the end. Test for strcmp("word\n", input)==0.
  2. 5 is not sufficient size for input since you need to be testing for newlines ("push\n\0" is 6 bytes; "print\n\0" is 7 bytes)
  3. You test for strcmp("exit", input) without input being initialized. This is undefined behavior. Set input to be all-zeroes before beginning your loop.
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85