5 Replies - 174 Views - Last Post: 08 February 2012 - 11:02 AM Rate Topic: -----

Topic Sponsor:

#1 herpienne12  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 02-November 11

Binary Number

Posted 08 February 2012 - 03:54 AM

#include<iostream>
using namespace std;

int main()
{
	int b[8];
	int i=0, count=0;
	cout << "Enter a binary number: ";
	cin >> b[i];
	cin.get();
	for (i=0; i<=8; i++)
	{
		if (b[i] == 1) 
		count = count + 1;
	}
	cout << "The value of 1 is " << count;
	cin.get();
	return 0;
}


Wrong output. :(
The output is always 0.
Help me if you don't mind. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Binary Number

#2 UrIkOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-November 11

Re: Binary Number

Posted 08 February 2012 - 04:00 AM

You should use loops for the input because you declare b is an array.

This post has been edited by UrIkOn: 08 February 2012 - 04:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 herpienne12  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 02-November 11

Re: Binary Number

Posted 08 February 2012 - 04:05 AM

#include<iostream>
using namespace std;

int main()
{
	int b[8];
	int i=0, count=0;
	cout << "Enter a binary number: ";
	for (i = 0; i <=8; i++)
	{
	cin >> b[i];
	cin.get();
	}
	for (i=0; i<=8; i++)
	{
		if (b[i] == 1) 
		count = count + 1;
	}
	cout << "The value of 1 is " << count;
	cin.get();
	return 0;
}


i tried this one but this doesn't give me results.
Was This Post Helpful? 0
  • +
  • -

#4 UrIkOn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 06-November 11

Re: Binary Number

Posted 08 February 2012 - 04:20 AM

What is your expected output?
Was This Post Helpful? 0
  • +
  • -

#5 shurd  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 113
  • Joined: 05-February 12

Re: Binary Number

Posted 08 February 2012 - 04:29 AM

You can do it with only one loop
for(int i=0;i<8;i++)
{
   cin>>b[i];
   if(b[i]==1)
     count++;
}


Btw arrays in C start with 0 and finishes in size_specified-1, so if you want 8 spaces it will go from 0~7 meaning you need a loop that stops at 7 and not at 8, which would be <=7 or <8.
Was This Post Helpful? 0
  • +
  • -

#6 simeesta  Icon User is offline

  • D.I.C Addict


Reputation: 187
  • View blog
  • Posts: 529
  • Joined: 04-August 09

Re: Binary Number

Posted 08 February 2012 - 11:02 AM

If you do it this way you will have to enter the binary digits separated by a space so 5 would be
0 0 0 0 0 1 0 1


otherwise cin will read 00000101 as 101.

You could read it in as one integer then access each digit using % and / operators.

Or read them as individual characters and test whether it equals the character '1'.

Count is just counting the number of ones in the number and not calculating its value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1