我无法访问二进制字符串的各个字符来确定它们是否已设置,我做错了什么?或者有没有更简单的方法?这是我的代码:
#include <iostream>
#include <string>
using namespace std;
float BinToDec(const string & bin) {
short length = bin.length();
float result = 1.0f;
const char * str = bin.c_str();
for (int i = 0; i < length; ++i) {
if ( &str[i] == "1") cout << "SET" << endl << endl;
else cout << "NOT SET" << endl << endl;
}
return result;
}
int main() {
string bin = "";
cout << "Input a binary number: ";
cin >> bin;
cout << BinToDec(bin) << endl << endl;
}