[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2005-05-10
6
Medium Priority
?
181 Views
Last Modified: 2010-04-01
/*******************************************************************
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
Comment
Question by:cuong5985
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13975174
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()
{
 //.. Your code here

Also your main function is missing an end bracket }
0
 
LVL 30

Expert Comment

by:Axter
ID: 13975210
cuong5985,

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

Can you please close your open questions.

I also notice your grading history:
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.

Please read the grading guidelines.
0
 

Author Comment

by:cuong5985
ID: 13975229
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:balder
ID: 13975415
what's

    system("PAUSE");
     return 0;
}

doing at the end?
0
 
LVL 30

Accepted Solution

by:
Axter earned 150 total points
ID: 13977458
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

by:Masudkhan
ID: 14067967
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question