### How to join 3 programs in 1?

I am working on a project where i should convert decimal numbers to binar, octal and hexadecimal. I made all 3 of them, but how do i join them in one program so they execute together? Help please!

#include<iostream>
using namespace std;
int main ()
{
int nr, bin;
cout << "Shkruani numrin decimal: ";
cin >> nr;
cout << "I kthyer ne binar, numri " << nr << " eshte: ";
while (nr > 0)
{
bin = nr % 2;
cout << bin;
nr /= 2;
}
return 0;
}

#include <iostream>
#include <cmath>
using namespace std;
int konvert(int decimal);
int main()
{
int decimal;
cout << "Fusni nje numer decimal: ";
cin >> decimal;
cout << "I konvertuar ne oktal eshte: " << konvert(decimal) << endl;
return 0;
}
int konvert(int decimal)
{
int mbetja, i = 1, octal = 0;
while (decimal != 0)
{
mbetja = decimal % 8;
decimal /= 8;
octal += mbetja * i;
i *= 10;
}
return octal;
}

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int decimal, r;
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
cout << "Fusni nje numer decimal: ";
cin>> decimal;
while(decimal>0)
{
r = decimal % 16;
decimal = decimal/16;
}
}
You know how to make and call a function (other than main), for you have konvert(int).

What if you rename all three main() with unique names and add a new main() that calls those three functions?
From what he has written i guess he hasn't been tought about functions yet
Let's just say you have three different files, (first.cpp, second.cpp, third.cpp).

If you want to use the other files, for example, inside the first.cpp program, you might want to do this. (just make sure the other two files are in the same directory though...

***first.cpp***

 ``12345678910`` ``````#include #include "second.cpp" #include "third.cpp" int main(){ /* your codes here... */ return 0; }``````

@keskiverto and @barnack have good points. Have you tried browsing articles about functions? This would be a good opportunity to visit this website about functions as they have suggested... :)

http://www.cplusplus.com/doc/tutorial/functions/

Do not include a *.cpp.

If you do have three cpp and a main() in each, and you "include cpp" two to third, then pre-processor directives effectively copy-paste three files together and compiler will see one "file" with three different main() in it. That does not work.

Each *.cpp is supposed to be compiled separately to produce object file.
Linker combines object files into the executable of the application.

If you include cpp, then two compiled "files" will contain contents of the included cpp, which at linking stage is a violation of ODR (One Definition Rule).

What you do include is headers that declare functions and define types, whose implementation is in some other cpp.

there are several ways to do this kind of thing in general... FYI..

-you can compile all the programs to libraries and build a new main program that calls the functions as needed.
- you can combine the code directly as a new program, changing the old mains to new functions with new names and cleaning them up a bit as needed.

- you can refactor the code. I actually recommend this: c++ has direct text to hex to base10 conversions built in that you can just use. And, I don't think c++ has added a binary one, but hex is directly convertable to binary, so you can convert to hex and then kick out the byte-binary via a 16 entry lookup table.

Have you noticed?
 ``12`` ``````bin = nr % 2; nr /= 2;``````

 ``12`` ``````mbetja = decimal % 8; decimal /= 8;``````

 ``12`` ``````r = decimal % 16; decimal = decimal/16;``````

Spot any similarities?

Write ONE function whose only parameters are the original integer and the base.
Topic archived. No new replies allowed.