#include <iostream>
usingnamespace std;
string to_hexa (int n) {
if (n != 0) {
int a = n%16;
if (a == 0) return to_hexa(n/16) + "0";
if (a == 1) return to_hexa(n/16) + "1";
if (a == 2) return to_hexa(n/16) + "2";
if (a == 3) return to_hexa(n/16) + "3";
if (a == 4) return to_hexa(n/16) + "4";
if (a == 5) return to_hexa(n/16) + "5";
if (a == 6) return to_hexa(n/16) + "6";
if (a == 7) return to_hexa(n/16) + "7";
if (a == 8) return to_hexa(n/16) + "8";
if (a == 9) return to_hexa(n/16) + "9";
if (a == 10) return to_hexa(n/16) + "A";
if (a == 11) return to_hexa(n/16) + "B";
if (a == 12) return to_hexa(n/16) + "C";
if (a == 13) return to_hexa(n/16) + "D";
if (a == 14) return to_hexa(n/16) + "E";
if (a == 15) return to_hexa(n/16) + "F";
}
return"";
}
string to_octal (int n) {
if (n != 0) {
int a = n%8;
if (a == 0) return to_octal(n/8) + "0";
if (a == 1) return to_octal(n/8) + "1";
if (a == 2) return to_octal(n/8) + "2";
if (a == 3) return to_octal(n/8) + "3";
if (a == 4) return to_octal(n/8) + "4";
if (a == 5) return to_octal(n/8) + "5";
if (a == 6) return to_octal(n/8) + "6";
if (a == 7) return to_octal(n/8) + "7";
return to_octal(n/8) + "8";
}
return"";
}
string to_binary (int n) {
if (n != 0) {
if (n%2 != 0) return to_binary(n/2) + "1";
return to_binary(n/2) + "0";
}
return"";
}
int main () {
int n;
while (cin >> n) {
if (n == 0) cout << "0 = 0, 0, 0" << endl;
else cout << n << " = " << to_binary(n) << ", " << to_octal(n) << ", " << to_hexa(n) << endl;
}
}
So you want to check if say, position 64 in a string exists? You could just get the size of the string and if the size is < x, then that position will not exist. If you want to then check what is actually held at that position, you can use at(x).
1 2 3 4
std::string s;
s = "this is a string"
std::cout << s.size();
std::cout << s.at(7);