0

so currently I am trying to output a string and to keep my code clutter to a minimum it's a conditional

cout << ((maxam>=c) ? "Fuse was blown." : "Fuse was not blown.\nMaximum used current was [variable] A") << endl;

now I seem to remember from somewhere that you can have a value in the string without exiting the string. Something like "my variable is \%" Does anybody know of this?

I know that I could output it like

cout << "beginning of string " << variable << "end of string" << endl;

or

MyStr = "beginning" + variable + "end";
cout << MyStr << endl;

but I was curios about the ""\%""-method.

  • Sounds like a Python F-string, but C++ has no such thing. – Mark Ransom Jul 13 '20 at 15:07
  • 1
    Does this answer your question? ["string interpolation" in C++: Construct a std::string with embedded values (e.g. for error messages)?](https://stackoverflow.com/questions/37956090/string-interpolation-in-c-construct-a-stdstring-with-embedded-values-e-g) Waaay down at the bottom there is an answer that says it should be available in C++20. – dandan78 Jul 13 '20 at 15:07

1 Answers1

0

Okay so thanks for the quick response.

Turns out yes, you can do that. However one must skip the cout. In the form

((maxam>=c) ? printf("Fuse was blown.\n"):printf("Fuse was not blown.\nMaximal consumed current was %d A.\n",maxam));

which looks quite spiffy (don't know about the performance though)