AnimeSuki Forums

Register Forum Rules FAQ Community Today's Posts Search

Go Back   AnimeSuki Forum > Support > Tech Support

Notices

Reply
 
Thread Tools
Old 2006-05-03, 15:44   Link #1
MarcusG
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.
MarcusG is offline   Reply With Quote
Old 2006-05-03, 16:19   Link #2
[darkfire]
Give them the What For!
*Fansubber
 
 
Join Date: Feb 2006
Location: Cave of Evil- Invite Only
Age: 36
Send a message via AIM to [darkfire] Send a message via Yahoo to [darkfire]
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".
__________________
"Lepers, women are Lepers" - Sheriff of Nottingham
[darkfire] is offline   Reply With Quote
Old 2006-05-03, 20:36   Link #3
MarcusG
Remember Law of Ueki
 
 
Join Date: Dec 2005
Location: Georgia, USA
Thanks for your help. I wonder how many programmers we have on this forum.
MarcusG is offline   Reply With Quote
Old 2006-05-04, 11:04   Link #4
mr.muffin
Member
 
 
Join Date: Apr 2006
I'm a programmer.
mr.muffin is offline   Reply With Quote
Old 2006-05-04, 12:05   Link #5
RaistlinMajere
Now in MHD!
*Fansubber
 
 
Join Date: Dec 2003
I wish I knew how to program :/
RaistlinMajere is offline   Reply With Quote
Old 2006-05-04, 19:18   Link #6
lavielove
...
 
 
Join Date: Dec 2005
Quote:
Originally Posted by MarcusG
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.
http://www.cplusplus.com/ref/

I think stdlib.h have that kind of functions.
lavielove is offline   Reply With Quote
Old 2006-05-05, 11:44   Link #7
shiro83
Senior Member
 
Join Date: Jan 2006
Quote:
Originally Posted by mr.muffin
I'm a programmer.
Me too.
But I prefer Java.
shiro83 is offline   Reply With Quote
Old 2006-05-05, 12:06   Link #8
MarcusG
Remember Law of Ueki
 
 
Join Date: Dec 2005
Location: Georgia, USA
why is it so boring to learn programming and so hard to understand at first? There are books out there that give you crap examples and codes that don't even compile.
MarcusG is offline   Reply With Quote
Old 2006-05-05, 12:20   Link #9
shiro83
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.
shiro83 is offline   Reply With Quote
Old 2006-05-05, 12:52   Link #10
mr.muffin
Member
 
 
Join Date: Apr 2006
Quote:
Originally Posted by MarcusG
why is it so boring to learn programming and so hard to understand at first? There are books out there that give you crap examples and codes that don't even compile.
I guess you have to have an interest in programming to not find it boring.
mr.muffin is offline   Reply With Quote
Old 2006-05-05, 13:34   Link #11
052569
Member
 
 
Join Date: May 2006
Location: Santa Barbara, CA
Quote:
Originally Posted by MarcusG
why is it so boring to learn programming and so hard to understand at first? There are books out there that give you crap examples and codes that don't even compile.
I agree that the market is flooded with horribly written computer books, C++ books included. Almost everyone I know recommend C++ How to Program by Deitel & Associates. Ironically, I didn't like the book very much, but I'm going to assume that I'm just an outlier.

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.
__________________
When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute--and it’s longer than any hour. That’s relativity.
-- Albert Einstein
052569 is offline   Reply With Quote
Old 2006-05-05, 15:30   Link #12
MarcusG
Remember Law of Ueki
 
 
Join Date: Dec 2005
Location: Georgia, USA
Quote:
Originally Posted by 052569
I agree that the market is flooded with horribly written computer books, C++ books included. Almost everyone I know recommend C++ How to Program by Deitel & Associates. Ironically, I didn't like the book very much, but I'm going to assume that I'm just an outlier.
Yes, I think that book is horrible in my opinion, but it does give you some good exercises.
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.
MarcusG is offline   Reply With Quote
Old 2006-05-05, 19:11   Link #13
052569
Member
 
 
Join Date: May 2006
Location: Santa Barbara, CA
Quote:
Originally Posted by MarcusG
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#.
There are many ways to do it. The following is essentially an expanded version of what darkfire said:

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);
}
Another way is to use sscanf/sprintf, but it's more dangerous:

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);
}
In this version, you must be absolutely certain that you're passing valid parameters to sscanf/sprintf, and that convertFromInt has enough space to store the converted string. Countless exploits and security vulnerabilities are due to careless use of sscanf/sprintf.
__________________
When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute--and it’s longer than any hour. That’s relativity.
-- Albert Einstein
052569 is offline   Reply With Quote
Old 2006-05-06, 14:41   Link #14
MarcusG
Remember Law of Ueki
 
 
Join Date: Dec 2005
Location: Georgia, USA
i think i understand but....

Last edited by MarcusG; 2006-05-08 at 21:06.
MarcusG is offline   Reply With Quote
Reply


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


All times are GMT -5. The time now is 23:10.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
We use Silk.