...

View Full Version : C++ Data type conversion



Active X
10-04-2007, 01:12 PM
Hello to all of guys ,

I've got a question about C++ data type conversions .

I wanna convert an int variable to string or generally put it into a string variable . What should I do ?

How should I concatenate two strings in C++ to each other ? Is there any was except the strcat function ? I mean with something like an operator !?

Will be really thankful to your help

marek_mar
10-04-2007, 03:27 PM
To "convert" an int to a string you should use string streams. C++ strings can be concatenated using the + operator.


#include <iostream>
#include <string>
#include <sstream>

int main()
{
using namespace std;

int number = 46;
string my_string;

ostringstream my_string_stream;

my_string_stream << number;
my_string = my_string_stream.str();

// Concatenate.
my_string = "The number is " + my_string;

cout << my_string << endl;
}

Dunna
10-04-2007, 05:54 PM
#include <iostream>
#include <string>
#include <sstream>
#include <stdio>

int main()
{
using namespace std;

int number = 46;
string my_string;
char the_number_string[20] = "";

ostringstream my_string_stream;

my_string_stream << number;
my_string = my_string_stream.str();

// Concatenate.
sprintf(the_number_string, "%i", number);
my_string = "The number is " + the_number_string;

cout << my_string << endl;
}


Is that what you were looking for? Note: I typed my code in c++ non-.net so you have to make some slight modifications.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum