Thread: basic if statements

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    basic if statements

    hello, i seem to be having a problem with my if statement for my basic program. I have a while loop that goes through a file and assigns certain values to a string str. The while loop works and correctly assigns the values to str however when i run my if styatements to filter out certain criteria, all statements get printed out even if it only find 1 match for some reason. Ive checked my if statement structure and it looks right to me, could anyone offer suggestions?

    Code:
    string str;
    
    while ()
    {
    //loop through a file assigning values to 'str'
    
    }
    
    printf("%s", str);   // test print which shows while loop works
    
    if(strcmp ("dog", str) == 1)
    	       {   
                 printf("dog has found \n");
                 memset(str, 0, sizeof(str));
          	 }
          	 else if (strcmp ("cat", str) == 1)
          	 {
          	   printf("cat has found ");
          	                memset(str, 0, sizeof(str));
          	 }
          	 else 
          	 {
          	   printf("tortose");
          	                memset(str, 0, sizeof(str));
          	 }
    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strcmp returns 0 for equal, a postive number if first argument is greater than the second, and negative second argument being grater than the first.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    tanks for the input, ive had a mess about but it still wont work with anything. Say the whille loop returns the string values

    i have a dog
    lots of cats
    dogs and bears

    my structure with an '==0' or "001" doesnt function as i hoped, it either runs stright through to the last catch all else statement or prints them all

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, to find if something CONTAINS another string, you need strstr().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  3. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  4. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM