When you have such a definition:
int variable = 253243243;
Can I somehow refer for example to the third digit in this number? Something along the lines of vector or array? I need this to compare whether a certain digit in the number corresponds to a different figure given by the user. Is it even possible?
You can extract digits with a combination of %
and /
operations.
Alternatively, you can print the number to a string
using stringstream
and extract digits as characters from the string:
std::stringstream ss;
ss << variable;
std::string s = ss.str();
unsigned char first = s[0] - '0'; // this is the first digit (from left)
unsigned char second = s[1] - '0'; // this is the second digit (from left)
Alternatively, if you are lucky enough to use a C++11 conforming compiler, you can use std::string::to_string function instead of std::stringstream
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments