473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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@tetrahedraverse.com
Web: http://tetrahedraverse.com

Feb 9 '08 #1
12 1956
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
Feb 9 '08 #2
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
Feb 9 '08 #3
On Feb 9, 8:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 9 '08 #4
On Feb 9, 7:15 pm, "John Brawley" <jgbraw...@charter.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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 9 '08 #5

"John Brawley" <jg*******@charter.netwrote in message
news:1b*************@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@tetrahedraverse.com
Web: http://tetrahedraverse.com

Feb 10 '08 #6

Oops sorry missed the \ at the end of the line.
Feb 11 '08 #7
Martin York wrote:
Oops sorry missed the \ at the end of the line.
And the context!

--
Ian Collins.
Feb 11 '08 #8
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(getLocalisedFileName());
std::copy(std::istreambuf_iterator<char>(text),std ::istreambuf_iterator<char>(),std::ostream_iterato r<char>(std::cout));
Feb 11 '08 #9
On Feb 10, 5:17 pm, "John Brawley" <jgbraw...@charter.netwrote:
"John Brawley" <jgbraw...@charter.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.hh" 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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Feb 11 '08 #10
On Feb 11, 8:06 am, Martin York <Martin.YorkAma...@gmail.comwrote:

[...]
Personally I would do somthing like this:
1) Put the text in a seprate file:
std::ifstream text(getLocalisedFileName());
std::copy(std::istreambuf_iterator<char>(text),std ::istreambuf_iterator<char>(),std::ostream_iterato r<char>(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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 11 '08 #11

"James Kanze" <ja*********@gmail.comwrote in message
news:44**********************************@s37g2000 prg.googlegroups.com...
On Feb 10, 5:17 pm, "John Brawley" <jgbraw...@charter.netwrote:
"John Brawley" <jgbraw...@charter.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@tetrahedraverse.com
Web: http://tetrahedraverse.com


Feb 11 '08 #12
(Apologies; sent wrong place; now right place: here.)

On Feb 10, 5:17 pm, "John Brawley" <jgbraw...@charter.netwrote:
"John Brawley" <jgbraw...@charter.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.hh" 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<<"\nKeyboard 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@tetrahedraverse.com
Web: http://tetrahedraverse.com


Feb 11 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
4
by: karolina | last post by:
Hi I am playing around with visual studio 7.0. Why does the code below do like it does? Where do I set the character set to use in cout, properties->configuration properties->General->character...
4
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
4
by: wangzhihuii | last post by:
Hi all, I'm really confused, can cout<<""; contribute anything to the routine ?!! my programm won't work properly without this trivial sentence. Sincerely vivian
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
46
by: suresh | last post by:
Hi, For this code snippet, I get the following output, which I am unable to understand. (2^31 = 2147483648) cout<< -2147483648 << endl; cout << numeric_limits<int>::min() <<',' <<...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.