Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Convert int to string

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?
0
Makover
Asked:
Makover
1 Solution
 
Chase707Commented:
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
}

Chase707
0
 
AlexFMCommented:
string s = "yes";
char s1[10];

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

0

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now