Help on Binary to Decimal program

Im having trouble with my binary to decimal program. I have whats below already, although there are no errors it still is probably wrong, a little backstory is im trying to write a program that takes binary values from a file which includes some binary values with a letter in it or a space to make it incorrect meaning we have to output "bad digit on input" and whenever i run it, it only outputs "Binary number Decimal Equivalent" and nothing else. Not sure what im doing wrong. The file is this:
1
_10
11
___10000
10 1100
10101
11001g011
101110111
k0011011
00011

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;



int main()
{
int decimal = 0;
char binary;
int count = 0;
int width;
int b = 0;
int c = 0;
int a = 0;

ifstream infile;
infile.open("INLABVII.DAT");

cout << "Binary Number Decimal Equivalent" << endl;
cout << " ";
infile.get(binary);


while (infile)
{

if (binary == '1')
{
decimal = 2 * decimal + 1;
cout << binary;
c++;
}
}
if (binary == '0')
{
decimal = 2 * decimal;
cout << binary;
c++;
}
else if (binary == ' ')
{
for (a = 0; a < c; a++)
cout << "\b";
if (c > 0 && a > 0)
{
cout << "Bad digit on input" << endl;
}
}
if (binary != '0' && binary != '1' && binary != '\n' && binary != ' ')
{
a = 0;
}
for (a = 0; a < c; a++)
{
cout << "\b";
}
cout << "Bad digit on input" << endl;
if (binary == '\n')
{
width = 25 - c;
cout << setw(width) << decimal << endl << " ";
c = 0;
decimal = 0;
}

return 0;
}

Last edited on
Topic archived. No new replies allowed.