2006-05-03, 15:44 | Link #1 |
Remember Law of Ueki
Join Date: Dec 2005
Location: Georgia, USA
|
Programmers!!!
I have been trying to learn C++ programming language on my own but this book seem a little out dated, I think. I want to know is there a certain method to convert one data type to another. Like Converting String = Int or Int= String. or Float to Int.
|
2006-05-03, 16:19 | Link #2 |
Give them the What For!
Fansubber
|
I pulled this from a faq
http://cpp.enisoc.com/faq/general/#itoa Q. How do I convert from string to int? A. The simplest way is to use the function int std::atoi(const char*) declared in cstdlib. For example, std::atoi("31337") returns 31337. Alternatively, you can use std::strtol() or std::strtoul(). These functions provide more functionality such as the ability to convert hexadecimal or octal number strings, and are also declared in cstdlib. » Q. How do I convert from int to string? A. Converting from int to string is a little more complicated than converting a string to int. There are no standard C++ functions for this, but there is a way to do it using standard stringstreams. The class stringstream is declared in header sstream and is used like this: std::stringstream ss; std::string str; ss << 31337; ss >> str; The variable str now contains "31337".
__________________
|
2006-05-04, 19:18 | Link #6 | |
...
Join Date: Dec 2005
|
Quote:
I think stdlib.h have that kind of functions. |
|
2006-05-05, 12:20 | Link #9 |
Senior Member
Join Date: Jan 2006
|
Maybe those books assume you are already proficient in the language itself.
The code provided by the books will definitely compile. Bad reputation for them if they provide errorenous code. Most programming languages are case-sensitive, so you may have to check each line of code that you don't miss out anything. |
2006-05-05, 12:52 | Link #10 | |
Member
Join Date: Apr 2006
|
Quote:
|
|
2006-05-05, 13:34 | Link #11 | |
Member
Join Date: May 2006
Location: Santa Barbara, CA
|
Quote:
Personally, I like C++ Primer Plus by Stephen Prata. Note that there's another book with a very similar title (C++ Primer by Stanley B. Lippman), but they're two different books! The latter seems to be decent, but I've not read it. Having said all that, I would really recommend you reading The C++ Programming Language by Bjarne Stroustrup. He's the guy who developed C++ in the beginning, so you can't get more authoritative than that! However, his book do assume you already know C or Pascal. For that I suggest The C Programming Language by Kernighan & Ritchie (they developed C). Yes I know this means you need to read two books instead of one, but I think you'll have a greater grasp of the language than you would otherwise.
__________________
|
|
2006-05-05, 15:30 | Link #12 | |
Remember Law of Ueki
Join Date: Dec 2005
Location: Georgia, USA
|
Quote:
edit: still havnt figured it out. I know its so simple but I don't understand how string is converted to int or int converted to string. I do know its very easy to do this in C#. Last edited by MarcusG; 2006-05-05 at 16:40. |
|
2006-05-05, 19:11 | Link #13 | |
Member
Join Date: May 2006
Location: Santa Barbara, CA
|
Quote:
Code:
#include <sstream> #include <iostream> int main() { using namespace std; int someInt = 42; string convertFromInt; string convertToInt = "78"; cout << "Before: \n"; cout << "someInt = " << someInt << "\n"; cout << "convertFromInt = \"" << convertFromInt << "\"\n"; cout << "convertToInt = \"" << convertToInt << "\"\n"; cout << endl; // Converts someInt to string and store it in convertFromInt ostringstream sout; sout << someInt; convertFromInt = sout.str(); // Converts convertToInt to int and store it in someInt istringstream sin(convertToInt); sin >> someInt; cout << "After: \n"; cout << "someInt = " << someInt << "\n"; cout << "convertFromInt = \"" << convertFromInt << "\"\n"; cout << "convertToInt = \"" << convertToInt << "\"\n"; cout << endl; return(0); } Code:
#include <cstdio> int main() { using namespace std; int someInt = 42; char convertFromInt[20] = ""; char convertToInt[] = "78"; // Converts someInt to string and store it in convertFromInt sprintf(convertFromInt, "%d", someInt); // Converts convertToInt to int and store it in someInt sscanf(convertToInt, "%d", &someInt); return(0); }
__________________
|
|
|
|