バイナリ文字列の個々の文字にアクセスして、それらが設定されているかどうかを確認するのに問題があります。何が間違っていますか? それとももっと簡単な方法がありますか?これが私のコードです:
#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;
}