Solved

# Program to read a positive integer  and convert it to a binary string

Posted on 2005-05-10
176 Views
/*******************************************************************
Project         55
Author          Cuong Nguyen
Purpose         Program to read a positive integer  and convert it
to a binary string
*******************************************************************/
#include <iostream>
using namespace std;

int main()
{
char binary[256];
int num;

do
{
cout<< "Enter a number:";
cin >> num;
if (num==0)
continue;
if (num<0)
{
cout << "Error " << endl;
continue;
}
Convert(num,binary);
cout << "Binary representation: " << binary << endl;
}while(num);

void Convert(int num,char *binary)
{
char buf[256];
int i=0;
while (num!=0)
{
if (num%2)
buf[i]='1';
else
buf[i]='0';
num=num/2;
i++;
}
buf[i]=0;
for(int j=0;j<i;j++)

binary[j]=buf[i-j-1];

binary[i]=0;
}

system("PAUSE");

return 0;
}
Can someone help me with this, this program show me that there is some problem:
1. 26 H:\CS2\Nest Loop\Project 55.cpp `Convert' undeclared (first use this function)
31 H:\CS2\Nest Loop\Project 55.cpp a function-definition is not allowed here before '{' token
31 H:\CS2\Nest Loop\Project 55.cpp expected `,' or `;' before '{' token
I dont know how to fix this, does anyone know?
0
Question by:cuong5985

LVL 30

Expert Comment

You need to forward declare your function before you try to use it.
So before main(), add the following:
void Convert(int num,char *binary);

int main()
{

Also your main function is missing an end bracket }
0

LVL 30

Expert Comment

cuong5985,

You have seven questions open, with some about a month old.

B A B B B B B A B A

When grading, please consider the amount of points you're awarding compare to the quality of the answer.
A 20 or 50 point questions, normally should get an A grade if you got a complete answer.

0

Author Comment

I did change but still have problem
54 H:\CS2\Nest Loop\Project 55.cpp expected constructor, destructor, or type conversion before '(' token
54 H:\CS2\Nest Loop\Project 55.cpp expected `,' or `;' before '(' token
55 H:\CS2\Nest Loop\Project 55.cpp expected unqualified-id before "return"
55 H:\CS2\Nest Loop\Project 55.cpp expected `,' or `;' before "return"
56 H:\CS2\Nest Loop\Project 55.cpp expected declaration before '}' token

/*******************************************************************
Author          Cuong Nguyen
Purpose         Program to read a positive integer  and convert it
to a binary string
*******************************************************************/
#include <iostream>
using namespace std;

void Convert(int num,char *binary);
int main()
{
char binary[256];
int num;

do
{
cout<< "Enter a number:";
cin >> num;
if (num==0)
continue;
if (num<0)
{
cout << "Error " << endl;
continue;
}
Convert(num,binary);
cout << "Binary representation: " << binary << endl;
}while(num);
}

void Convert(int num,char *binary)
{
char buf[256];
int i=0;
while (num!=0)
{
if (num%2)
buf[i]='1';
else
buf[i]='0';
num=num/2;
i++;
}
buf[i]=0;
for(int j=0;j<i;j++)

binary[j]=buf[i-j-1];

binary[i]=0;
}

system("PAUSE");
return 0;
}
0

LVL 3

Expert Comment

what's

system("PAUSE");
return 0;
}

doing at the end?
0

LVL 30

Accepted Solution

When you get a compile error, jump to the specific line, and look to see if there is something wrong with it.

If you're learning C++, these are things that you should try and do, to increase your knowledge and your ability to debug your own code.

The compile errors are pointing you in the right direction.  You just need to analyze it, and apply some critical thinking to what the compiler is telling you.
0

Expert Comment

Here is a working program
==================

/*******************************************************************
Author          Cuong Nguyen
Purpose         Program to read a positive integer  and convert it
to a binary string
*******************************************************************/
#include <iostream>
using namespace std;

int Convert(int num,char *binary);
int main()
{
char binary[256];
int num;

do
{
cout<< "Enter a number:";
cin >> num;
if (num==0)
continue;
if (num<0)
{
cout << "Error " << endl;
continue;
}
Convert(num,binary);
cout << "Binary representation: " << binary << endl;
}while(num);

return 0;
}

int Convert(int num,char *binary)
{
char buf[256];
int i=0;
while (num!=0)
{
if (num%2)
buf[i]='1';
else
buf[i]='0';
num=num/2;
i++;
}
buf[i]=0;
for(int j=0;j<i;j++)

binary[j]=buf[i-j-1];

binary[i]=0;

system("PAUSE");
return 0;
}
0

## Featured Post

### Suggested Solutions

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.