Reputation: 23
i'm having an issue converting an int into a char. Code looks like :
int main {
int i = 10;
char c[80];
std::vector<char> data;
sprintf(i,"%d",c);
data.push_back(c)
}
But I keep getting a invalid conversion from char* to std::vector ... error. Is there an easier way to convert an integer into a character and then store it inside a vector that holds chars? Because of an earlier task I need to first bring in the value as an int and I need to bring that integer 10 into the vector as '10'.
Upvotes: 0
Views: 1443
Reputation: 310980
For starters it seems there is a typo
sprintf(text,"%d",f);
You nean
sprintf( c ,"%d", i );
The value type of this vector
std::vector<char> data;
is char
. So in the member function push you have to supply an expression that has the type char
. However in this call
data.push_back(c);
you supplied an object of an array type
char c[80];
If you want to store in the vector a character representation of a number as separate characters then you can write for example
size_t n = strlen( c );
data.reserve( n );
data.assign( c, c + n );
Or you could declare the vector initializing it by the representation of the number like
std::vector<char> data( c, c + n );
If you want to store the whole number as one element of the vector then you should declare the vector like
std::vector<std::string> data( 1, std::to_string( i ) );
Upvotes: 3