3

I have tried this

cout. setf(ios::oct, ios::basefield) ;
cout << 79 << endl;

It works but using manipulator setiosflags

cout << setiosflags (ios::oct) << 79 << endl;

It doesn't work, there's still 79 printed at the screen.
Although I heard setiosflags is the alternative of `setf.

Then how to print out the decimal number as octal number using setiosflags?

2
  • what is the meaning of "it doesn't work" ? If you get compiler errors you have to include them in the question Commented Aug 16, 2019 at 11:18
  • 1
    I've changed my mind and voted to reopen the question. I think it's useful for future research about the problem. Commented Aug 16, 2019 at 11:44

4 Answers 4

6

Keep It Simple and Stupid (or shortly KISS):

std::cout << std::oct << 79 << std::endl;

std::oct is a syntactic sugar for str.setf(std::ios_base::oct, std::ios_base::basefield), which (as you noticed) is one of the ways to force stream to print integral values in octal notation.

See it online

Sign up to request clarification or add additional context in comments.

Comments

4

You need to reset the flags first using the std::resetiosflags:

#include <iostream>
#include <iomanip>

int main()
{
  int x = 42;
  std::cout << std::resetiosflags(std::ios_base::dec)
            << std::setiosflags(std::ios_base::hex | std::ios_base::showbase)
            << x;
}

The | std::ios_base::showbase part is optional.

1 Comment

Can you briefly explain @Ron why do we need to use resetiosflags
0

you can use also

 printf("%o",79);

3 Comments

The OP clearly asks about c++ I/O manipulators. Your answer doesn't cover that.
@πάνταῥεῖ With that approach, my answer should be downvoted as well, because it doesn't use std::setiosflags
@Yksisarvinen But your answer shows a way how to use I/O manipulators.
0

Just try this your issue will be resolve. cout << hex << 79; cout << oct << 79;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.