Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2006, 4:54 PM   #1
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Int to String

How do I turn an in into a string in C++?
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Jun 28th, 2006, 5:09 PM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 296
Rep Power: 4 Klarre is on a distinguished road
Try this function to convert from int to std::string.
#include <sstream>

template <class T>
std::string stringConverter(const T &value)
{
	std::stringstream stream;
	stream << value;

	return stream.str();
}

/Klarre
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old Jun 28th, 2006, 6:17 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You can use atoi(string s) if it is available on your system.
titaniumdecoy is offline   Reply With Quote
Old Jun 28th, 2006, 6:18 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Klarre has a good answer, but consider this: suppose you have the absolute value of 10 (decimal, the average number of fingers on two hands, assuming you don't mess around carelessly with a table saw). What is a string representing that? "10"? "Ten"? "diez"? "umpty-zillion"? You are in an area of (hopefully, somewhat cooperative) representation. There is no hard and fast answer.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 28th, 2006, 7:02 PM   #5
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3 Seif is on a distinguished road
Quote:
Originally Posted by DaWei
the average number of fingers on two hands.
That the average number of fingers on mutant hands?
Seif is offline   Reply With Quote
Old Jun 28th, 2006, 7:30 PM   #6
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
make sure its in the ascii range, or what ever system your using. look for the bounds.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Jun 28th, 2006, 8:08 PM   #7
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 639
Rep Power: 4 Jessehk is on a distinguished road
Just to be helpful (and to learn it myself), I though I'd devise the reverse function.

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

class ParseError : public std::invalid_argument {
    public:
        ParseError(const std::string &msg)
            : std::invalid_argument(msg) {
        }
};

template<typename T>
T parse_string(const std::string &input) throw(ParseError) {
    std::istringstream source(input);

    T result;
    if(!(source >> result))
        throw ParseError("Unable to retrieve data from input string.");
    else
        return result;
}

int main() {
    try {
        int n1 = parse_string<int>("3412");
        std::cout << n1 << std::endl;

        float n2 = parse_string<float>("3.14159");
        std::cout << n2 << std::endl;

        //should raise ParseError
        int n3 = parse_string<int>("abc");
        std::cout << n3 << std::endl;

    } catch(ParseError &pe) {
        std::cout << pe.what() << std::endl;
    }
}
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jun 28th, 2006, 9:31 PM   #8
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 579
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Well what i want is a loop that will turn the index number of that for loop into the string... ex. 10 = "10" not "Ten"
__________________
Quote:
Originally Posted by DaWei View Post
Well, it's better than Pen Islands url....;)

crawforddavid2006 is offline   Reply With Quote
Old Jun 28th, 2006, 9:54 PM   #9
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 3 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Slightly-off-topic alert: This is not 100% C++, but it is an intresting function that I came across yesterday:
#include <iostream> 
#include <stdio.h>
int main() 
{
    char string[80];
    int number = 10;
    sprintf(string, // Char to which you want to put data in,
    "%i", // your string data, with the int specifed,
    number); // and of course, your int.
    std::cout << string << std::endl; 
    std::cin.get();
    return 0;
}

I wish it was that easy in C++.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply

Last edited by Prm753; Jun 28th, 2006 at 10:12 PM.
Prm753 is offline   Reply With Quote
Old Jun 28th, 2006, 10:10 PM   #10
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
is that C prm753?
angry_asian 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 4:35 PM.

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