convert to loop

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.

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

using namespace 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;

}
how about this?


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


using namespace 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;

}


it still messes up on big numbers like 10010000 and also when - 1 is inserted it needs to output a message like all set
Jvardam I am not sure i understand, Binary number 10010000 is in decimal 144
this is what your code shows, and here you have confirmation on the web
http://coolconversion.com/math/binary-octal-hexa-decimal/Convert__number_10010000_in__

What do you want to happen when you break loop?
Topic archived. No new replies allowed.