English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hey all,
I'm programming in c++ windows forms apps.
I'm very new and have been searching for 2 days now but i can't seem to find a way that works.

I need to convert my int to a string to put it into a label control.
Also i'm still a little confused with namespaces and stuff so if you could make that clear that would help alot.

thanks.

2007-03-05 15:55:52 · 6 answers · asked by ronin.stretch 1 in Computers & Internet Programming & Design

yes i looked at itoa() but i can't get my head round it in all the examples.. i havnt used printf so... I havn't used char's much either.

What i want is a simple function that will take an int variable and return a string..
So i can do say...

Form1::textbox1->Text = ConverIntToString( thisInt)

even if i have to make that function.. i need to convert lots of int's to strings to display them.

thanks for your help

2007-03-05 16:50:48 · update #1

Thank you all for your help i think i have the jist of it now.

Also id like to say i am learning C++ i'm just learning alone. Moving through a basic project and looking up everything i need on the way. Just sometimes i get stuck.. I do understand namespaces, i meant i just get lost with them.

2007-03-05 17:04:28 · update #2

6 answers

Converting int to string:
While Amanda's and Sridhar's suggestion to use itoa is an option, it is the *worst* option available in C++. itoa is non-portable. You have the options of stringstreams (C++) as well as the C option of sprintf. Both are preferable to itoa.

C++:
#include
#include
.
.
.
stringstream ss;
int i;
ss << i;
string s = ss.str(); //Gets you a C++ STL string

C:
#include
.
.
.
int i;
char buf[50];
sprintf(buf, "%d", i); //Gets you a C string

Namespaces are a way of assigning some category to related code: classes, structs, enums, functions, etc. For example, cout is in the namespace std. You can have your own cout in your own namespace. Using namespaces prevents name clashes, so you don't have to ridiculous things like void companyname_projectname_functionname(); Well, namespaces are more powerful than that, but you get the idea.

These are basic C++ topics. I suggest you learn C++ first before jumping into windows forms and GUI programming.

2007-03-05 16:57:10 · answer #1 · answered by csanon 6 · 1 1

Converting from int to string is a little more complicated than converting a string to int. There are no standard C++ functions for this, but there is a way to do it using standard stringstreams. The class stringstream is declared in header sstream and is used like this:

std::stringstream ss;
std::string str;
ss << 31337;
ss >> str;

The variable str now contains "31337".

2007-03-05 16:55:12 · answer #2 · answered by ehossain 2 · 1 0

U could desire to parse the String to int.... use parseInt() function for that ie. int z=parseInt(x); if it doen't artwork attempt Integer.ParseInt() i in simple terms puzzled with VB and c++ at the instant.. or in simple terms refer books to parse string as int or google it.

2016-10-17 09:13:53 · answer #3 · answered by ? 4 · 0 0

use itoa() is the easiest

int number;
char ch[10];

itoa(number, ch, 10);
//now the string "ch" contains the integer as a string

2007-03-06 01:11:35 · answer #4 · answered by justme 7 · 1 0

I think that there is funtion in stdlib.h that is itoa(). It return the char pointer and it is reverse case of atoi(). Try that and reply me.

2007-03-05 16:02:11 · answer #5 · answered by Anonymous · 1 0

Couple of handy functions... atoi and itoa

Read the link below for an example.

2007-03-05 15:59:08 · answer #6 · answered by Amanda H 6 · 1 0

fedest.com, questions and answers