LearnFrenzy
Sign in Take a free test

Input/Output with cin and cout · Predict the Output · Question 2

setfill with left and right alignment Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following C++ program?

main.cpp
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::setfill('*') << std::setw(6) << 42 << '|'
              << std::left << std::setw(6) << 42 << '|'
              << std::setw(6) << "ab" << "|\n";
    return 0;
}
  • A ****42|42****|ab****| Correct
  • B 42****|****42|****ab|
  • C ****42|42****|****ab|
  • D 42|42 |ab |
Correct Answer: Option A

The fill character and std::left are sticky; the width must be given again for every field. Right alignment is the default.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links