473,511 Members | 10,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char* combination problem :(

x
Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing....

char *x = (int1 + "/" + int2 + "/" + int3);

whats wrong? int1-3 can equal 0-99...












6e
Jul 22 '05 #1
5 1764
x <ao****@hotmail.com> spoke thus:
Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing.... char *x = (int1 + "/" + int2 + "/" + int3); whats wrong? int1-3 can equal 0-99...
Simple - you forgot what language you were using. + isn't a catch-all
string creation operator in C++ like it is in various other languages.
If you want a string, declare a string. A decent C++ book and the FAQ
(posted below) will be most helpful.

http://www.slack.net/~shiva/welcome.txt
http://www.parashift.com/c++-faq-lite/











6e


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2
x wrote:
Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing....

char *x = (int1 + "/" + int2 + "/" + int3);

whats wrong?


Doesn't make much sense to add integers and pointers together and write
the result into a pointer. Rembember: char* is not a string, but a
pointer to char. Try:

std::stringstream stream;
stream << int1 << "/" << int2 << "/" << int3;
std::string x = stream.str();

Jul 22 '05 #3
x
Rolf Magnus <ra******@t-online.de> wrote in message news:<c5*************@news.t-online.com>...
x wrote:
Im trying to convert a few variables into a string object ( a
character string object). Only I have forgotten how! how
embarrasing....

char *x = (int1 + "/" + int2 + "/" + int3);

whats wrong?


Doesn't make much sense to add integers and pointers together and write
the result into a pointer. Rembember: char* is not a string, but a
pointer to char. Try:

std::stringstream stream;
stream << int1 << "/" << int2 << "/" << int3;
std::string x = stream.str();

the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......
Jul 22 '05 #4
x <ao****@hotmail.com> spoke thus:
the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......


Still easy, if const char* is acceptable - std::string's c_str()
method converts the string to an array of characters, C-style. If you
need a non-const char *, then you'll have to declare an array of
characters and populate it appropriately (I suggest sprintf):

char buf[256]; // larger than required, just make sure it's big enough
// as sprintf will happily overflow the buffer if it
// isn't

sprintf( buf, "%d/%d/%d", int1, int2, int3 );

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #5
x wrote:
the only problem with that is that the function I am tryin to use the
string of characters with requires an input of char*......


You can extract a char* from a string. See the functions in the string
class.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2790
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes...
9
387
by: x | last post by:
Im trying to convert a few variables into a string object ( a character string object). Only I have forgotten how! how embarrasing.... char *x = (int1 + "/" + int2 + "/" + int3); whats...
5
2247
by: SinusX | last post by:
Hello I creating program which is genereting file names. I have to create char* as a file name. Name of file looks like: char + unsigned short . char (eg. test12.tmp) How to conect char with...
9
2996
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have...
28
2671
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
5
7769
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
26
11611
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
12
3546
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
9
469
by: Ioannis Vranos | last post by:
Under C++03: Is it guaranteed that char, unsigned char, signed char have no padding bits?
17
2163
by: mlt | last post by:
I have a function that returns a char*. But how do I print the whole string that the char* points to? I don't know at forehand how long the string is.
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.