Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Int to String (http://www.programmingforums.org/showthread.php?t=10560)

crawforddavid2006 Jun 28th, 2006 5:54 PM

Int to String
 
How do I turn an in into a string in C++?

Klarre Jun 28th, 2006 6:09 PM

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

titaniumdecoy Jun 28th, 2006 7:17 PM

You can use atoi(string s) if it is available on your system.

DaWei Jun 28th, 2006 7:18 PM

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.

Seif Jun 28th, 2006 8:02 PM

Quote:

Originally Posted by DaWei
the average number of fingers on two hands.

That the average number of fingers on mutant hands? :confused:

mrynit Jun 28th, 2006 8:30 PM

make sure its in the ascii range, or what ever system your using. look for the bounds.

Jessehk Jun 28th, 2006 9:08 PM

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;
    }
}


crawforddavid2006 Jun 28th, 2006 10:31 PM

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"

Prm753 Jun 28th, 2006 10:54 PM

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++.

angry_asian Jun 28th, 2006 11:10 PM

is that C prm753?


All times are GMT -5. The time now is 8:07 AM.

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