![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Int to String
How do I turn an in into a string in C++?
__________________
|
|
|
|
|
|
#2 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 296
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
|
You can use atoi(string s) if it is available on your system.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 214
Rep Power: 3
![]() |
Quote:
![]() |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#7 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 639
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#8 |
|
Expert Programmer
|
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"
__________________
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
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. |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
is that C prm753?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|