0

I cannot figure out why this code is jumping straight to pattern 5. I've looked at it several times and I just don't see it. Any help would be greatly appreciated. I am guessing it has something to do with the way that I've initialized arrays and the way I am comparing them. I have tried using 'strcmp' and currently trying to compare direct array positions. Both of these have compiled successfully, but I just can't seem to get it to work.

    char one[3][3];
    const char *pattern[] = {"p1","p2","p3","p4","p5"};

    printf("%s Commands are {'p1', 'p2', 'p3', 'p4', 'p5'\n", prompt);
    printf("Enter your first pattern choice: ");
    scanf("%2s",one[0]);

    printf("Enter your second pattern choice: ");
    scanf("%2s",one[1]);

    printf("Enter your third choice: ");
    scanf("%2s",one[2]);

    for (int i = 0; i < 2; i++)
    {
        do{
            if (one[i] == "p1")
            {
                printf("\n1.1");
                patternOne();}
       else if (one[i] == "p2")
            {
                printf("\n1.2");
                patternTwo();}
       else if (one[i] == "p3")
            {
                printf("\n1.3");
                patternThree();}
       else if (one[i] == "p4")
            {
                printf("\n1.4");
                patternFour();}
       else
            {
                printf("\n1.5");
                patternFive();
       }

  }
while (i < 3);
Charkins12
  • 190
  • 4
  • 16

1 Answers1

1

For string comparison use strcmp() function from string.h.

You are not comparing strings in C style, so it is evaluating to else.

Your expected code will probably be:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char one[3][3];
    const char *pattern[] = { "p1", "p2", "p3", "p4", "p5" };

    printf("Enter your first pattern choice: ");
    scanf("%2s", one[0]);

    printf("Enter your second pattern choice: ");
    scanf("%2s", one[1]);

    printf("Enter your third choice: ");
    scanf("%2s", one[2]);

    for (int i = 0; i < 2; i++)
    {
        if (strcmp(one[i],"p1") == 0)
        {
            printf("\n1.1");
            patternOne();
        }
        else if (strcmp(one[i], "p2") == 0)
        {
            printf("\n1.2");
            patternTwo();
        }
        else if (strcmp(one[i], "p3") == 0)
        {
            printf("\n1.3");
            patternThree();
        }
        else if (strcmp(one[i], "p4") == 0)
        {
            printf("\n1.4");
            patternFour();
        }
        else if (strcmp(one[i], "p5") == 0)
        {
            printf("\n1.5");
            patternFive();
        }
        else
        {
            printf("Unknown input.");
        }
    }
    return(0);
}

Changes made:

  1. Removed the inner do-while loop as i was incremented only by the outer for loop. Since i was not incremented inside the do-while it can cause infinite loop.
  2. Added an else if cluse to handle p5 input and added a separate else to indicate that un-expected output was encountered.
  3. Replaced if else conditions with strcmp() equallent condition and included string.h in the file.

Edit (to answer the comment):

If you want it to display all 3 results, change:

    for (int i = 0; i < 2; i++)

To

    for (int i = 0; i <= 2; i++)

Currently, for i < 2 it loops for i = {0, 1} and skips for i = 2 as condition fails. If you change condition to i <= 2, it will loop for i = {0, 1, 2}.

  • Awesome response thank you! That did the trick. Now though it is only displaying 2 of any 3 patterns entered. Any clue as to why that is? – Charkins12 Feb 23 '15 at 03:40