Integer to Binary

so I have this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{

   int decimal;

   while (cin >> decimal)
   {
      cout << "Enter a decimal integer to convert it to binary: ";
      cin >> decimal; 
      if (decimal > 0)
      {
         cout << "" << decimal << " (base 10) = ";
         decToBin( decimal, 2 ); 
         cout << " (base 2). " << endl;
      }
      else
         cout << "" << decimal << " is not a positive integer. " << endl;
   }
   cout << endl;
   return 0;


} 

that outputs
32
 
//supposed to calculate this binary 

Enter a Decimal Integer to convert it to binary: -2
-2 is not a positive integer.
3
 
//supposed to calculate this binary 

Enter a Decimal Integer to convert it to binary: 6
6 (base 10) = 110 (base 2).

Why is it not calculating every binary but calculating every other one?
Last edited on
Can You write the code of decToBin function code ??
1
2
3
4
5
6
7
8
9
10
void decToBin(int num, int base)
{

   if (num > 0)
   {
      decToBin(num/base, base);
      cout<< num % base;
   }

}
You are reading a number on line 6 AND on line 9. You don't do anything with the number entered on line 6.
ok, I took out the cin >> decimal; on line 9, but it seems to do one input when I have more inputs available
you could change the while loop to:
while(cin){
so that you dont read in cin twice
If he do that he will run the loop once too much.
I changed the while to a do-while, took out that extra cin statement and everything is fine EXCEPT it outputs this

Enter a decimal integer to convert it to binary: -1080046200
-1080046200 is not a positive integer. //not one of my inputs
32
Enter a decimal integer to convert it to binary: 32
32 (base 10) = 100000 (base 2).
2
Enter a decimal integer to convert it to binary: 2
2 (base 10) = 10 (base 2).
-3
Enter a decimal integer to convert it to binary: -3
-3 is not a positive integer.
6
Enter a decimal integer to convert it to binary: 6
6 (base 10) = 110 (base 2).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{

   int decimal;


   do
   {
      cout << "Enter a decimal integer to convert it to binary: " << decimal << endl;
      if (decimal > 0)
      {
         cout << "" << decimal << " (base 10) = ";
         decToBin (decimal, 2); 
         cout << " (base 2). " << endl;
      }
      else
         cout << "" << decimal << " is not a positive integer. " << endl;
   } while (cin >> decimal);
   return 0;
}

What could possibly be wrong with this?
Now decimal will be uninitialized when you run the first loop iteration.
Well I don't know what to do cuz I changed it to a while loop and it gave me this

36
-2
6


Enter a decimal integer to convert it to binary: 6
6 (base 10) = 110 (base 2).
got it!! Peter87, thank you for walking me through what I was doing wrong not just giving me the code!!
Please may you post the complete source code?

I would love to have a program which does this! :)

Nice coding
cameron, the simplest way to do this is to use the format manipulators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//manip.cpp --- using format manipulators
#include <iostream>
int main()
{
    using namespace std;
    cout << "Enter an integer: ";
    int n;
    cin >> n;

    cout << "n\tn*n\n";
    cout << n << "\t" << n * n << " (decimal)\n";
    //set to hex
    cout << hex;
    cout << n << "\t" << n * n << " (hexadecimal)\n";
    //set to octal
    cout << oct << n << "\t" << n*n << " (octal)\n";
    //alternative way to call manipulator
    dec(cout);
    cout << n << "\t" << n * n << " (decimal)\n";
    return 0;
}

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

using namespace std;

void decToBin (int, int);

int main()
{

   int decimal;
   cin >> decimal;
   do
   {
      cout << "Enter a decimal non-negative integer to convert it to binary: " << decimal << endl;
      if (decimal > 0)
      {
         cout << "" << decimal << " (base 10) = ";
         decToBin (decimal, 2); 
         cout << " (base 2). " << endl;
      }
      else
         cout << "" << decimal << " is not a non-negative integer. " << endl;
   } while (cin >> decimal); 
   return 0;
}

void decToBin(int num, int base)
{

   if (num > 0)
   {
      decToBin (num/base, base);
      cout << num % base;
   }

}
Topic archived. No new replies allowed.