Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 23rd, 2005, 2:14 AM   #1
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
A simple question

I know this might sound like a dumb question, but how do i convert a const char to string;
ex
string collect;
const char str[] = "convert";
collect = convertostring(str); //what function is available for converting it
thanks for the help
Master is offline   Reply With Quote
Old Dec 23rd, 2005, 2:25 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
#include <iostream>
#include <string>

using namespace std;

int main()
{
        string collect;
        const char str[] = "convert";
        collect = str;
        cout << str << endl;
        return 0;
}
This works for me.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 23rd, 2005, 1:19 PM   #3
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
thanks, how can i convert a string to int
such as
string num = "55"
Master is offline   Reply With Quote
Old Dec 23rd, 2005, 2:44 PM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
(atoi)num.c_str()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 23rd, 2005, 3:44 PM   #5
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
Thanks again ^_^
Master is offline   Reply With Quote
Old Dec 23rd, 2005, 4:52 PM   #6
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
std::string str = "123";
int i = atoi(str.c_str());
or
std::string str = "123";
int i;
sscanf(str.c_str(), "%i", &i);
or
#include <sstream>
...
std::string str = "123";
int i;

std::stringstream ss;
ss << str;
ss >> i;

atoi() is probably the easiest. There is also:
int atoi(const char *nptr);            /* convert string to int */
long atol(const char *nptr);           /* convert string to long */
long long atoll(const char *nptr);     /* convert string to long long */
para is offline   Reply With Quote
Old Dec 24th, 2005, 5:46 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
While atoi is easiest for this specific circumstance, using a stringstream, as detailed in para's third example, would be best, as you can do pretty much everything with it:
int i         = 41;
double d      = 2.194;
char c        = 'x';
std::string s = "meep";

std::stringstream ss;
ss << i << ' ' << d << ' ' << c << ' ' << s;
std::cout << ss.str();
This will print:
41 2.194 x meep
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 24th, 2005, 6:29 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
M'kay, I answered a completely different question. Do what para said.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Dec 24th, 2005, 7:04 AM   #9
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
lol Ooble not like you to make a mistake
HaCkeR is offline   Reply With Quote
Old Dec 24th, 2005, 2:20 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Au contraire, I always make at least one mistake. It's just that I usually notice it before the Edit buttons disappears.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC