Thread: Reading and Concatenating Strings

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    10

    Reading and Concatenating Strings

    I have a main method that creates a String and passes it to a function:

    Code:
    char* user_command;
    
    user_command = get_user_command(user_command);
    Then the function "get_user_command" finds out what the user wants to do in my program and sends back a String containing the command.

    Code:
    char* get_user_command(char* user_command)
    {
    	int choice;
    	char text[20];
    
    	printf("-=Security Station=-\n");
    	printf("Add a User 	[1]\n");
    	printf("Change Type 	[2]\n");
    	printf("Change Location [3]\n");
    	printf("Remove User 	[4]\n");
    
    	   fputs("Choice: ", stdout);
    	   fflush(stdout);
    	   if ( fgets(text, sizeof text, stdin) )
    	   {
    	      if ( sscanf(text, "%d", &choice) == 1 )
    	      {
    	         printf("number = %d\n", choice);
    	      }
    	   }
    
    
    	switch(choice)
    	{
    	    case 1:
    	    	user_command = add_user(user_command);
    	    	break;
    	    case 2:
    	    	user_command = change_type(user_command);
    	    	break;
    	    case 3:
    	    	user_command = change_location(user_command);
    	    	break;
    	    case 4:
    	    	user_command = remove_user(user_command);
    	    	break;
    	    default :
    	    	printf("Unknown Command\n");
    	}
    
    	return user_command;
    }
    The add_user code for example, asks the user for information about the person they want to add to the system. Then the strings from the user input should be added together but separated by a "/", so it can be processed as a command later.

    Code:
    char* add_user(char* user_command)
    {
    	char staff_ID[80];
    	char staff_name[80];
    	char staff_type[80];
    	char security_ID[80];
    
    	printf("What is the StaffID of the staff member to be added: ");
    	scanf("%s", staff_ID);
    
    	printf("What is the name of the staff member to be added: ");
    	scanf("%s", staff_name);
    
    	printf("What is the type of the staff member to be added: ");
    	scanf("%s", staff_type);
    
    	printf("What is your StaffID: ");
    	scanf("%s", security_ID);
    
    	strcpy(user_command, "1");
    	strcat(user_command, "/");
    
    	strcat(user_command, staff_ID);
    	strcat(user_command, "/");
    
    	strcat(user_command, staff_name);
    	strcat(user_command, "/");
    
    	strcat(user_command, staff_type);
    	strcat(user_command, "/");
    
    	strcat(user_command, security_ID);
    
    	printf("The full command is %s\n", user_command);
    
    
    	return user_command;
    }
    But the string "user_command" appears to be empty. And the printf doesn't print...

    Any ideas?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, which printf doesn't print?

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    "printf("The full command is %s\n", user_command);"

    Sorry I should have made that clearer.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Step 1: There had better be something in between the lines of code here:
    Code:
    char* user_command;
    
    user_command = get_user_command(user_command);
    where you actually create your string. Right now you have a pointer that could point to a string, if only you had one available for it to point to. Adding that actual creation seems to help:
    Code:
    $ ./str
    -=Security Station=-
    Add a User 	[1]
    Change Type 	[2]
    Change Location [3]
    Remove User 	[4]
    Choice: 1
    number = 1
    What is the StaffID of the staff member to be added: 2458
    What is the name of the staff member to be added: tabstop
    What is the type of the staff member to be added: E10
    What is your StaffID: 1367
    The full command is 1/2458/tabstop/E10/1367
    1/2458/tabstop/E10/1367

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    How would I do that, because I tried:

    Code:
    char* user_command;
    
    	char temp[256];
    
    	*user_command = temp;
    
    	user_command = get_user_command(user_command);
    And now it doesn't even ask me for the choice.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably you should avoid syntax errors, is my guess:
    Code:
    user_command = temp;

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    10
    Sorry, quite new to C, and I'm not fully clued up on pointers.

    Thanks a lot, fully functional now. Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-11-2008, 01:36 AM