1

I am having trouble accessing the individual characters of the binary string to do find out whether they are set or not, what am I doing wrong? Or is there an easier way? Here is my code:

#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; } 
2
  • @ Lightness Races in Orbit To me this wasnt worth a full answer as it was just a syntax error. PLUS the answers appeared when i hit enter, check timestamps. Commented Nov 4, 2012 at 21:33
  • @JonathanCruz: It's not worth a question as it was just a syntax error, but since here we are, that is the answer to the question and therefore should be and has been written as such! Commented Nov 4, 2012 at 21:34

4 Answers 4

3

You can iterate directly on your string bin, no need to get the const char *, also the & operator isn't needed here, since by using [] you're already dereferencing and getting a char (which is why you shouldn't compare it to "1" which is not a char but a string literal)

All in all, I think this would be a better approach:

for (int i = 0; i < length; ++i) { if ( bin[i] == '1') cout << "SET" << endl << endl; else cout << "NOT SET" << endl << endl; } 

Also, storing the length in a short might work now, but strings longer than the maximum value of short exist, so you should use size_t.

Sign up to request clarification or add additional context in comments.

Comments

2

It's not working for you because:

  • you're sort of trying to obtain a substring to compare with the string "1", but your sub-string will terminate at the end of the input... i.e. probably way past the 1 character.
  • comparing C-strings with == just compares pointer values

Instead, compare just individual characters:

if ( str[i] == '1') cout << "SET" << endl << endl; // ^ ^ ^ // | character literals are delimited by _single_ quotes // no `&` required 

But I don't understand why you're using .c_str() at all; just operate directly on bin instead of creating this C-string str:

float BinToDec(const string& bin) { size_t length = bin.length(); float result = 1.0f; for (int i = 0; i < length; ++i) { if (bin[i] == '1') cout << "SET" << endl << endl; else cout << "NOT SET" << endl << endl; } return result; } 

I've also corrected the type of length.

Comments

1

If you're sure you want to do this with C-style strings, change:

if ( &str[i] == "1") cout << "SET" << endl << endl; 

To

if ( str[i] == '1') cout << "SET" << endl << endl; 

That way you'll compare a single character of str with '1', a literal character (instead of "1" a string containing a 1 character.

You existing code is taking the address of offset i into the c_str(), which is effectively the same as the end of the string starting at character i, and comparing it with literal string "1". Note that you can't do C-style string comparison like this, since it'll compare the underlying pointers.

Comments

0

Since you are trying to check the value for each character, using single quotes and not double quotes.

float BinToDec(const string & bin) { short length = bin.length(); float result = 1.0f; const char * str = bin.c_str(); char c; for (int i = 0; i < length; ++i) { c = str[i]; // Use single quotes and not double quotes here if ( c == '1') cout << "SET" << endl << endl; else cout << "NOT SET" << endl << endl; } return result; } 

That said, I think lccarrasco's way is the right way to do what you are trying to achieve.

3 Comments

It's preferable for answers to explain a solution, providing code as helpful demonstration rather than sole content.
Added the explanation. Will follow the guidelines henceforth. Appreciate the feedback. Thanks.
Oh that's much better - ta :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.