How do I get the output of this code to output to a text file instead of the .ex
ID: 3757791 • Letter: H
Question
How do I get the output of this code to output to a text file instead of the .exe screen in the compiler?
#include<iostream>
using namespace std;
//this is function for binary
void getbinj(int nnm)
{
int rmd;
if (nnm <= 1)
{
cout << nnm;
return;
}
rmd = nnm % 2;
getbinj(nnm / 2);
cout << 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--)
cout<<hcdm[jooo];
cout<<endl;
}
//main function
int main()
{
//variables declaration for
int dcm, getbin;
cout << "Decimal "<<"Binary "<<"HexaDecimal";
cout<<" ";
cin >> dcm;
cout<<" ";
getbinj(dcm);//function call
cout<<" ";
getHexa(dcm);//function call
cout << endl;
return 0;
}
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<<" ";
cin >> dcm;
outputFile<<" ";
getbinj(dcm);//function call
outputFile<<" ";
getHexa(dcm);//function call
outputFile << endl;
outputFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.