Thread: code ignoring if statements?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    1

    Question code ignoring if statements?

    for some reason my code is compiling but not recognizing the if statements at all? I put printf statements to find out the where the problem was but its really just skipping past all the printf statements and reading input straight into main. how do I fix this

    heres my code in case you need it

    Code:
    char *consider(void){
        char *clothing;
        printf("what are you considering: ");
        clothing=readToken(stdin);
        if (strcmp(clothing, "shirt\n")==0) printf("what kind of shirt: ");
        if (strcmp(clothing, "pants\n")==0) printf("what kind of pants: ");
        if (strcmp(clothing, "other\n")==0) printf("What kind of stuff: ");
        return clothing;
    }
    
    
    
    
    char *kind(char *clothing)
    {
        char *kindofclothes;
        printf("what kind?: \n");
        kindofclothes=readToken(stdin);
    
    
         if (strcmp(clothing,"shirt")==0)
         {
          if (strcmp(kindofclothes, "tshirt")==0)
          if (strcmp(kindofclothes, "sweatshirt")==0)
          if (strcmp(kindofclothes, "nice")==0)
          return kindofclothes;
         } return kindofclothes;
    
    
         if (strcmp(clothing,"pants")==0)
         {
          if (strcmp(kindofclothes, "jeans")==0)
          if (strcmp(kindofclothes, "other")==0)
          return kindofclothes;
         } return kindofclothes;
    
    
         if (strcmp(clothing,"other")==0)
         {
          if (strcmp(kindofclothes, "socks")==0)
          if (strcmp(kindofclothes, "other")==0)
          return kindofclothes;
         } return kindofclothes;
    
    
    }
    
    
    int main()
    {
        char *clothes;
        clothes=consider();
    
    
        char *Watkind;
        Watkind=kind(clothes);
        printf("you chose %s\n", Watkind);
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Does readToken leave the newline at the end of the token? If not, then remove the newlines in consider.

    You have many if statements piled up in kind. I believe you think you're creating a kind of logical "or" structure, but you're actually creating an always-false logical "and" structure. For the logical "or" structure, use the logical "or" operator in a single if statement:
    Code:
    if (strcmp(clothing,"shirt")==0) {
        if (strcmp(kindofclothes, "tshirt")==0
            || strcmp(kindofclothes, "sweatshirt")==0
            || strcmp(kindofclothes, "nice")==0)
            return kindofclothes;
    }
    return kindofclothes;
    Your code doesn't make much sense since it returns kindofclothes no matter what.
    Another thing to consider is whether or not you need to free the strings returned by readToken, but It may use a static buffer instead.
    Last edited by algorism; 02-26-2016 at 11:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code ignoring a cin statement
    By tomatitobean in forum C++ Programming
    Replies: 4
    Last Post: 05-22-2015, 03:34 PM
  2. C code, MD5 hash, if statements assistance
    By letmein in forum C Programming
    Replies: 5
    Last Post: 09-14-2010, 09:25 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 1
    Last Post: 05-26-2006, 08:13 AM
  5. If Statements Tutorial Code Won't Compile.
    By civilwarsearch in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2003, 02:43 AM

Tags for this Thread