LearnFrenzy
Sign in Take a free test

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

Printing a string with std::quoted Answer, explanation & discussion

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

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

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

int main() {
    std::string s = "say \"hi\"";
    std::cout << s << " | " << std::quoted(s) << " | " << std::quoted("a b", '\'') << '\n';
    return 0;
}
  • A say "hi" | "say \"hi\"" | 'a b' Correct
  • B say "hi" | "say "hi"" | "a b"
  • C say \"hi\" | "say \"hi\"" | 'a b'
  • D say "hi" | say "hi" | a b
Correct Answer: Option A

std::quoted wraps the text in quotes and escapes quotes inside it with a backslash. The second argument chooses a different quote character.

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