Advertisement

Need help with C++

Started by March 13, 2000 06:58 AM
3 comments, last by Dark Star 24 years, 6 months ago
Hi everyone, I use DJGPP C++ for Dos and I was wondering id anyone knows if it has a function to convert numbers to strings so that they can be added to another string. I know that basic has a function called Str$() that does it. If you use it like this: print str$(66) it would print not the number but the string representation of the number. Thanks in advance Dark Star
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
That doesn''t look like C++ to me... more like basic. For C++, the best way I can think of to do it is like this. It takes care of both making a string out of the number and adding it to the other string.

#include stdlib.h // I think that is the right one

char Buffer[80];
int Counter = 5;

sprintf(Buffer, "The value of Counter is %d right now!", Counter);

look this up somewhere for all of the little %something placeholders... %d is for a signed short int.

--------------------


You are not a real programmer until you end all your sentences with semicolons;
www.trak.to/rdp

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
Thanks for the reply, but that sample code was a code from BASIC. that langauge don''t need ; at the end of statements. Thank you very much

---------------------------------------------You Only Live Once - Don't be afriad to take chances.
Using standard C++ library functions you can also create a strstream object and << the int to it.
ex:
strstream s;
int i = 66;
s << 66;
The function ''itoa()'' converts integers into strings.

Usage: (quoteth Borland c++ library reference)

Syntax
#include
char *itoa(int value, char *string, int radix);
wchar_t *_itow(int value, wchar_t *string, int radix);

Description

Converts an integer to a string.
itoa converts value to a null-terminated string and stores the result in string. With itoa, value is an integer.
radix specifies the base to be used in converting value; it must be between 2 and 36, inclusive. If value is negative and radix is 10, the first character of string is the minus sign (-).

Note: The space allocated for string must be large enough to hold the returned string, including the terminating null character (\0). itoa can return up to 17 bytes.

Return Value

itoa returns a pointer to string.

This topic is closed to new replies.

Advertisement