Plasmeious
Plasmeious

Reputation: 23

c: strcmp Not Stopping At Conditional Statement It Should Hit

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

Upvotes: 1

Views: 111

Answers (2)

Akshay Patole
Akshay Patole

Reputation: 474

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.

Upvotes: 1

Govind Parmar
Govind Parmar

Reputation: 21532

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.

Upvotes: 0

Related Questions