Using Characters in If Else Statement

This is a discussion on Using Characters in If Else Statement within the C Programming forums, part of the General Programming Boards category; How do I do this properly? I want it to printf a separte statement if a certain name is entered. ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    21

    Using Characters in If Else Statement

    How do I do this properly?
    I want it to printf a separte statement if a certain name is entered. (spencer)
    Code:
    int main (){
      char name[30];
    printf("\nTo get started, Enter your name!\n");
                          
        scanf("%c", &name);
         if (name=='spencer')
        {
            printf("Welcome to my program Spencer");  
            }
            else{ printf ("Welcome %c", name);
            }
      
        
      system ("pause");
        }
    Last edited by anonymoususer; 10-15-2011 at 11:14 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use single quotes for single characters.
    Use double quotes for multiple characters.

    You can't compare multiple characters (strings, or arrays, etc.) with ==, you need to compare each character at a time. Luckily C has a function for strings to do this: strcmp

    In printf %c is for a single character and %s is for strings.


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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    I've taken into account what you said and it works better now but when I enter spencer it says welcome spencer. (its supposed to say welcome to my program spencer)
    Code:
    int main (){
      char name[30];
    printf("\nTo get started, Enter your name!\n");
                           
        scanf("%s", &name);
         if (name=="spencer")
        {
            printf("Welcome to my program Spencer");  
            }
            else{ printf ("Welcome %s", name);
            }
       
         
      system ("pause");
        }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,756
    You can not use the comparison operator== with C-strings you need to use strcmp().

    Jim

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    Thanks that was helpful but when I set my code up like this I get "welcome to my program" and "welcome spencer".
    Its only supposed to say welcome to my program

    Code:
    int main (){
      char name[30];
       char szKey[] = "spencer";
      char szInput[80];
    printf("\nTo get started, Enter your name!\n");
                           
        scanf("%s", &name);
        if( strcmp( name, "spencer") == 0 )
    {
            printf("Welcome to my program");  
            }
             if( strcmp( name, "Spencer") == 0 )
    {
            printf("Welcome to my program");  
            }
            else
            { 
                  
                  printf ("Welcome %s", name);
            }
       
         
      system ("pause");
        }
    Last edited by anonymoususer; 10-16-2011 at 12:07 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Change this:
    Code:
    scanf("%s", &name);
    to this:
    Code:
    scanf("%s", name);
    
    if((strcmp(name, "spencer")) || (strcmp(name, "Spencer)))
    should help clean up the code. Your compiler may have a case insensitive version of strcmp(), or you can just lower the chars in name, and make all Spencer's, into spencer, (or SPENCER, either one), before the comparison.
    Last edited by Adak; 10-16-2011 at 12:32 PM.

  7. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Your indentation is horrid.

    Code:
    if (strcmp(name, "spencer") == 0 ) {
        printf("Welcome to my program"); 
    }
    if (strcmp(name, "Spencer") == 0 ) {
        printf("Welcome to my program"); 
    }
    else {
        printf ("Welcome %s", name);
    }
    There. else statements are executed when the previous if statement is NOT executed. For example:

    Code:
    if (age < 18)
        printf("You can not smoke cigarettes.\n");
    else
        printf("Light up, hophead!\n");
    Just take out the first if statement in the code I posted above, and you should be good.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    21
    Thanks alot! that fixed it

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,756
    You need to look at the return value of strcmp(), it returns 0 when they are equal. So you really should compare against zero.
    Code:
    if((strcmp(name, "spencer") == 0) || (strcmp(name, "Spencer") == 0)){
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  2. Can repeated characters be printed with a C statement?
    By Pete_2000 in forum C Programming
    Replies: 4
    Last Post: 05-05-2006, 07:47 PM
  3. if statement
    By xlordt in forum C Programming
    Replies: 7
    Last Post: 04-11-2003, 08:06 AM
  4. Using the Switch statement with Characters?
    By Sulaco in forum C Programming
    Replies: 2
    Last Post: 11-18-2002, 12:13 PM
  5. help with if else if statement and characters
    By z.tron in forum C++ Programming
    Replies: 2
    Last Post: 07-09-2002, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21