Decimal to Binary Converter Using Functions in C++

Hey,

So I'm in an intro C++ programming course and I'm completely stuck right now. I am supposed to make a Decimal to Binary converter using functions, store the 1's and 0's in an array. Then use a Raspberry Pi 2 to light up LEDs on a breadboard. I have no idea how to store the 1's and 0's in an array using a function. Here's my code. Any help will be appreciated.

#include <iostream>
#include <wiringPi.h>
#include <stdlib.h>

using namespace std;

int DecBin(int);

int main()
{

int options;
int DecNum;


Top:

system("clear");

cout << " Decimal to Binary Converter \n";
cout << "++++++++++++++++++++++++++++++++++++++++++++++ \n";
cout << "+ This program converts Decimal numbers into + \n";
cout << "+ 8-bit Binary numbers. + \n";
cout << "++++++++++++++++++++++++++++++++++++++++++++++ \n\n";
cout << "Enter 1 to run the program. \n";
cout << "Enter 2 to exit the program. \n";

cin >> options;

if ((options > 2)|| (options <= 0))
{
cout << "\nERROR! Not a valid option. \n";
cout << "Press the enter key to continue...";
cin.clear();
cin.ignore();
cin.get();
goto Top;
}

switch (options)
{

case 1:

Reset:

cout << "Please enter a positive decimal number (or) \n";
cout << "Enter a negative number to go back to the menu: ";

cin >> DecNum;

if (DecNum < 0 || cin.fail())
{
cin.fail();
cin.ignore();
cin.clear();
goto Top;
}

cout << "\nThe Binary conversion for the Decimal number " << DecNum << " is: ";
DecBin(DecNum);
cout << "\n\nPress the Enter key to continue...";
cin.get();
cin.get();
system("clear");
goto Reset;

break;

case 2:

cout << "\nYou have chosen to exit the Decimal to Binary Converter. Good-bye! \n";
exit(0);
cin.get();

break;

default:

cout << "\nERROR! Please enter a NUMBER only! \n";
cout << "Press the enter key to continue... \n";
cin.fail();
cin.ignore();
cin.clear();
cin.get();

break;

}

//cin.get();
main();
//return 0;


}

int DecBin (int DecNum)
{

int DecRemainder;


DecRemainder = DecNum % 2;
DecNum = DecNum / 2;

if (DecNum > 0)
{
DecBin(DecNum);
}

else if (DecNum = 0)
{

cout << "0";
return DecNum;
}

cout << DecRemainder;

}
closed account (48T7M4Gy)
This might help. Just store each result per iteration in your array. Remember the order of elements in the array is significant.

http://www.cplusplus.com/forum/beginner/179751/
Topic archived. No new replies allowed.