I have this piece of code: #include<iostream> using namespace std; #include <fst
ID: 3758523 • Letter: I
Question
I have this piece of code:
#include<iostream>
using namespace std;
#include <fstream>
ofstream outputFile;
//this is function for binary
void getbinj(int nnm)
{
int rmd;
if (nnm <= 1)
{
outputFile << nnm;
return;
}
rmd = nnm % 2;
getbinj(nnm / 2);
outputFile << rmd;
}
//this is function for Hexa
void getHexa(int nnnm){
int quo = nnnm;
int oo=1,jooo,too;
char hcdm[100];
while(quo!=0) //iteratoing over the loop.
{
too = quo % 16;
if( too < 10)
too =too + 48;
else
too = too + 55;
hcdm[oo++]= too;
quo = quo / 16;
}
for(jooo = oo -1 ;jooo> 0;jooo--)
outputFile<<hcdm[jooo];
outputFile<<endl;
}
//main function
int main()
{
outputFile.open("output.txt");
//variables declaration for
int dcm, getbin;
outputFile << "Decimal "<<"Binary "<<"HexaDecimal";
outputFile<<" ";
cin >> dcm;
outputFile<<" ";
getbinj(dcm);//function call
outputFile<<" ";
getHexa(dcm);//function call
outputFile << endl;
outputFile.close();
return 0;
}
It is supposed to display deciaml number, binary number, and hexdecimal. I want to display 0-255. But i dont think its working right.
Specifications:
I want to list the deciamal number, binary number, and hexdecimal number from 0 to 255.
Example:
Decimal Binary Hexdecimal
0 0000 0000 00
1 0000 0001 01
2 0000 0010 02
.
.
255 1111 1111 FF
I want to display my output just like this. Which is this code above. But I don’t know how to iterate it so that it will display until 255.
It should output in a .txt file.
I also have that in the code above.
I just don’t think it is working to output the hexdecimal.
Explanation / Answer
#include<iostream>
using namespace std;
#include <fstream>
ofstream outputFile;
//this is function for binary
void getbinj(int nnm)
{
int rmd;
if (nnm <= 1)
{
outputFile << nnm;
return;
}
rmd = nnm % 2;
getbinj(nnm / 2);
outputFile << rmd;
}
//this is function for Hexa
void getHexa(int nnnm){
int quo = nnnm;
int oo=1,jooo,too;
char hcdm[100];
while(quo!=0) //iteratoing over the loop.
{
too = quo % 16;
if( too < 10)
too =too + 48;
else
too = too + 55;
hcdm[oo++]= too;
quo = quo / 16;
}
for(jooo = oo -1 ;jooo> 0;jooo--)
outputFile<<hcdm[jooo];
outputFile<<endl;
}
//main function
int main()
{
outputFile.open("output.txt");
//variables declaration for
int dcm, getbin;
outputFile << "Decimal "<<"Binary "<<"HexaDecimal";
outputFile<<" ";
for(int i = 0; i < 255; i++){
//cin >> dcm;
outputFile<<i;
outputFile<<" ";
getbinj(i);//function call
outputFile<<" ";
getHexa(i);//function call
outputFile << endl;
}
outputFile.close();
return 0;
}