I am trying to write a program in Visual C++ 6.0 using MFC where I take an octal number and convert it to decimal. I understand how to convert octal to decimal but am having a hard time writing the program. However, I find out about the setioflags method but when writing the following code it does not convert octal to decimal, actually it converts octal to hex instead.
1 2 3
cout<< "The octal vale of 00076 is" <<setiosflags(ios::dec)<<00076<<endl;
3e was returned instead of 62. Can anyone help me with this program.
You don't need to set the ios flags to decimal, they are set to decimal by default unless you change it. I get 62 just fine by just doing cout << 00076 << endl;
I'd just like to point out that 00076 merely represents a value. 076, 62, and 0x3E all mean the same thing.
What you're doing doesn't really make any sense. The program could be changed to
std::cout <<"The octal vale of 00076 is "<<62<<std::endl;
and it would compile to exactly the same machine code.
Thanks,
Is there an easier way to convert octal to decimal. I just need an easy way to convert an octal input to decimal. The reason for this is that I need to check the user input, there are certain octal values that can be accepted and I think it will be best to convert the ranges to decimal and convert the user input and compare. I have been having problems with comparing two octal numbers.
Yes
int n=076;//octal value std::cout<<"the decimal value of n is "<<dec<<n<<std::endl;//prints 62
Note: as helios says this is just reformatting a number expressed in one base (8) to another base (10)..........both meanings are identical.
Thanks Buffbill,
I have one more question, how to store 62 into a variable instead of printing it out. So I want to convert an octal number to decimal and store the decimal value into a variable.