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?