much text, one cout<< ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Brawley

    much text, one cout<< ?

    Is this going to cause me a portability problem?
    Purpose: make it easy for me to write my 'help' text block in the .cpp file,
    just as I wish it to be output 'formatted-ly' to the user upon demand while
    the program is running.

    #include <iostream>;
    using namespace std;

    int main() {
    cout<<"these \
    lines\n\
    are \
    much \
    longer\n\
    than \
    this.\n";

    return 0;
    }

    (Or, any better suggestion, without more code?)
    Thanks


    --
    Peace
    JB
    jb@tetrahedrave rse.com
    Web: http://tetrahedraverse.com





  • Victor Bazarov

    #2
    Re: much text, one cout&lt;&lt; ?

    John Brawley wrote:
    Is this going to cause me a portability problem?
    It's not valid C++, so, yes, you bet.
    Purpose: make it easy for me to write my 'help' text block in the
    .cpp file, just as I wish it to be output 'formatted-ly' to the user
    upon demand while the program is running.
    >
    #include <iostream>;
    using namespace std;
    >
    int main() {
    cout<<"these \
    lines\n\
    are \
    much \
    longer\n\
    than \
    this.\n";
    >
    return 0;
    }
    >
    (Or, any better suggestion, without more code?)
    Thanks
    Surround every line of text with double quotes.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

      #3
      Re: much text, one cout&lt;&lt; ?

      On 2008-02-09 19:15, John Brawley wrote:
      Is this going to cause me a portability problem?
      Purpose: make it easy for me to write my 'help' text block in the .cpp file,
      just as I wish it to be output 'formatted-ly' to the user upon demand while
      the program is running.
      >
      #include <iostream>;
      using namespace std;
      >
      int main() {
      cout<<"these \
      lines\n\
      are \
      much \
      longer\n\
      than \
      this.\n";
      >
      return 0;
      }
      >
      (Or, any better suggestion, without more code?)
      If you have a number of string literals after each other they will be
      concatenated when compiling, just write each line enclosed by double quotes:

      #include <iostream>

      int main()
      {
      std::cout <<
      "Here begins a number "
      "of long lines";
      }


      Do not forget adding newlines (\n) wherever you need one.

      --
      Erik Wikström

      Comment

      • James Kanze

        #4
        Re: much text, one cout&lt;&lt; ?

        On Feb 9, 8:32 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        John Brawley wrote:
        Is this going to cause me a portability problem?
        It's not valid C++, so, yes, you bet.
        What's wrong with it? It's not good C++, but it looks legal to
        me.
        Purpose: make it easy for me to write my 'help' text block
        in the .cpp file, just as I wish it to be output
        'formatted-ly' to the user upon demand while the program is
        running.
        #include <iostream>;
        using namespace std;
        int main() {
        cout<<"these \
        lines\n\
        are \
        much \
        longer\n\
        than \
        this.\n";
        return 0;
        }
        (Or, any better suggestion, without more code?)
        Surround every line of text with double quotes.
        Which will make the code more readable (since the following
        lines can be correctly indented), but won't change anything in
        the meaning of the code.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • James Kanze

          #5
          Re: much text, one cout&lt;&lt; ?

          On Feb 9, 7:15 pm, "John Brawley" <jgbraw...@char ter.netwrote:
          Is this going to cause me a portability problem?
          Purpose: make it easy for me to write my 'help' text block in the .cpp file,
          just as I wish it to be output 'formatted-ly' to the user upon demand while
          the program is running.
          #include <iostream>;
          using namespace std;
          >
          int main() {
          cout<<"these \
          lines\n\
          are \
          much \
          longer\n\
          than \
          this.\n";
          return 0;
          }
          (Or, any better suggestion, without more code?)
          In general (or at least in theory), every program has a
          potential portability problem. All implementations have
          ressource limits, and if you exceed them, you're going to get
          into trouble. As long as your string doesn't exceed the maximum
          string length of the compiler, however, and there's enough room
          for it in the memory in which the program runs, this should not
          cause a problem.

          To tell the truth, I have no idea what the maximum string length
          for a compiler might be, but I have, on occasion, had strings
          which represented tens of lines of output---several KB in
          all---, without the slightest problem. (One would hope, of
          course, that the maximum string length would only be limited by
          the memory available to the compiler, but one never knows.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • John Brawley

            #6
            Re: much text, one cout&lt;&lt; ?


            "John Brawley" <jgbrawley@char ter.netwrote in message
            news:1bmrj.67$W F1.33@newsfe06. lga...
            Is this going to cause me a portability problem?
            Purpose: make it easy for me to write my 'help' text block
            in the .cpp file, just as I wish it to be output 'formatted-ly' to
            the user upon demand while the program is running.
            >
            #include <iostream>;
            using namespace std;
            >
            int main() {
            cout<<"these \
            lines\n\
            are \
            much \
            longer\n\
            than \
            this.\n";
            >
            return 0;
            }
            Thanks Victor, James, Erik, Kai-Uwe;
            I deduce:
            Mine is legal (it works, no serious portability problem), but poor C++
            practice. (Even though the console window of help text looks exactly like I
            want it to.)
            I'll think about it more but probably take your suggestions and use
            doub-quotes around every line. It'll be 'uglier' to me in the .cpp file,
            but
            I'd just as soon do things "the right way" whenever possible.
            Appreciated.
            (It was unexpectedly interesting to see the first two kind helpers
            disagreeing with each other... (*grin*) ...over what I feared was 'way too
            simple (newbie-ignorant) a question for an actual disagreement from
            experienced programmers to arise.)
            Have a nice day!

            --
            Peace
            JB
            jb@tetrahedrave rse.com
            Web: http://tetrahedraverse.com





            Comment

            • Martin York

              #7
              Re: much text, one cout&lt;&lt; ?


              Oops sorry missed the \ at the end of the line.

              Comment

              • Ian Collins

                #8
                Re: much text, one cout&lt;&lt; ?

                Martin York wrote:
                Oops sorry missed the \ at the end of the line.
                And the context!

                --
                Ian Collins.

                Comment

                • Martin York

                  #9
                  Re: much text, one cout&lt;&lt; ?

                  I'll think about it more but probably take your suggestions and use
                  doub-quotes around every line. It'll be 'uglier' to me in the .cpp file,
                  but
                  I'd just as soon do things "the right way" whenever possible.
                  Appreciated.
                  This is a perfect example of a place where you can separate data and
                  code.
                  You don't really want the text in the source. If you move the data out
                  of the code into a separate file you increase readability (as it is
                  not ugly) you increase portability (as you can then localize the
                  string without changing the code (a bit of extra work required but not
                  much)) you reduce your probability of running into compiler specific
                  limits.

                  Personally I would do somthing like this:

                  1) Put the text in a seprate file:

                  std::ifstream text(getLocalis edFileName());
                  std::copy(std:: istreambuf_iter ator<char>(text ),std::istreamb uf_iterator<cha r>(),std::ostre am_iterator<cha r>(std::cout)) ;


                  Comment

                  • James Kanze

                    #10
                    Re: much text, one cout&lt;&lt; ?

                    On Feb 10, 5:17 pm, "John Brawley" <jgbraw...@char ter.netwrote:
                    "John Brawley" <jgbraw...@char ter.netwrote in message
                    [...]
                    I'll think about it more but probably take your suggestions
                    and use doub-quotes around every line. It'll be 'uglier' to
                    me in the .cpp file, but I'd just as soon do things "the right
                    way" whenever possible.
                    I'm curious. Why uglier? Your solution doesn't allow
                    indentation of any but the first line---using separate string
                    literals (concatenated by the compiler) does.

                    Of course, if this is really a more or less large body of text
                    that you want to maintain as text, the best solution is to do
                    just that---maintain it as text, in a separate file. If you
                    still want to have it "compiled into" your program (there are
                    pros and contras to this), then a simple preprocessor will
                    convert it to a C style array. Something like:

                    #! /usr/bin/awk
                    BEGIN {
                    print "// Automatically generated file"
                    print "// DO NOT EDIT"
                    print ""
                    print "#include \"helpText.hh\" "
                    print ""
                    print "char const helpText[] ="
                    }
                    {
                    print " \"" $0 "\""
                    }
                    END {
                    print ";"
                    }

                    Then create (manually) the necessary "helpText.h h" header (which
                    is just one line), and the work is done.

                    (FWIW: I'd strongly recommend this. Maintaining text in a
                    format that has a C++ string literal per line will be a pain,
                    e.g. anytime one line gets to long, and you have to reformat the
                    paragraph. Whereas if the file is pure text, and the editor
                    recognizes it as such, it will do the reformatting for you.)

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    • James Kanze

                      #11
                      Re: much text, one cout&lt;&lt; ?

                      On Feb 11, 8:06 am, Martin York <Martin.YorkAma ...@gmail.comwr ote:

                      [...]
                      Personally I would do somthing like this:
                      1) Put the text in a seprate file:
                      std::ifstream text(getLocalis edFileName());
                      std::copy(std:: istreambuf_iter ator<char>(text ),std::istreamb uf_iterator<cha r>(),std::ostre am_iterator<cha r>(std::cout)) ;
                      Whether this is a good idea or not depends on context. It
                      definitly makes deployment more complicated, because you have to
                      deliver two files, rather than one. And you have to somehow
                      ensure that the program can find the second file, which isn't
                      necessarily trivial. Anytime you can reasonably put everything
                      into a executable, and just deliver that single file, it's a
                      definite advantage. (I've seen far too many programs where the
                      only message you get from help is something along the lines
                      "Cannot open .../help.txt". Not very helpful, really.)

                      Of course, it all depends. If you need a number of different
                      files anyway, one more isn't going to change anything, and it
                      does make maintaining different versions (e.g. depending on
                      language) slightly easier. Back in the old days, of course,
                      when memory was tight, you'd always use separate file, since
                      that way, it wasn't part of the binary imagine in memory; that
                      could still be a consideration today if you have very large help
                      files. (More likely, today, the help command will be a separate
                      executable, with a name something like /usr/bin/firefox or
                      C:/Program Files/Mozilla_Firefox/firefox.exe:-). In which case, of
                      course, you'll definitely keep the help text in a separate file.)

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      • John Brawley

                        #12
                        Re: much text, one cout&lt;&lt; ?


                        "James Kanze" <james.kanze@gm ail.comwrote in message
                        news:44ffceb1-4ef3-4e78-911e-70cb5d2c6704@s3 7g2000prg.googl egroups.com...
                        On Feb 10, 5:17 pm, "John Brawley" <jgbraw...@char ter.netwrote:
                        "John Brawley" <jgbraw...@char ter.netwrote in message
                        [...]
                        I'll think about it more but probably take your suggestions
                        and use doub-quotes around every line. It'll be 'uglier' to
                        me in the .cpp file, but I'd just as soon do things "the right
                        way" whenever possible.
                        I'm curious. Why uglier? Your solution doesn't allow
                        indentation of any but the first line---using separate string
                        literals (concatenated by the compiler) does.

                        (James, I replied but it's not here; it's possible I sent it to you directly
                        (wrong key?) If so, I apologize....
                        Uh, and please tell me if yuou got it; I'd have to rewrite it otherwise?)


                        --
                        Peace
                        JB
                        jb@tetrahedrave rse.com
                        Web: http://tetrahedraverse.com




                        Comment

                        • John Brawley

                          #13
                          Re: much text, one cout&lt;&lt; ?

                          (Apologies; sent wrong place; now right place: here.)

                          On Feb 10, 5:17 pm, "John Brawley" <jgbraw...@char ter.netwrote:
                          "John Brawley" <jgbraw...@char ter.netwrote in message
                          [...]
                          I'll think about it more but probably take your suggestions
                          and use doub-quotes around every line. It'll be 'uglier' to
                          me in the .cpp file, but I'd just as soon do things "the right
                          way" whenever possible.
                          I'm curious. Why uglier? Your solution doesn't allow
                          indentation of any but the first line---using separate string
                          literals (concatenated by the compiler) does.

                          Of course, if this is really a more or less large body of text
                          that you want to maintain as text, the best solution is to do
                          just that---maintain it as text, in a separate file. If you
                          still want to have it "compiled into" your program (there are
                          pros and contras to this), then a simple preprocessor will
                          convert it to a C style array. Something like:

                          #! /usr/bin/awk
                          BEGIN {
                          print "// Automatically generated file"
                          print "// DO NOT EDIT"
                          print ""
                          print "#include \"helpText.hh\" "
                          print ""
                          print "char const helpText[] ="
                          }
                          {
                          print " \"" $0 "\""
                          }
                          END {
                          print ";"
                          }

                          Then create (manually) the necessary "helpText.h h" header (which
                          is just one line), and the work is done.

                          (FWIW: I'd strongly recommend this. Maintaining text in a
                          format that has a C++ string literal per line will be a pain,
                          e.g. anytime one line gets to long, and you have to reformat the
                          paragraph. Whereas if the file is pure text, and the editor
                          recognizes it as such, it will do the reformatting for you.)
                          James Kanze

                          Hi James (and all).
                          These several last additions to the thread have me a bit confused....
                          Let me tell you what and why, then I will put in the actual function
                          that outputs the help file (you may have to unscramble if Usenet
                          linewraps)...

                          The program runs forever (user manually terminates).
                          The user can forget what keyboard keys are used to do whatever, and I
                          want him/her to _need_ to remember only the 'h' key. Thus the help page
                          needs to be in the executable thus in memory (yes, I want to
                          'distribute' one .exe file and a single .txt file).

                          All the lines of text look to me in the .cpp file, pretty much exactly
                          as they do on the console screen, with either the \ (continue on the
                          next line) or the " " double quotes-around, so I'm not sure what the
                          disagreement is about....

                          Here's the actual function, copied out of my working .cpp file:

                          void tvhelp() {
                          cout<<"\nKeyboa rd control keys (case is significant):\n \n"
                          "1,2,3,4,5, and [shift] 1,2,3,4,5 :\n"
                          "decrease or increase containment sphere radius by\n"
                          "0.1, 0.01, 0.001, 0.0001, 0.00001\n"
                          "r =manual ReScan NOW!; [shift]-R =turn off automatic reScan if on\n"
                          "s =write snapshot (.3D; view with Graph3D.exe) and .TVR files NOW!\n"
                          "p =write a PovRay file NOW!\n"
                          "h = this help screen (any time)\n"
                          "q =quit (end program; write final .3D and .TVR files); \n"
                          "[spacebar] =get a NOW! one-line pack status report containing:\n"
                          "pHi: greatest, pLo: smallest, pAvg: average, overlap,\n"
                          "gRad: present container radius, zC: per-piont overlaps, inner loop\n"
                          "Any other key: \"not a valid key\", (returns to the
                          rogram)\n\n\n"; }

                          (last line broke in emailer; don't know why; "returns to the
                          program"...)

                          I call it once at startup, so the user knows it's there and how to get
                          back to it, then it can be returned to the console screen by hitting the
                          'h' key (which control is inside a switch statement with all those above
                          case:es also....)

                          User is usually in "live" control, hitting the spacebar every now and
                          then, to judge the state of the packing by the info displayed, so he's
                          'right there' anyway.

                          So, does this (I guess this is its 'context'?) explain what/why better?
                          The console screen looks exactly like that, without the \n s and the
                          quote characters (and the C++ codewords, etc.)
                          It looked exactly like that also when using the continuation backslashes
                          instead, except the left hand side (continuation characters preserved
                          the indentations not present here). Six of one, half-dozen of the
                          other. This is a little "uglier" to me (all those quote " characters
                          instead of just two...)

                          Peace
                          JB
                          jb@tetrahedrave rse.com
                          Web: http://tetrahedraverse.com




                          Comment

                          Working...