Binary checker

Pages: 12
Can someone please tell me whats wrong with my checker? I copied and pasted some parts of my code, where I use this checker but it seems that there is something wrong as if I enter 111111111111111 it comes as an error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for ( int k = 0; k < n; k++)
 {
      cout << "Please enter the Binary number: ";
      cin >> A[n]; //storing binary values in an array
      if (!checkbinary(A[n])) 
      cout << "ERROR: NUMBER NOT A BINARY NUMBER; PLEASE RE-ENTER: ";
      cin >> A[n]; 
  }
bool checkbinary(long bin)
{
    int d; 
    while (bin > 0)
    {
        d = bin % 10; 
        if ( (d != 1) || (d !=0) )
        return false;
        bin/= 10;
    }
    return true;
}

Any help, please?
Last edited on
I'm assuming A is an array of int. Is that correct?

If it is correct, then:
111 billion is too large for 32-bits, which your ints probably are.
If your machine is 64-bits, then long is probably 64-bits.
So either make A long, or better yet, just read the binary value into a string.
A is already longed
I will show you my program, but let me explain its purpose: it is a program that takes n binary values and checks them and gives feedback then prints all binary values in a table and converts them to decimal
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iomanip> 
#include <iostream>
#include <string>
using namespace std;
bool checkbinary(long);
int convertBtoD(long); 
void drawline(char);
void header (); 
void body(long);
int main()
{
  int n;    // taking inputs
  long A[20], bin;  // saving a place for an entire array of maximum 20 binary values
  cout << "Please enter the number of binary values desired: ";
  cin >> n; 
  while ( (n < 0) || (n > 20) )
  { 
      cout << "ERROR: THE NUMBER ENTERED IS IN-VALID; PLEASE RE-ENTER: ";
      cin >> n; 
      cout << endl; 
  }
  for ( int k = 0; k < n; k++)
  {
      cout << "Please enter the Binary number: ";
      cin >> A[n];                            //entering the binary value
      if (!checkbinary(A[n])           //checking the binary value
      cout << "ERROR: NUMBER NOT A BINARY NUMBER; PLEASE RE-ENTER: ";
      cin >> A[n]; 
  }
  drawline ('#');
  header(); 
  drawline ('-');
    for (int c = 0; c <= n; c++)
    {
        bin = A[n];
        body(bin);
    }
  drawline ('#');
  system ("pause");
}
void drawline(char $)
{
    for (int c = 1; c<=30; c++)
    cout << $;
    cout << endl; 
}
void header()
{
    cout << setw(15) << "Binary" << setw(15) << "Decimal" << endl; 
}
void body(long bin)
{
    cout << setw(15) << bin << setw(15) << convertBtoD(bin) << endl; 
    return;
}
bool checkbinary(long bin)
{
    int d; 
    while (bin > 0)
    {
        d = bin % 10; 
        if ( (d != 1) || (d !=0) )
        return false;
        bin/= 10;
    }
    return true;
}
int convertBtoD (long num)
{
    int d = 0, w = 1; 
    while (num > 0)
    {
        d = d + (num % 10) * w; 
        num/=10;
        w = w * 2;
    }
    return d; 
}


The output should look like this:
######################################
                  binary                            decimal
---------------------------------------------------------------------
                  11111                                    their equivalent 
                101010
                101110
and so on 
########################################
Last edited on
However, when I enter a binary value like 1111111111 it gives a message that the number is not binary
Well, it's still a dumb choice to read it into an integer. It would make a lot more sense to just read it into a string.

And you gave an incredibly stupid example. Your code wouldn't even work for 1.

Anyway, this part of your code is wrong (a common beginner mistake):
 
if ( (d != 1) || (d !=0) )   // if it is not 1 OR it is not 0 (that will always be true!) 

It should be
 
if (d != 1 && d !=0)   // if it is not 1 AND it is not 0 

Or, more intelligently:
 
if (d > 1)

Sorry for my "inane" code; my c++ knowledge is just 3 months old
The inanity displayed in your post has nothing to do with your C++ knowledge. It's your general intelligence. If your code doesn't even work for an input of 1, why would you give 111111111111111 as an example? It's braindead. You should have said "no matter what I enter it says it's not binary".

But what did you say: "if I enter 111111111111111 it comes as an error". It comes as an error. I think that says it all.
Last edited on
Given it is midnight in my country, and I have been working on this code for maybe hours, can't find an error, and am a beginner and not a CS or CE major, I can ask whatever I want. I didn't force you to answer my question, and my "general intelligence" is probably higher than yours. You don't know me and the obstacles I overcame in my life or my scores on IQ and standardized tests.
You know what is higher than my and your level of intelligence, it is the level of politeness that you probably lack. I tried to stay as calm and polite as possible, but honestly F#$* you and your ego, who do you think you are.
You are the one who lacks politeness. You are asking people for help, and look at how the lack of effort in your question. It's pathetic. Your "general intelligence" is certainly not higher than mine, you egotistical little moron.
Last edited on
I, first, asked what's wrong with my checker, and you replied and said that my example is "stupid."
I apologized for the stupid coding mistake; you continued insulting my general intelligence. What is wrong with you? Who is the one lacking politeness here? You don't know me, I repeat, and I am not egotistic at all. (I am not the one who insults beginners, asking a simple question and, then, apologizing for their coding mistake).
Let the dust settle.

Here's some minor revisions to make it (sort of) work. Please go on to the text afterwards ... and grit your teeth: I'm going to acknowledge the errors that @tpb found.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iomanip> 
#include <iostream>
#include <string>
using namespace std;

bool checkbinary(unsigned long long);
int convertBtoD(unsigned long long);
void drawline(char);
void header (); 
void body(unsigned long long);


int main()
{
  int n;
  unsigned long long A[20], bin;              // <===== Gives slightly more margin .. but ultimately
                                              //        binary representations are so long that you need a string

  cout << "Please enter the number of binary values desired: ";
  cin >> n; 
  while ( (n < 0) || (n > 20) )
  { 
      cout << "ERROR: THE NUMBER ENTERED IS IN-VALID; PLEASE RE-ENTER: ";
      cin >> n; 
      cout << endl; 
  }
  for ( int k = 0; k < n; k++)
  {
      cout << "Please enter the Binary number: ";
      cin >> A[k];                            // <===== You want the kth value, not the nth
      while (!checkbinary(A[k]) )             // <===== Same again ...  also, change to a while loop (may repeat the error)
      {
         cout << "ERROR: NUMBER NOT A BINARY NUMBER; PLEASE RE-ENTER: ";
         cin >> A[k];                         // <===== And again
      }
  }

  drawline ('#');

  header(); 

  drawline ('-');

  for (int c = 0; c < n; c++)                 // <===== Should be < n, not <= n
  {
      bin = A[c];                             // <===== You want the cth value, not the nth
      body(bin);
  }

  drawline ('#');
  system ("pause");                           // <===== Not ideal
}


void drawline(char SYM)                       // <====== Don't use $
{
    for (int c = 1; c<=30; c++) cout << SYM;
    cout << '\n';
}


void header()
{
    cout << setw(15) << "Binary" << setw(15) << "Decimal" << endl; 
}


void body(unsigned long long bin)
{
    cout << setw(15) << bin << setw(15) << convertBtoD(bin) << endl; 
    return;
}


bool checkbinary(unsigned long long bin)
{
    int d; 
    cout << "testing " << bin << endl;
    while (bin > 0)
    {
        d = bin % 10; 
        if ( (d != 1) && (d !=0) )           // <===== Should be &&, not ||
           return false;
        bin /= 10;
    }
    return true;
}


int convertBtoD (unsigned long long num)
{
    int d = 0, w = 1; 
    while (num > 0)
    {
        d = d + (num % 10) * w; 
        num /= 10;
        w = w * 2;
    }
    return d; 
}


Please enter the number of binary values desired: 3
Please enter the Binary number: 112
testing 112
ERROR: NUMBER NOT A BINARY NUMBER; PLEASE RE-ENTER: 1100
testing 1100
Please enter the Binary number: 1010
testing 1010
Please enter the Binary number: 111111
testing 111111
##############################
         Binary        Decimal
------------------------------
           1100             12
           1010             10
         111111             63
##############################



Outstanding issues.

(1) Binary representations are VERY long. I've given you a bit more margin with an unsigned long long above (a long is probably the same size as an int). Ultimately, you do need to replace this with a string (as @tpb tried to persuade you right at the beginning).

(2) A general comment on how you write code. You have an int main() and five separate functions ... before you start testing. Isolate them and check one at a time, building your program gradually.
Short test driver + convertBtoD()
Short test driver + checkBinary()
Also, avoid the fancy formatting until last of all - it's distracting from the hard numerical parts.


Get some sleep and revise it in the morning.
Last edited on
Thank you a lot for your help and recommendations
I, first, asked what's wrong with my checker, and you replied and said that my example is "stupid."
I apologized for the stupid coding mistake; you continued insulting my general intelligence. What is wrong with you? Who is the one lacking politeness here? You don't know me, I repeat, and I am not egotistic at all. (I am not the one who insults beginners, asking a simple question and, then, apologizing for their coding mistake).

Are you crying because I said you made a "common beginner mistake"? Is that what's got you misty? It's common. You were bound to make it at some point. Pretty much everybody does.
Last edited on
I am not crying; you insulted a beginner, who made a coding mistake which wasn't even intentional as it was midnight, by offending his general intelligence. Don't you know how to explain mistakes with a degree of politeness without any offence; I don't understand what is wrong with you? This is supposed to be a forum dedicated for beginners, not professionals, and, if I have not been a member here for months in which I received great answers with respect, I could have said that this is a forum dedicated for bullying and offending.
whine, cry, snivel, snuffle, snort, ... whatever buddy.
Go weep into your pillow.
@tpb
You are flaming. That's not productive, merely rude. Please keep it to a minimum.
I am not flaming. I am responding to lies. In my opinion, he started this. And he keeps continuing it. All I want is for him to leave me alone.
Last edited on
Lies ?! Can't you read?
tpb wrote:
whine, cry, snivel, snuffle, snort, ... whatever buddy.
Go weep into your pillow.

49 minutes later:
tpb wrote:
I am not flaming

That's the most asinine thing I've read all day.

If you really want to be left alone, stop coming back for more. There is a difference between criticism and invective. Your earlier posts are okay, because they're critical of something meaningful. It's content-free rudeness that has ruined this thread.

@Jack
Please carefully read:
http://www.catb.org/esr/faqs/smart-questions.html
In particular,
http://www.catb.org/esr/faqs/smart-questions.html#answers
I am sorry, but did you even try inputting "1" only in my code and see if it works? I think you then will know that my question was perfectly fine.
Input:
1
1
Let me show you what will be the output:
##############################
         Binary        Decimal
------------------------------
              1              1
              1              1
##############################

However, try to input a larger number and the program will terminate. I didn't list the reasons. I just, as a beginner, asked a question based on my observation, and again that doesn't give anyone the right to disrespect and offend people.
Pages: 12