How do i change an integer to a string?, C/C++ Programming

A: The simplest way is to use a stringstream:

#include

#include

#include

using namespace std;

string itos(int i) // convert int to string

{

stringstream s;

s << i;

return s.str();

}

int main()

{

int i = 127;

string ss = itos(i);

const char* p = ss.c_str();

cout << ss << " " << p << "\n";

}

Naturally, this technique works for converting any type that you can output using << to a string.

 

 

Posted Date: 3/20/2013 3:37:49 AM | Location : United States







Related Discussions:- How do i change an integer to a string?, Assignment Help, Ask Question on How do i change an integer to a string?, Get Answer, Expert's Help, How do i change an integer to a string? Discussions

Write discussion on How do i change an integer to a string?
Your posts are moderated
Related Questions
Four cells, each with an internal resistance of 0.40 ? and an e.m.f. of 2.5 V are connected in series to a load of 38.40 ?. (a) Verify the current flowing in the circuit and t

Just need the answers to the following 3 questions. "NO NEED to write a program compile or run it, do not need it. Just need the answers for the following questions. Example quest

#write a program that counts the number of occurances of the string in the n-th padovan string p(n)

What is copy constructor? describe the concept of copy constructor.

Linear search in array - C program: Write a program in c to define a linear search in array. void main()                 {                 clrscr();

#include stdio.h> #include conio.h> #include string.h>   void main() {           char a[50],b[25][25],l[25],temp1;           int i=0,j=0,k=0,l1[25],c=0,c1=0,t=



Define Bitwise Left Shift and Right Shift Operators: >? The bitwise shift operators shift their first operand right (>>) or left ( // Illustration of the bitwise right shi

Aim: To implement a program to swap two numbers using reference arguments. Code: void swap(int *a, int *b) {             int temp;             temp=*a;