Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

stealth

String -> Int

This topic is 5885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering u see I am reading strings froma file , I was wondering how do I convert the string to an int if the string represents an int?? I know its hard ot understand, but I think this is kinda easy OI just dunno how to do it say I had a string "100" how do I turn that into an INT of 100?? thanks to anyone who can help Cheers Stealth

Share this post


Link to post
Share on other sites
Advertisement
Why instead of loading the number into string you load it into an int?

int n;
ifstream file("file.txt");
file >> n;



[edited by - Ragadast on June 4, 2002 11:50:07 PM]

Share this post


Link to post
Share on other sites
i havent used them in a long time, so youll have to look up the syntax, but the functions itoa, atoi, ftoa, and atof will do what you want.

Share this post


Link to post
Share on other sites

      
#include <string>

#include <sstream>


int n;
std::string str = "100";

std::istringstream is( str );
is >> n;


Or download the boost library, and use boost::lexical_cast


  
#include <string>

#include <boost/lexical_cast.hpp>


int n;
std::string str = "100";

n = boost::lexical_cast<int>( str );
str = boost::lexical_cast<std::string>( 10000 );


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 4, 2002 11:48:02 PM]

[edited by - Fruny on June 4, 2002 11:48:16 PM]

Share this post


Link to post
Share on other sites
quote:
"int n;
ifstream file("file.txt");
file >> n;


I don''t think that will work. Characters in text files are stored with ASCII encoding or something. For example, I believe you will get a 48 if you read in a zero from a text file. Atof, atoi, and that family, however, will do the job; I recently had to use them to convert input from a string to an int for a calculator program I had to write at school.

Share this post


Link to post
Share on other sites
If you're using C, you can do:


      
char * IntReadIn = "4242";
int Converted;
sscanf(IntReadIn, "%d", &Converted);


/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on June 4, 2002 12:50:56 AM]

[edited by - Chem0sh on June 4, 2002 12:51:14 AM]

Share this post


Link to post
Share on other sites
You may also consider storing your numbers in binary. It''s much more efficient than using ASCII text, although you won''t be able to edit them in a normal text editor. Look into the "read" and "write" methods of ifstream and ofstream. Make sure you open the file in ios::binary mode, too.

Share this post


Link to post
Share on other sites
`Thanks for the help but I am even more confused

what I am actually doing is reading the string from a fiel which means I CANT store as anything else but that , the little project I am working on is something like an interpreter , so therefore it has to take strings from a file , strings which will reperesent ints , so something like


string MyString;
MyString = "100";

I have to change this to an int to do something like:

string MyString;
MyString = "100";

MyInt = MyString(converted to int of course);

NewInt = 300 - MyInt;




something like that


, this stuff is starting to get me confused lol , I ACTUALLY want to take the Number which WILL be a number represented by a string , so it HAS to be stored as a string but it WILL be a number , so can some one plz explain ina little more of an understandable way?


thanks to all those who helped and I''ll continue to try and understand the help I have been given

cheers


Stealth

Share this post


Link to post
Share on other sites
Hm, even based on this clarification, it still sounds like what you need is the atoi faimily. For example...

(Psuedo code)

- Read value you from file (it will be a string)
- Convert to int use atoi()
- Do necessary operations using ints
- Write to file (it will become a string again)

Is this what you''re trying to do?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!