Convert int to string

Posted on 2003-03-18
Medium Priority
Last Modified: 2012-05-04
How can I convert an int variable to string? The following code outputs "Yesb" instead of "Yes98":

string s = "Yes" ;
int a = 98 ;
s += a ;

cout << s << endl ;

Is there a way to do it without using CString?
Question by:Makover
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Chase707 earned 120 total points
ID: 8159590
Yes, using the ostringstream class:

static std::string ToString(int i)
    std::ostringstream oss; // create a stream
    oss << i; // insert value to stream
    return oss.str(); // return the string

LVL 48

Expert Comment

ID: 8159617
string s = "yes";
char s1[10];

sprintf(s1, "%d", 98);
s += s1;


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question