Thread: Simple string problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Simple string problem

    I would like to create a program where a user inputs their first and last name and the program say it to the user, but my program is only showing the first name.

    Any help is greatly appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char userfirst[] = "";
        char userlast[] = "";
        
        printf("Please enter in your first name and last name:\n");
        scanf("%s,%s", userfirst, userlast);
        
        printf("Is your first and last name i%s?\n", strcat(userfirst, userlast));
        
        getchar();
        
        return 0;   
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Your arrays are one byte long, which is only long enough to hold an empty string. C does not have dynamically expanding arrays/strings like some languages, nor will it tell you when you've overflowed a buffer, which will happen here.

    There's no simple way to do proper text input in C. The quick fix to your problem is to do something like:
    Code:
    char userfirst[32], userlast[32];
    Now you have room for 31 characters (plus the terminating null character which all strings require). You can still overflow the buffers, but at least this is a start.

    Next, you'll want to use "%s%s", not "%s,%s". The comma is not a special separator here. Instead, it tries to read a literal comma. The problem is that %s reads all non-whitespace characters, so it will read a comma, too. Thus whenever %s stops reading characters, you can be guaranteed the next character will not be a comma.

    You can get fancy and use "%31s%31s"; the “31”s tell scanf() to read no more than 31 characters. This will prevent buffer overflows, but if a person's name is longer than 31 characters, you'll have other issues to deal with. This is what I mean by text input in C being difficult.

    Finally, strcat(), like so many other functions in C, will not do any buffer overflow checking. You have to be sure there's enough space in the target before you tack on the other string. You could do this by making sure the first array is at least twice the length of the second, although you'd probably be better off with:
    Code:
    printf("%s %s\n", userfirst, usersecond);

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You first need to allocate some memory for your arrays.
    Code:
        char userfirst[] = "";
        char userlast[] = "";
    This is not allocating any memory for these variables.

    Jim

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by jimblumberg View Post
    You first need to allocate some memory for your arrays.
    Code:
        char userfirst[] = "";
        char userlast[] = "";
    This is not allocating any memory for these variables.

    Jim
    As cas pointed out, it's allocating 1 byte for each variable; enough space to hold the string terminator.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Thanks a lot cas, jimblumberg and itsme86!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by benrogers View Post
    I would like to create a program where a user inputs their first and last name and the program say it to the user, but my program is only showing the first name.

    Any help is greatly appreciated.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char userfirst[] = "";
        char userlast[] = "";
        
        printf("Please enter in your first name and last name:\n");
        scanf("%s,%s", userfirst, userlast);
        
        printf("Is your first and last name i%s?\n", strcat(userfirst, userlast));
        
        getchar();
        
        return 0;   
    }
    First off that is not the way to declare stings. Since you used "" you have created a string that will hold exactly 0 characters. Try something like ... char userfirst[16];

    Second you have a , in the formatting string for scanf... this will require the user to enter Bill,Jones which is rather unnatural. Just have a space between the two string catchers so the person can enter Bill Jones which is very natural.

    Third you can't use strcat where you have it. This has to be a separate operation done before you display the concactinated names... And be sure that userfirst has enough space to hold both the first and last names anyone is likely to enter... char userfirst[128]; or so.

    Finally you could have printed the first and last names without using strcat... just use the %s string catcher twice in your printf() statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. C/C++ String Problem.
    By Jaken Veina in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2005, 10:11 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM