tags:

views:

362

answers:

4

This has been driving me crazy. I know in C++ and in PHP you can fill a string or a file with hard coded text.

If I remember correctly this is how it is supposed to look:

var <<< DELIMITER
   Menu for program X
   1.Add two numbers
   2.Substract two numbers
   3.Multiply two numbers
   Please pick an option from (0-3);
DELIMITER

This can be used for menus or text that remains the same no matter what like a header. But without having to do

foobar << "Menu for program X" << endl << "1.Add two numbers" << endl << "2.Substract two numbers"
+11  A: 

It's called the HEREDOC syntax in PHP.

<?php

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

?>
Greg
+2  A: 

Multiline string literal?

Drakonite
This is similar to what the OP is looking for; I don't think you can put newline characters in it, though.
sheepsimulator
@sheepsimulator You have to escape them, or quote each line individually. Googling for "multiline string literal c++" brings up a few examples.
Drakonite
+7  A: 

C++ doesn't have any equivalent to PHP's HEREDOC syntax.

You can, however, do this in C++:

cout << "   Menu for program X\n"
        "   1.Add two numbers\n"
        "   2.Substract two numbers\n"
        "   3.Multiply two numbers\n"
        "   Please pick an option from (0-3);" << endl;

Or this in C:

printf( "   Menu for program X\n"
        "   1.Add two numbers\n"
        "   2.Substract two numbers\n"
        "   3.Multiply two numbers\n"
        "   Please pick an option from (0-3);\n" );
fflush(stdout);

Which is directly equivalent to PHP's HEREDOC syntax:

echo <<<EOT
   Menu for program X
   1.Add two numbers
   2.Substract two numbers
   3.Multiply two numbers
   Please pick an option from (0-3);
EOT;

The above syntax for C and C++ is treated by the compiler as one long string, by stitching them together. It has no other effect on the string literal, hence the need for '\n'.

greyfade
Note that the <<<LABEL in PHP however much it looks like the C++ is completely unrelated. <<<LABEL is just a funny looking string quote. << is an operator. I'll note that I managed to program for 35+ years using plain ol' string quotes, so PHP's HEREDOC literals seem to me to be just one more redundant bit of useless syntax.
Ira Baxter
I'm *quite* aware of the difference. And HEREDOC is a little more that just a "funny looking string quote," it's similar to double-quoted strings, but with preformatted text. It's extremely useful in situations where you are creating very long strings or lots of output. (And smart editors can recognize the HEREDOC syntax, and if you use, for example, <<<SQL, or <<<HTML, the editor can do appropriate syntax highlighting for you.) It's by no means redundant or useless. I'd also like to point out to you that in C++, the syntax I showed works equally well for printf() or other things.
greyfade
Your C++ to C equivalence is wrong. `endl` will print a newline, and flush the stream. Your C code neither prints a newline nor flushes the stream. It needs a `printf("\n"); fflush(stdout);`
GMan
@GMan: Quite right. Fixed. I typed it in a bit of a rush.
greyfade
A: 

In Windows you store long text as a resource or as a file.

You can store it in a Unixish by using internationalization (I18) libraries. It's a good hack to write the string in your program like printf( _("_xmenu_ %d, %d, %d"), 1, 2, 3) and then you can "translate" _xmenu_ into English, as well as whatever other languages you want to support. Because the translations are stored in another file, you don't have to look at them all the time and they're easy to change without recompiling.

Zan Lynx
In this particular case there is no need to store it in a different file since it is an experiment for myself but I do agree it is best practice to use external means to store static things since Users tend to love to change UI stuff.
ThorDivDev