• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple C++ Question

QurazyQuisp

Platinum Member
What is the easiest way to convert a string value to an integer?

So... assuming this...

string super = "1234567890";
int one = 0;

one = super.at(2); // line

How do I get it to equal the number 2?

The compilier does not like it when I do what I did at // line, as it is technically a character...

** I'd prefer not to use any functions inorder to do this, other then the member functions of string... unless this is not possible. **
 
well, if you only convert a character to int, as is the case with your example, you can simply subjectract '0' from it eg one = (int)(super.at(2) - '0');
but if you want to convert a whole string, you have to use external functions
you can either use something like atoi() eg one = atoi(super.c_str()); //atoi takes c-strings
or you can use a stringbuffer eg

std::stringstream buffer(super);
buffer >> one;

^actually I'm not sure if that works. I usually just use atoi()
 
Back
Top