Sign in to follow this  
darenking

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

Recommended Posts

darenking    122
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
Basiror    241
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
Gink    100
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
Lajnold    226
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
Guest Anonymous Poster   
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this