Thread: strcmp() inside an if() statement & printf()

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    103

    strcmp() inside an if() statement & printf()

    Good morning all, this is some code to a larger program. First of all, would multiple if statements with the strcmp functions specifying "commands" to drive the ">" prompt be sufficient, or is there a better way to do this? Right now, with the printf statements inside the if functions, i'm not getting output after compiling and running the program, but if the printf statements are outside of the closing "}" brace after each if statement I get output. But the program is displaying ang executing both, this is not what I want. I only want each one displayed for each "MEETING LIST" command and for example "MEETING ADD 2 101" command. Thanks!


    Also, i tried several times with the code comment, and kept getting an error to include the code comment that's why I attached the source code as an attachment to this post.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    834
    I don't know what a "code comment" is, but you can, of course, post your code here:
    Code:
    #include <stdio.h>
    #include <string.h>
     
    #define MEETING_ARRAY_LENGTH 5
     
    int main(void)
    {
        char meeting_array[MEETING_ARRAY_LENGTH][20] = {
            "meeting 1",
            "meeting 2",
            "meeting 3",
            "meeting 4",
            "meeting 5",
        };
     
        char string[15];
        char string_2[15];
        int meeting_id_number;
        int date_id;
        char line[200];
        int num = 1;
     
        do
        {
            printf("> ");
     
            fgets(line, sizeof line, stdin);
            sscanf(line, "%s %s %d %d", string, string_2, &meeting_id_number, &date_id);
     
            if (meeting_id_number > MEETING_ARRAY_LENGTH)
            {
                printf("Program will produce an error!\n");
                break;
            }
     
            if (strcmp(string, "MEETING") == 0 && strcmp(string_2, "LIST") == 0 && meeting_id_number == num)
            {
                printf("%s\n", meeting_array[meeting_id_number - 1]);
            }
     
            // # MEETING ADD num 01102020
     
            if (strcmp(string, "MEETING") == 0 && strcmp(string_2, "ADD") == 0 && meeting_id_number == num && date_id > 1)
            {
                printf("array: %s\ninputted date_id: %d\n", meeting_array[meeting_id_number - 1], date_id);
            }
     
        } while (strcmp(string, "0") != 0);
    }
    I haven't changed anything, except to replace tabs with spaces and remove some extraneous parens. Hopefully someone else can help you.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    240
    This might help...
    Code:
    if (strcmp(string, "MEETING") == 0 && strcmp(string_2, "LIST") == 0 && meeting_id_number) {
      printf("%s\n", meeting_array[meeting_id_number - 1]);
    };
    if (strcmp(string, "MEETING") == 0 && strcmp(string_2, "ADD") == 0 && meeting_id_number && date_id > 1) {
      printf("array: %s\ninputted date_id: %d\n", meeting_array[meeting_id_number - 1], date_id);
    };
    "without goto we would be wtf'd"

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,478
    It would be helpful to see some of the actual commands allowed.

    Also is it guaranteed that the "messages" will always be in upper case?

    You may want to simplify your if statement chain. It looks like the string1 (why the horrible meaningless name) must be "MESSAGE" and that the id number must be greater than 1 so perhaps starting with something more like? :

    Code:
       if((strcmp(string, "MEETING" == 0) && meeting_id_number == num)   
       {
          if (strcmp(string_2, "LIST") == 0)
          {
                 // Do whatever is needed to "LIST".
          }	
          else if(strcmp(string_2, "ADD" == 0)
          {
                 // Do whatever is needed to "ADD".
          }
       }
    Also your sscanf() should be using the proper "optional" width argument to prevent possible buffer overflows of your strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. While statement with getline and strcmp to break
    By Freem in forum C++ Programming
    Replies: 3
    Last Post: 07-08-2011, 06:57 AM
  3. printf not working inside main scope
    By Brownman in forum C Programming
    Replies: 3
    Last Post: 08-03-2009, 12:35 AM
  4. printf+gets inside of code
    By tomas.ra in forum C Programming
    Replies: 7
    Last Post: 11-01-2005, 10:58 PM
  5. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 01:26 AM

Tags for this Thread