Why ios::hex and ios::oct Does not Work in C++
Someone might think ios:ct and ios::hex doesn't work in Linux for some unknown reason. The thing is that I have got to know why it's happening. Actually there is no problem, the thing is by default ios::dec turned on in C++. So we have to turn it off by typing cout.unsetf(ios::dec); before our cout statement or cout<<hex<<21323<<endl;.
Eg:-1.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.unsetf(ios::dec); //Turn off decimal mode
cout<<setiosflags(ios::hex)<<12323<<endl; //Hex mode is turned on from now on
return 0;
}
Eg:-2.
#include<iostream>
using namespace std;
int main()
{
cout<<hex<<12323<<endl;/*This is easy because we do not have to type cout.unsetf(ios::dec).This thing automatically turns off ios::dec.*/
return 0;
}
Eg:-3.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.unsetf(ios::dec);
cout<<setiosflags(ios::hex)<<setw(5)<<12323<<endl;
return 0;
}
I learned this thing from http://www.learncpp.com/cpp-tutorial...tream-and-ios/
Eg:-1.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.unsetf(ios::dec); //Turn off decimal mode
cout<<setiosflags(ios::hex)<<12323<<endl; //Hex mode is turned on from now on
return 0;
}
Eg:-2.
#include<iostream>
using namespace std;
int main()
{
cout<<hex<<12323<<endl;/*This is easy because we do not have to type cout.unsetf(ios::dec).This thing automatically turns off ios::dec.*/
return 0;
}
Eg:-3.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.unsetf(ios::dec);
cout<<setiosflags(ios::hex)<<setw(5)<<12323<<endl;
return 0;
}
I learned this thing from http://www.learncpp.com/cpp-tutorial...tream-and-ios/
Total Comments 0