Strings and Char

I have recently began programming and am having some difficulty resolving this problem:

The aim is to take in a binary number from a user, store it in a string then check each position to make sure it is only 1's 0r 0's entered. I cant seem to find a way to compare a variable containing either 1 or 0 to the value at the specified string position. Here is my code so far:

--------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string binary;
    int length; //string length
    int i; //position in string
    char a; //Value of binary[i]
    char ones[] = "1";
    char zeros[] ="0";

    //gets string and displays its length
    cout << "please enter a binary number:" << endl;
    cin >> binary;
    length = binary.length();
    cout << "the number is " << length << " characters long."<< endl;
    //end of string length

    //To check the binary value
    for(i = 0; i < binary.length(); i++) {
        a = binary[i];
        if (a == zeros) {
        cout << "one" << endl;
        }
        else if (a == ones){
        cout << "two" << endl;
        }
        else {
        cout << "three" << endl;}
        }
        //end of string input check

    }

--------------------------------------------------------------------------------

I have done quite a bit of reading trying to resolve this (I cant ask someone who would know just now either) but cant seem to find a specific answer. The outcomes from the IF statements are just for testing purposes just now, they will be altered when i can get the if's to work correctly.

Really appreciate any input, Thanks.
Last edited on
Please edit your post and put your code inside [code] tags.
You seem to be comparing a char to a char array?
I seem to have it now, heres what i have for anyone interested:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string binary;
    int i; //position in string
    char a; //Value of binary[i]
    int incorrect = 0;

    //gets string
    cout << "please enter a binary number:" << endl;
    cin >> binary;
    //end of string length

    //To check the binary value
    for(i = 0; i < binary.length(); i++) {
        a = binary[i];
        if (a == '0') {
        cout << ".";
        }
        else if (a == '1'){
        cout <<".";
        }
        else {
            cout << "x";
        incorrect ++;}
        }
        //end of string input check

    if (incorrect == 0) {
    cout << endl << endl << "the input was in the correct format" << endl;
    }
    else {
    cout << endl << endl << "you entered " << incorrect << " incorrect numbers" << endl;
    }
    }


I wanted to use variables which stored either 1 or 0 to compare to 'char a' during the if statements as we were told its good practice to use variables that can be called again instead of just a number/character. Any input on how this may be done would be appreciated so i can maybe try it that way to.

Cheers.
Yeh, what I said, if you defined zeroes and ones as:

1
2
ones = '1';
zeroes = '0';


You would be able to use the variables to compare. You were defining them as arrays, which is a different object type, so would never be equal.

I don't see why this would be "good practice", unless you were calling these variables in multiple places. Having them have a page of code up actually makes it less readable, as you don't know what you're actually comparing to, and there's always the chance the variable could be changed to something else.
Last edited on
Good practice would be to use the STL functions since you already have a string...
The String library has some great functions for checking validity of inputs... Have you tried something like this?

1
2
3
4
5
6
7
8
9
10
cin >> binary;

if (binary.find_first_not_of("01") == string::npos)
{
  // We're all good!
}
else
{
  // We've got a problem here...
}
Last edited on
Topic archived. No new replies allowed.