When I typed this code
#include <iostream>
#include <string>
class binary
{
std::string s;
public:
void read();
void check_format();
};
void binary::read()
{
std::cout << "Enter a number\n";
std::cin >> s;
}
void binary ::check_format()
{
for (int i = 1; i <= s.length(); i++)
{
if (s.at(i) != '0' && s.at(i) != '1')
{
std::cout << "Incorrect format\n";
exit(0);
}
}
};
int main()
{
binary num;
num.read();
num.check_format();
return 0;
}
I was getting the correct output for the oneswith no '1' and '0' in them like 44, but for the numbers with '1' and '0' in them I got this error
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 2) >= this->size() (which is 2)
Please help in fixing this.