Thread: Using strings in if statements

  1. #1
    Brita like the water
    Join Date
    Mar 2005
    Location
    Look at the palm of your hand, its somewhere in there
    Posts
    9

    Using strings in if statements

    I'm learning C in my spare time so there are always little pieces of information I'm missing. For a simple program I wrote, I want it to say for the first if statement, if the age you entered was 18 and you are a girl then do "this".

    I first had something like
    if ((arg1 == 18) && (sex1 = girl))

    but that doesn't work... i initialized all my variables and stuff, i just have the syntax wrong. can someone please give me an answer
    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you're using the assignemnt operator in your second test. But we'll assume that's a typo.

    Next, were it actually correct to do so, it would be:
    Code:
    if( (arg1 == 18) && (sex1 == "girl") )
    Since I'm assuming that because your topic stated you were using strings, that "girl" was supposed to be a string...

    But since you can't do that, you use the built in string comparison function:
    Code:
    if( thisint == 18 && strcmp( thisstring, "girl" ) == 0 )
        ...do something...
    Quzah.
    Last edited by quzah; 03-14-2005 at 05:14 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Brita like the water
    Join Date
    Mar 2005
    Location
    Look at the palm of your hand, its somewhere in there
    Posts
    9

    Oooo la la

    Thanks dude! that makes much more sense. and hehe, yes that was a typo, meant to have double quotes around girl and ==, i couldnt figure out how to use the tags for the code so just quick typed something in to test.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, yeah, I forgot to tell you, strcmp returns zero on success.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Brita like the water
    Join Date
    Mar 2005
    Location
    Look at the palm of your hand, its somewhere in there
    Posts
    9
    when my if statement is true it skips straight to the else statement... anyone know what im doing wrong?

    Code:
    #include <stdio.h>
    
    int main()
    {
    int arg1;
    char sex1[5];
    
    	printf("Enter your age and sex please.\n\r", arg1, sex1);
    	scanf("%d %s", &arg1, &sex1);
    
    
    if( arg1 == 18 && strcmp( sex1, "girl" ) )
    {
    	printf("Whoohoo, you're legal, %s!\n\r", &sex1);
    }
    else
    	printf("Aww, how boring, you're a minor.\n\r");
    	return 0;
    }
    thanks.

  6. #6
    Brita like the water
    Join Date
    Mar 2005
    Location
    Look at the palm of your hand, its somewhere in there
    Posts
    9
    haaha, that answers it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Using Strings in IF statements
    By Programmer3922 in forum C Programming
    Replies: 6
    Last Post: 08-02-2008, 01:36 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. if statements using strings as the condition
    By trang in forum C Programming
    Replies: 7
    Last Post: 12-13-2003, 05:20 PM
  4. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM
  5. need a little help with if statements and strings
    By kes103 in forum C Programming
    Replies: 1
    Last Post: 03-20-2002, 12:08 PM