By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,776 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,776 IT Pros & Developers. It's quick & easy.

combine user input variable with normal variable

100+
P: 109
Hi I want to combine these variables:

char computera[10]; //this variable contains input from the user
char path[]= "\\\\";
char location[] = "\\C$\\Windows\\System32\\chat.txt";

The order I want them in is:

path[] + computera[10] + location[]

I have tried this to get the first two variables joined together:

strcat(path,computera);

However, when I tried to print this to check that it had worked, some weird symbols came up.

Any ideas?
Jun 20 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 256
Hi I want to combine these variables:

char computera[10]; //this variable contains input from the user
char path[]= "\\\\";
char location[] = "\\C$\\Windows\\System32\\chat.txt";

The order I want them in is:

path[] + computera[10] + location[]

I have tried this to get the first two variables joined together:

strcat(path,computera);

However, when I tried to print this to check that it had worked, some weird symbols came up.

Any ideas?
you have to create a buffer large enough to hold everything and then strcat/strcpy/whatever into that
Expand|Select|Wrap|Line Numbers
  1. char buff[1024] = {0}; //1024 should be big enough!
  2. ...
  3. ...
  4. strcpy( buff, path );
  5. strcat( buff, computera );
  6. strcat( buff, location )
  7.  
Jun 20 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This is for C:
char buff[1024] = {0}; //1024 should be big enough!
...
...
strcpy( buff, path );
strcat( buff, computera );
strcat( buff, location )
This is how it's done in C++:
Expand|Select|Wrap|Line Numbers
  1. string computera; //this variable contains input from the user
  2. string path("\\\\");
  3. stirng location("\\C$\\Windows\\System32\\chat.txt");
  4.  
  5. //The order I want them in is:
  6.  
  7. path= path + computera + location;
  8.  
  9. //and you are done.
  10.  
Jun 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.