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

Started by
6 comments, last by GameDev.net 18 years, 8 months ago
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]
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
http://www.8ung.at/basiror/theironcross.html
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.
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]
There's...
std::stringstream
atoi
boost::lexical_cast

Off the top of my head.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
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" );}
Thanks all! I now have got it all working fine.
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

This topic is closed to new replies.

Advertisement