Need Help on binary input of values

dumanli's Avatar
Newbie Member
Hi there everyone,

I've got some query! I just started college and i have got some tough homework! I think it is part of an assignment! But i'm not sure!

I have tried doing it but programming is just too hard for me! I just don't understand much!

I've been asked to write up a program using C++ but as i said, I am new and dont understand much!

The specification is to write a program that allows the user to input a series of 0's and 1's which will be stored as an array representing a binary word. Input should stop when something other than a 0 or a 1 is input by the user.

I am urgently asking for your help!!!!

Please Help Me! I havent got much time left!

Thanks Alot!
DaWei's Avatar, Join Date: Dec 2006
Team Leader
"Need help" is not a good subject line. It applies to almost every post on the forum. It is highly unlikely that anyone is going to do your work for you. That would be a disservice to you, possibly dishonest, if you claim it as your own work. If you have waited until the last moment to hit the books or pay attention in class, then go have a peek in your mirror at the culprit. It would benefit you to read the forum's rules and FAQ and advice on how you should post. I would also suggest that you read this.

When you have put some basic code together by breaking a sweat of your own, and can't get it working, then post back. Be sure and include all the relevant information regarding your problems. You'll see that referred to in the materials I have suggested.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Try reading Before you make a query thread which is linked from every page of the forum. The link is on the top right section of the page.
dumanli's Avatar
Newbie Member
I, know. I was a bit late in reading the info before the post! I wanted to change the heading but couldn't, Just wandered if you can change it to something relevant!
dumanli's Avatar
Newbie Member
Quote:
Originally Posted by DaWei
"Need help" is not a good subject line. It applies to almost every post on the forum. It is highly unlikely that anyone is going to do your work for you. That would be a disservice to you, possibly dishonest, if you claim it as your own work. If you have waited until the last moment to hit the books or pay attention in class, then go have a peek in your mirror at the culprit. It would benefit you to read the forum's rules and FAQ and advice on how you should post. I would also suggest that you read this.

When you have put some basic code together by breaking a sweat of your own, and can't get it working, then post back. Be sure and include all the relevant information regarding your problems. You'll see that referred to in the materials I have suggested.
The thing is that I have been going to lessons and paying attention, but its not that simple! I have tried doing it! But couldn't manage it! Asked help as well from my teacher which was helpful but still i have troubles with it!

Also even if someone at this forum was to help me, which i would love them to do that, it wont be a straight copy because i have to change that to the specification, add more bits and pieces to it! This is just part 1 which i have troubles with!

If you can change the topic name to something more relevant, i.e Creating a program using arrays to represent a binary word!

Waiting for your replies!

Thanks
dumanli's Avatar
Newbie Member
Got some coding which i'm not sure if its right so far! Just wanted to check!

Code:
#include <iostream.h>
void main(void) 
  {
    float binary_value(int n, int numbers[]); // function prototypes
    void  display(int n, int x[]);

    int number,n=0,i;
    float binary;
    int numbers[8]; // declare an array, max size 8 elements

    for (i=0;i<8;i++) numbers[i]=0; 
    cout << "Enter positive integers, each followed by cr \n\n";
    cout << "End by entering any negative number \n\n";
    do 
      {
        cin >> number;
        if (number >= 0) 
          {
	    numbers[n]=number;  // store current number in array
	    n++;                // increment number count
          }
        else cout << "-ve number entered, bye bye\n";
          
      } while(number >=0);   // end of do  while loop
Thanks
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Yes you are not allowed to edit your own posts unless you have n number of posts and I have edited it for you.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You say your user should enter 0s and 1s, but you're asking for positive integers. What are you planning to do with a float? I invite you to run your code and enter 'A' instead of a number.

By entering single-digit 1s and 0s you are simulating the value of bits. By placing these in an array, you are simulating a collection of bits. Once you have accomplished that you can simulate bit-operations on them. You will need to review your earlier work with these entities.

If you run out of exclamation points, just ask Santa for more.
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by dumanli
Got some coding which i'm not sure if its right so far! Just wanted to check!

Code:
#include <iostream.h>
void main(void) 
  {
    float binary_value(int n, int numbers[]); // function prototypes
    void  display(int n, int x[]);

    int number,n=0,i;
    float binary;
    int numbers[8]; // declare an array, max size 8 elements

    for (i=0;i<8;i++) numbers[i]=0; 
    cout << "Enter positive integers, each followed by cr \n\n";
    cout << "End by entering any negative number \n\n";
    do 
      {
        cin >> number;
        if (number >= 0) 
          {
	    numbers[n]=number;  // store current number in array
	    n++;                // increment number count
          }
        else cout << "-ve number entered, bye bye\n";
          
      } while(number >=0);   // end of do  while loop
Thanks
I think you already got the answer in some other forum.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
I hope not, if that code is the 'help'. Beginning with line 1 (iostream.h), continuing with line 2 (void main ...), and on and on, it's a mess.