Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,443 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,311 people online right now. Registration is fast and FREE... Join Now!




concatenation of variables

 
Reply to this topicStart new topic

concatenation of variables

tootypegs
7 Jan, 2008 - 11:55 AM
Post #1

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
Hi i have 3 unsigned char's saved which put together contain 'D' 'O' 'C'. Is there a way to join the unsigned char's together into 1 string so that i can have the string equal to 'doc'?



thanks
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Concatenation Of Variables
7 Jan, 2008 - 12:10 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,459



Thanked: 10 times
Dream Kudos: 325
My Contributions
If you put them into an array and add a '\0' at the end, you'll have a string.
User is offlineProfile CardPM
+Quote Post

tootypegs
RE: Concatenation Of Variables
7 Jan, 2008 - 12:24 PM
Post #3

D.I.C Head
**

Joined: 9 Oct, 2007
Posts: 177


My Contributions
thanks!!!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Concatenation Of Variables
7 Jan, 2008 - 12:25 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 39 times
Dream Kudos: 25
My Contributions
Several ways to do it - using strcat()/strcpy(), oer even as tom suggests, simply declare an array and place them in there,element by element.

http://irc.essex.ac.uk/www.iota-six.co.uk/...cat_strncat.asp

User is offlineProfile CardPM
+Quote Post

CruorAvis
RE: Concatenation Of Variables
2 Aug, 2008 - 01:06 AM
Post #5

New D.I.C Head
*

Joined: 2 Aug, 2008
Posts: 17

QUOTE(Amadeus @ 7 Jan, 2008 - 01:25 PM) *

Several ways to do it - using strcat()/strcpy(), oer even as tom suggests, simply declare an array and place them in there,element by element.

http://irc.essex.ac.uk/www.iota-six.co.uk/...cat_strncat.asp


Thank you I'm new to C++ and am using a rather outdated book 'A Weekend Crash Course in C++' though I've tried several websites and of course the help files in Visual C++ i still couldn't figure it out. I did have to follow a 'Warning' Code which suggested i use strcat_s() instead though. Thank you very much again, i just spent an hour trying to figure this out!

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Concatenation Of Variables
2 Aug, 2008 - 03:35 AM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
If you're working with C++ you could do this:
cpp
#include <string> // string header
using std::string; // or using namespace std;
//. . .
char a = 'D';
char b = 'O';
char c = 'C';
// now we use a STRING datatype to store it
string abc = a + b + c;

User is online!Profile CardPM
+Quote Post

fountainoftruth
RE: Concatenation Of Variables
2 Aug, 2008 - 10:08 AM
Post #7

D.I.C Head
Group Icon

Joined: 4 Dec, 2007
Posts: 73


My Contributions
QUOTE(gabehabe @ 2 Aug, 2008 - 04:35 AM) *

If you're working with C++ you could do this:
cpp
#include <string> // string header
using std::string; // or using namespace std;
//. . .
char a = 'D';
char b = 'O';
char c = 'C';
// now we use a STRING datatype to store it
string abc = a + b + c;



I was about to suggest trying that because I remember my professor saying you can add strings easily in C++. The simplest way. smile.gif
User is online!Profile CardPM
+Quote Post

Bench
RE: Concatenation Of Variables
3 Aug, 2008 - 04:39 AM
Post #8

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 621



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(gabehabe @ 2 Aug, 2008 - 12:35 PM) *

If you're working with C++ you could do this:
cpp
#include <string> // string header
using std::string; // or using namespace std;
//. . .
char a = 'D';
char b = 'O';
char c = 'C';
// now we use a STRING datatype to store it
string abc = a + b + c;


Except that char is merely an integral type, so that will give a compiler error for attempting to assign a numerical value to a string

On the other hand, you could do this
cpp
char a = 'D';
char b = 'O';
char c = 'C';

string abc = string() + a + b + c;
the std::basic_string type provides an overloaded operator for char, to concatenate a single char to the end of a string. In this case, you're concatenating char a to the end of a temporary, empty string, which then allows the subsequent characters to be added in the same manner
User is online!Profile CardPM
+Quote Post

gabehabe
RE: Concatenation Of Variables
3 Aug, 2008 - 05:29 AM
Post #9

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,539



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Good catch icon_up.gif
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 02:14PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month