0

I am trying to set up a program that when you type /s then a movie title (ex. /s Green Mile it searches for that movie title in the movies array but my strstr function is not working. Also /a adds movie /r shows all movies in database and /q quits program.

#include <stdio.h>
#include <string.h>
main() {

    char movies[1000][64];
    int i;
    int j = 1;
    int k = 0;
    int counter = 0;
    char buffer[64];
    char buffer2[64];
    int len;

    do {
      printf("Enter command for movie database:\n");
      fgets(buffer, 64, stdin);

      if (buffer[1] == 'a') {
         len = strlen(buffer);
         if (buffer[len - 1] == '\n')
             buffer[len - 1] = '\0';

         for(i=3; i<sizeof(buffer); i++) {
            movies[counter][k] = buffer[i];
            k++;
         }
         printf("You have chosen to enter a movie in the database.\n");
         printf("The movie added to the database is: %s\n", &movies[counter][0]);
         counter++;
         k = 0;
      }

      if (buffer[1] == 'q') {
         printf("You have chosen to quit.\n");
         printf("Goodbye.\n");
      }

      if (buffer[1] == 's') {
         for(i=2; i<sizeof(buffer); i++) {
            buffer2[k] = buffer[i];
            k++;
         }

         for (i = 0; i < counter; i++) {
            if (strstr(movies[i], buffer2) != NULL) {
               printf("Found %s in position %d,%s\n", buffer2, i + 1, movies[i]);
               printf("Index of Movie is %d.\n", i + 1);
            }
         }
         k = 0;
      }

      if (buffer[1] == 'r') {
         printf("You have choosen to report movies in database\n");
         printf("These are the movies you have watched:\n");
         for( i = 0; i < counter; i++ ) {
            printf("%d: %s\n", j,  &movies[i][0]);
         j++;
         }
      printf("\n");
      j = 1;
      }
   } while (buffer[1] != 'q');
}
Preston S
  • 2,751
  • 24
  • 37
  • Your buffer2's first character is a space and has not been properly null terminated. – Preston S May 13 '14 at 19:26
  • So I would have to make my counter variable (i) start at 3 and I thought buffer was null terminated with the '\0' in the beginning then I copied buffer to buffer2? – user3633960 May 15 '14 at 21:13
  • You are right, I missed that you were copying the whole buffer. You would be better off doing strcpy(buffer2, &buffer[3]); – Preston S May 16 '14 at 00:49
  • Ya I realized after that I could just use strcpy. But why isnt strstr working its returning null always when they are compared. – user3633960 May 16 '14 at 05:36
  • If removing the space between "/s" and the movie name didn't fix your problem, you'll have have to print out `buffer2` and `movies[i]` to see what's different. Also, use `strcmp(buffer2, movies[i]) == 0` instead of strstr. – Preston S May 16 '14 at 14:59
  • Also, you can remove buffer2 altogether from your program and replace it with `char * movieTitle = &buffer[3];` Then it will always reference a string beginning at the 3rd character in the buffer and will never need to be modified. Then just use `movieTitle` in your /a and /s cases. – Preston S May 16 '14 at 15:04

1 Answers1

0

Revised code.

#include <stdio.h>
#include <string.h>
main() {

    char movies[1000][64];
    int counter = 0;
    char buffer[64];
    char * movieTitle = &buffer[3];

    while(true) {
      printf("Enter command for movie database:\n");
      fgets(buffer, 64, stdin);

      //replace terminator every time
      int len = strlen(buffer);
      if (buffer[len - 1] == '\n')
          buffer[len - 1] = '\0';

      if(strstr(buffer, "/a") && counter < 1000) {
         strcpy(movies[counter], movieTitle);

         printf("You have chosen to enter a movie in the database.\n");
         printf("The movie added to the database is: %s\n", &movies[counter][0]);
         counter++;
      }

      else if (strstr(buffer, "/s")) {
         for (int i = 0; i < counter; i++) {
            if (strcmp(movies[i], movieTitle) == 0) {
               printf("Found %s in position %d,%s\n", movieTitle, i + 1, movies[i]);
               printf("Index of Movie is %d.\n", i + 1);
            }
         }
      }

      else if (strstr(buffer, "/r")) {
         printf("You have choosen to report movies in database\n");
         printf("These are the movies you have watched:\n");

         for( i = 0; i < counter; i++ ) {
            printf("%d: %s\n", i + 1,  &movies[i][0]);
         }
         printf("\n");
      }

      else if (strstr(buffer, "/q")) {
         printf("You have chosen to quit.\n");
         printf("Goodbye.\n");
         break;
      }

      else {
          printf("unrecognized command");
      }
   }
}
Preston S
  • 2,751
  • 24
  • 37