How do i make this so it keeps repeating unless a -1 is inputed. And when inputed it couts a statement. I have tried do while and if but it messes up on me everytime.
#include<iostream>
usingnamespace std;
int main()
{
long bin, dec = 0, rem, num, base = 1;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal equivalent of " << bin << " : " << dec << endl;
return 0;
}
#include<iostream>
#include<cstdlib>
usingnamespace std;
int main()
{
long bin, dec = 0, rem, num =1, base = 1;
while (num != -1)
{
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal equivalent of " << bin << " : " << dec << endl;
}
return 0;
}