binary number

how can i check if the number that is entered is a binary number? i tried with code.find(), and code.find_first_of()...
if the user input a string that contains any number >1, it should give us an error. i need to use while loop only.
if it is not a binary number, the program will stop and give an error.
1
2
3
4
5
6
7
8
9
10
11
12
13
int count=0;
    int i=0;
    
    while(i<s.length())
    {
     
       if(s.at(i)=='1')
           count++;
           i++;
          
    }
    cout<<"The Number of Ones is: "<<count<<endl;
    cout<<endl;


thanks in advance
if number is a binary number function return 1 else return 0
1
2
3
4
5
6
7
8
9
10
11
12
int testBinary(int number)
{
	int dv;
	while(number!=0)
	{
		dv=number%10;
		if(dv>1)
			return 0;
		number=number/10;
	}
	return 1;
}
thanks for your reply.
the problem is that the input is a string, so how do i convert that from string to int, or is there any other way?
thanks again.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int testBinary(int number)
{
	int dv;
	while(number!=0)
	{
		dv=number%10;
		if(dv>1)
			return 0;
		number=number/10;
	}
	return 1;
}
int ConvertStringToInt(string str)
{
	int tem;
	tem=atoi((char*)str.c_str());
	return tem;
}


or


1
2
3
4
5
6
7
8
9
string a;
	cin>>a;	
	for(int i=0;i<a.length();i++)
	{
		if(a.at(i)=='1' || a.at(i)=='0')
		{
			cout<<"ok"<<endl;
		}
	}
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
        for(int i=0;i<code.length();i++)
	{
		if(code.at(i)=='1' || code.at(i)=='0')
		   {
			cout<<"This is a Binary number"<<endl;
			cout<<endl;
	                   num=counter(code);
                    }
	            else
	                {cout<<"Error: This is not a Binary number!!!"<<endl;}
	            
                
    }      



int counter(const string &s)
{
    int count=0;
    int i=0;
    
    while(i<s.length())
    {
       
       if(s.at(i)=='1')
           count++;
           i++;
          
    }
    cout<<"The Number of Ones is: "<<count<<endl;
    cout<<endl;
}
   


That's how i am putting my code. not working well :(

Thanks
start by converting the string to an int by using atoi, no extra function needed just call it once at the start of the program. see http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

once you have your number, use the % operator (known as modulus) the way that ledien showed you to decide if it is binary or not. Don't bother using cout until you know whether or not the whole number is binary.

If you don't want to do all that... then just treat the string like a number and use a boolean flag like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool IsBinary( string s )
{
   // assume true, set to false if a non binary number is found
   bool bIsBinary = true;

   for( int i = 0; i < s.length( ); i++ )
   {
      if( s.at(i) != '1' && s.at(i) != '0' )
      {
          bIsBinary = false;
      }
   }

   return bIsBinary;
}


that's not really a great way of doing it imo...
This is how i set up my program...

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
#include <iostream>
#include <string>



using namespace std;
int counter(const string &);
bool IsBinary(string &);

int main() 
{
 string code;
 int num;


while(code!="Exit"&&code!="exit")
{
 cout<<"Please enter a binary number and I will let you know the number of"<<endl;
 cout<<"1s there are (Type Exit To Exit): "<<endl;
 cout<<"Binary code: ";
 cin>>code;
 cout<<endl;   
 cout<<"Your Binary Code is: "<<code<<endl;
 
                  IsBinary(code);
                  if(false)	                   
                    {cout<<"Error!"<<endl;}
                       else
                         { num=counter(code);}
}                  
      cout<<endl;
      cout<<endl;
      
 system("pause"); 
    
}


int counter(const string &s)
{
    int count=0;
    int i=0;
    
    while(i<s.length())
    {
       
       if(s.at(i)=='1')
           count++;
           i++;
          
    }
    cout<<"The Number of Ones is: "<<count<<endl;
    cout<<endl;
}

bool IsBinary( string s )
{
   // assume true, set to false if a non binary number is found
   bool bIsBinary = true;

   for( int i = 0; i < s.length( ); i++ )
   {
      if( s.at(i) != '1' && s.at(i) != '0' )
      {
          bIsBinary = false;
          
      }
   }

   return bIsBinary;
}


thanks for ur help guys!!!
what should i do to make it compile?
thanks advance!
borok you have an error there.

you need to set something equal to the return value of IsBinary

1
2
3
4
5
6
7
8
if(false)
{
   // will never get hit
}
else
{
   // will ALWAYS get hit
}


instead...

1
2
3
4
5
6
7
8
9
10

bool bIsBinary = IsBinary( code );
if(bIsBinary )
{
   // code is binary
}
else
{
   // code is not binary
}


make sense?
thanks a lot, got it!
u sure? post your code!
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
#include <iostream>
#include <string>



using namespace std;
int counter(const string &);
bool IsBinary(string );

int main() 
{
 string code;
 int num;

 // Welcome message
	cout << " ------------------------------------------------ \n\n"
		 << " Welcome to Binary Number Evaluator\n"
		 << " ------------------------------------------------- \n\n"; 


while(code!="Exit"&&code!="exit")
{
 cout<<"Please enter a binary number and I will let you know the number of"<<endl;
 cout<<"1s there are (Type Exit To Exit): "<<endl;
 cout<<"Binary code: ";
 cin>>code;
 cout<<endl;   
 cout<<"Your Binary Code is: "<<code<<endl;
 
             bool bIsBinary = IsBinary( code );
                 if(bIsBinary )
                    {
                    num=counter(code);
                    cout<<endl;
                        }
                        else
                            {
                            cout<<"Error!!! Your Number is Not Binary"<<endl;
                            cout<<endl;
                                }
}                  
      cout<<endl;
      cout<<endl;
      
 system("pause"); 
    
}


int counter(const string &s)
{
    int count=0;
    int i=0;
    
    while(i<s.length())
    {
       
       if(s.at(i)=='1')
           count++;
           i++;
          
    }
    cout<<"The Number of Ones is: "<<count<<endl;
    cout<<endl;
}

bool IsBinary( string s )
{
   int i=0;
   bool bIsBinary = true;
   
   while (i<s.length())
   {
      if( s.at(i) != '1' && s.at(i) != '0' )
          {
          bIsBinary = false;
          }
          i++;
   }

   return bIsBinary;
}
Last edited on
Topic archived. No new replies allowed.