I wanna convert an integer to a character then i will store it later to the index of a c-string......

something like...
int res=123

then it will be putted in....
var[0] = 123
where in var is a cstring, and here 123 is now a char type.....


here is what ive tried...

#include<iostream>
using namespace std;
int main()
{
	int res=123;

	char buf[10];
	itoa(res,buf,10);

	cout<<"new char: "<<buf[0];


	system("pause>0");
}

what happened is, only the 1 in 123 is being converted into char and stored to the cstring........
is there a way to avoid it? is the itoa function that i used is wrong? i found something on the net where the integer will be converted into string...... but i cant put the value of this string to the index of the cstring...
help please

what i want to achieve is for the whole buff[0] will contain the 123 value..... because ill gonna use it later in the arithmetic in stacks so that i can separate them when i do for example addition...............

buff[0] + buff[1]
= 123 + 54

You are printing only first character of the array that is why you are only getting 1.
Because buff[0] = '1' , buff[1] = '2' and buff[3] ='3'

If you want "123" in one element of an array. Then has to be a string array or a multidimensional character array. Because char array will only store one character to its index.
hence you get buff[0] = '1'

However if you declare string buff[10], now instead of saving one character you will be saving the entire string to buff[0].

#include<iostream>
using namespace std;
int main()
{
	int res=123;

	char buf[10];
	itoa(res,buf,10);

	cout<<"new char: "<<buf;//this line


	system("pause>0");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.