I am doing C++ assignment, it invols designing a program where the user can put binary numbers and gets a word in result.
I have done the first part of the program but i am strugling to do the the last to tasks.
Here is what i have done so far

#include <iostream>
using namespace std;
void main(void) 
  {
    int binaryword(int binary, int bits[]); // function prototypes
	void  display(int binary, int x[]);

    int bit,binary=0,i;
    int bits[20]; // declare an array, max size 20 elements

    for (i=0;i<19;i++) bits[i]=0; // good practice to initialise array
    cout << "enter only 0 and 1 \n\n";
    cout << "End by entering any negative number \n\n";
    do // example of a do ... while loop
      {
        cin >> bit;
        if ((bit == 0) || (bit ==1)) 
          {
	    bits[binary]=bit;  // store current number in array
	    binary++;                // increment number count
          }
        else 
          {
            cout << "-ve number entered, bye bye\n";
          }

      } while((bit == 0) || (bit ==1));

    // now call the display function to show the array
    display(binary,bits); //  don't need []after the 'numbers' parameter

    
  } // of main

  

  // Function to display the array
  void display(int binary, int x[]) // parameter order matters not the name
    {
      for (int j=0;j<binary;j++) // notice int can be declared in the for statement
        {
	  cout << x[j];
        }
    } // of display function

That is not it, i have to convert it to decimal so the user can type binary numbers and get a word
please HELP
Cheers

Recommended Answers

All 4 Replies

please edit your first post and place your code in CODE=cpp tags.

I think there is some Error in you in/out-put stream,
for :"cin>> bit"(line 21) i guess you want getting one char(0 or 1) each time , but "cin" let you keep inputing untill you enter the "enter" .
wish that can help you.

please edit your first post and place your code in CODE=cpp tags.

Agreed... As a side note...

void main(void)

void main is bad practice, main returns an int & doing otherwise is just asking for bad things to happen.

// binary.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <conio.h>              //look here!!!!
#include <iostream>
using namespace std;
void main(void) 
{
int binaryword(int binary, int bits[]); // function prototypes
void display(int binary, int x[]);

int bit,binary=0,i;
int bits[20]; // declare an array, max size 20 elements

cout << "enter only 0 and 1 \n\n";
cout << "End by entering any negative number \n\n";

for (i=0;i<19;i++) bits[i]=0; // good practice to initialise array

/////////////////////////////////////////
//look here!!!!
do // example of a do ... while loop
{
	//cin>> bit;
	
	bit=getch();
	
if ((bit == '0') || (bit =='1')) 
{  
	putchar(bit);
	bit=bit-48;
bits[binary]=bit; // store current number in array
binary++; // increment number count
}
//cout<<bit<<endl;
////////////////////////////////////////////////
else 
{
cout <<endl<< "-ve number entered, bye bye\n";
}

} while((bit == 0) || (bit ==1));

// now call the display function to show the array
display(binary,bits); // don't need []after the 'numbers' parameter

system("pause");
} // of main



// Function to display the array
void display(int binary, int x[]) // parameter order matters not the name
{
for (int j=0;j<binary;j++) // notice int can be declared in the for statement
{
cout << x[j];
}
} // of display function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.