Simple newbie question:
I have String variables.
I have int variables.
I want to combine them into a String that I can output to a TextBox or Label.
How do I turn an int into a String? (talking about __gc Strings here)
Printable View
Simple newbie question:
I have String variables.
I have int variables.
I want to combine them into a String that I can output to a TextBox or Label.
How do I turn an int into a String? (talking about __gc Strings here)
Do you mean you wnat to display "The answer is 4" where 4 is a variable??
printf("The answer is %d", x);
Sort of, except I'm working with C++.net and I want to display the final String in the ->Text of a TextBox. It doesn't like it when I try to put anything other than a String (like the managed kind) in there.
You mean, convert a int into a char array? look into the functions itoa [integer to a] (dont ask me what a is), and atoi.
(e.g.)
I might have x and k backwards, but thats the idea. Now k has the string-version of x in it (the last parameter is the base for the conversion. I have it in base 10, 'cuz thats how most people count...). You can then use scrcat() to append it to another string, which should be what you're trying to do if I understand you right. Only warning is, make sure you make k big enough to hold all the digits!Code:char k[5];
int x = 20;
atoi(x, k, 10);
Probably the best way is to use sprintf.
Code:#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char new_string[100];
sprintf( new_string, "%s: %d", "An integer", 123 );
cout<< new_string <<endl;
return 0;
}
IMO the best way is to use stringstream.
Then the type-checking is done automatically compile-time, not run-time as with printf.
Code:#include <sstream>
using namespace std;
stringstream str;
str << "An integer : " << 123;
string myString = str.str();
Prelude posted something regarding atoi/atof, and the reasons why using strtod() and similar functions are a much better choice. You can do a search for it... I could explain it but it's explained nicely in the post.Quote:
Originally posted by Hunter2
You mean, convert a int into a char array? look into the functions itoa [integer to a] (dont ask me what a is), and atoi.
(e.g.)
I might have x and k backwards, but thats the idea. Now k has the string-version of x in it (the last parameter is the base for the conversion. I have it in base 10, 'cuz thats how most people count...). You can then use scrcat() to append it to another string, which should be what you're trying to do if I understand you right. Only warning is, make sure you make k big enough to hold all the digits!Code:char k[5];
int x = 20;
atoi(x, k, 10);
It's blind faith... anything she says I believe. (This goes for the other super-smart people too)
Hmm... Never heard of strtod(). Guess I'll look into it though, since you mentioned it :) I only told what I knew, when I said atoi()...