Hey!
I'm currently writing a program which converts a number from binary to int. That part is done, but I'm now trying to check wheither the input string only consists of 1 and 0.
This is what I've tried so far, but it doesnt work the way I want.
string binarynumber;
bool binaryLoop = true;
Console.Writeline("Please type in a binary number: ");
binaryNumber = Convert.ToString(Console.ReadLine());
while (binaryLoop)
{
try
{
foreach (char digit in binaryNumber)
{
int digit = Convert.ToInt32(siffer.ToString());
if (digit == 1 || digit == 0)
{
binaryLoop = false;
}
else
{
Console.WriteLine("The binary number can only be 1 and 0, please retype");
}
}
}
catch(FormatException)
{
Console.WriteLine("The binary number can only be 1 and 0, please retype");;
}
}
Any hint? Or better methods?
Regards Asotop.