Sign in to follow this  

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

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

This topic is 4520 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.

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