Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Dark Star

Need help with C++

This topic is 6796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!