Jump to content
  • Advertisement
Sign in to follow this  
darenking

SOLVED: C++, how do I convert a string to an int?

This topic is 4856 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

Hi, I'm reading text from a .txt file and storing the text in a vector of string objects. This works fine, but sometimes instead of text, the program will come across an integer. I want it to store the integers in a vector of ints instead of in the vector of strings. This means I have to convert the string to an int. This is a problem in itself. I could probably work out how to do it manually by converting each digit to ascii etc, but is there a better way? Also, how do I detect whether or not the string can be converted? The strings are read in like this:
	std::vector<std::string> commands;

	while ( ! file.eof() )
	{
		char next[256];
		file >> next;
		commands.push_back(next);
	}



(Also, I seem to be using both old C string arrays and C++ string objects - is that OK? Is there a simpler way?) [Edited by - darenking on August 2, 2005 2:11:56 AM]

Share this post


Link to post
Share on other sites
Advertisement
atoi string to int
itoa int to string
std::string s;
int i = atoi(s.c_str());

there are several other functions for other datatypes see the msdn

Share this post


Link to post
Share on other sites
This works for strings

string s = "something from input";

if(s.find_first_of("013423456789") != string::npos){
cout << "Number Found " << endl;
}
if(s.find_first_of("abcdefghijklmnopqrstuvwxyz") != string::npos){
cout << "Letter found" << endl;
}

If the thing you read in has a # and letter then both will be found.

Share this post


Link to post
Share on other sites
Quote:
Original post by darenking
(Also, I seem to be using both old C string arrays and C++ string objects - is that OK? Is there a simpler way?)


You could have a string object instead of the char-array.


std::vector<std::string> commands;

while ( ! file.eof() )
{
std::string next;
file >> next;
commands.push_back(next);
}





Edit: As for converting string to int, you could use stringstream.


#include <sstream>

while ( ! file.eof() )
{
std::string next;
file >> next;

std::stringstream ss(next);
int num;

if(ss >> num)
nums.push_back(num);
else
commands.push_back(next);
}



This is not failsafe. It will probably not give the result you want if the word read from the file is like "1a23", which will push back the '1' into nums and throw away the rest of the word.

[Edited by - Lajnold on August 1, 2005 4:57:53 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
boost::lexical_cast


Example:

#include <cassert>
#include <string>
#include <boost/lexical_cast.hpp>

using namespace boost;

int main () {
assert( lexical_cast< int >( "42" ) == 42 );
assert( lexical_cast< std::string >( 42 ) == "42" );
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Reading from a stream and using std::basic_ios::eof() as the loop condition is wrong. You will, possibly, read once too many times. Use the read operation such as std::getline() as your loop condition. See:

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5



// ville

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!