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 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.
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 :(
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;
}
#include <iostream>
#include <string>
usingnamespace 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;
}
#include <iostream>
#include <string>
usingnamespace 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;
}