Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert integer to string

Status
Not open for further replies.

Maurader

Technical User
May 8, 2002
59
CA
How do i convert an integer to a string??

actually, i need to do

string line;

line = "G" + getIntVal() + getStringVal();
where getIntVal returns an integer and getStringVa() retrns a string

Thanks!
 
#include <stdio.h>
...
sprintf ( line, &quot;G%d%s&quot;, getIntVal ( ), getStringVal ( ));


Marcel
 
I get error

error C2664: 'sprintf' : cannot convert parameter 1 from 'const char *' to 'char *'

 
thx! doh! still doesn't work!


C:\ExtendPaths\NCParser.cpp(243) : error C2440: 'type cast' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
 
you can use one of this codes:

#include<stdio.h>
.....
...
char buffer[15];
int iValue;
sprintf( buffer,&quot;%d&quot;,iValue );

or

#include<stdlib.h>
.....
...
char buffer[15];
int iValue;
_itoa( iValue, buffer, 10 );
 
sprintf works with char arrays not strings.
sprintf( char * , char *format , ... );

string str

sprintf( str.c_str , &quot;%d&quot; , value ); dl_harmon@yahoo.com
 
Use the advantages of C++.

#include <sstream>

ostringstream ostr;

int int_var;
string str_var;

ostr << &quot;G&quot; << int_var << str_var;
cout << ostr.str();

// you can access the string as a char * string too
char *v = ostr.str().c_str();


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top