First program

This is a discussion on First program within the C Programming forums, part of the General Programming Boards category; Hi, I'm new here. I've been trying to make this program work but i'm not sure how to store text ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    Smile First program

    Hi, I'm new here. I've been trying to make this program work but i'm not sure how to store text user input. This program seems to miss the first statement no matter what the input. Sorry for being such a noob

    Code:
    #include <stdio.h>
    
    char c[6];
    char kate[7] = "kate";
    
    main()
    {
    	printf("What is your name : ");	
    	scanf("%c",c);
    	if(c == kate) printf("Thats a great name");
    	else printf("What a $$$$ name");
    return 0;
    }

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    To compare strings, you must use the strcmp() function, you cannot use ==.
    Code:
    if (!strcmp(c, kate))
    {
    ...
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>>scanf("%c",c);

    This needs to be scanf("%s", c);
    notice the %s to input a whole string and not just 1 character.

    ----edit---- It would be best also to make sure the compiler knows main returns int by saying
    Code:
    int main()
    Last edited by stumon; 07-25-2003 at 07:24 AM.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    if(c == kate) printf("Thats a great name");

    Use strcmp() or another string related function to compare strings. I think strcmp() returns 0 if the strings match. But I haven't use it in a very long time so I'm not positive.

    As well I'm not sure what header strcmp() is in on your compiler. For Borland and DJGPP it is usually in <string.h> but I'm sure MSVC is different.

    Not to confuse you or anything but you can use the string class to accomplish this as well. It will let you manipulate/compare strings in an easier, more natural manner.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>>As well I'm not sure what header strcmp() is in on your compiler. For Borland and DJGPP it is usually in <string.h> but I'm sure MSVC is different.

    That is the C standard string library. MSVC, Borland, GCC, and almost all compilers use that header for string manipulation.
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    49
    Wow, this is a really fast forum
    Thanks for your help guys, it works

    now its gone to the recycle bin
    but at least i learned something

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    That is the C standard string library. MSVC, Borland, GCC, and almost all compilers use that header for string manipulation.
    Thanks. Wasn't sure. Sometimes Borland and MSVC differ a bit in their headers.

  8. #8
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    If you don't want to use strcmp you could also compare your strings element by element but that's no smoth way of doing it imho.

    :edited for spelling mistakes:
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  9. #9
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    Re: First program

    Originally posted by Elite
    Hi, I'm new here. I've been trying to make this program work but i'm not sure how to store text user input. This program seems to miss the first statement no matter what the input. Sorry for being such a noob

    Code:
    #include <stdio.h>
    
    char c[6];
    char kate[7] = "kate";
    
    main()
    {
    	printf("What is your name : ");	
    	scanf("%c",c);
    	if(c == kate) printf("Thats a great name");
    	else printf("What a $$$$ name");
    return 0;
    }
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
          char ustr[30], cstr[] = "Kate", *p;
          
          printf("What is your name: ");
          fgets(ustr, sizeof(ustr), stdin);
    
          if((p = strchr(ustr, '\n') != NULL)
               *p = '\0'; /* strip newline if present */
    
          if(strcmp(ustr, cstr) == 0)
                printf("%s, that's a great name!", ustr);
          else
                printf("%s, isn't a bad name either", ustr);      
    
          return 0;
    }
    If you want not to worry about case in the user entry such as "kate" being the same as "Kate", "KaTe", or "KATE", then use stricmp
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    if((p = strchr(ustr, '\n')) != NULL)
    >then use stricmp
    Note that stricmp is non-standard and may not be available.

    And it is best to have a newline presented to the stdout before a program exits, otherwise it may not be displayed.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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