<Help> Simple Pause Needed.

M

Mike Wahler

Jonathan Mcdougall said:
Mmm.. I though \n was not portable, I should have informed myself.
Let me rephrase.

I prefer using std::endl since it is clearer than a bunch of \n.
It is also supposed to flush the buffer, but flushing the buffer
does not necessrily mean that the data is written.

Again, Eh?

-Mike
 
B

Buster

da Vinci said:
4) learn about arrays and vectors

Very soon! I actually want to use an array for the character input at
certain places. This program will crash if someone enters two or more
characters when it is specifically looking for a Y or N. If the user
mis-keyed and hit NB or something (I did once and that is how I found
the problem) it will lock up the program. If I used an array, then
just looked at the [1] position, if they entered NB, it would only see
the N. I think.... :)

Use a string, and use getline. You can look at the first position of
a string using the [] indexing operator, but remember the positions
are numbered from zero.
Is there a portable way to clear a screen without using a mass of endl
or \n's?
No.


Your speaking Greek again. :) I am vaguely familiar with Arrays and
plan to tackle them next.

It's your call, obviously, but vectors are easier to use. Arrays aren't
*very* hard but there are subtleties.
So would this be good to use as a continue type statement?

bool continue;
continue = false;

while (continue == false) {
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if (continue == 'Y')
continue = true;
else
continue = false;
}

No, because `continue' is a keyword. And because 'cin >> x' where x
is a bool expects either 0/1 or true/false or vrai/faux depending on
various factors, but never Y/N. Use a string and use getline. Apart
from that, the principle is sound and you could do:

bool carry_on = false;

while (carry_on == false)
{
std::cout << "Do you want to continue (Y/N)?\n";

std::string response;
std::getline (std::cin, response);

if (response [0] == 'Y') carry_on = true;
else carry_on = false;
// or replace the previous two lines with:
// carry_on = (response [0] == 'Y');
}

What would be simpler, though, is:

while (true)
{
std::cout << "Do you want to continue (Y/N)?\n";

std::string response;
std::getline (std::cin, response);

if (response [0] != 'Y') break;
}

Regards,
Buster.
 
J

Jonathan Mcdougall

Could be, except that you use 'continue' as a char and a boolean,
but you understand. Personaly, something like

do
{
cout << "Do you want to continue(Y/N)?";
cin >> continue;

} while ( std::toupper(continue) != 'Y' );

If you are not familiar with this version of while, you can
stay with the preceeding one, it is quite good. Or try this

while ( true ) // infinit loop
{
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if ( std::toupper(continue) == 'Y' )
break; // exits the loop

Thanks to Buster for reminding me that continue is a keyword,
so it is illegal to use it as a variable name.

Sorry about that,


Jonathan
 
D

da Vinci

return 0;
} // Ends Main

This was actually funny :)

It didn't seem so at the time, but now I do see the humor in it. :)
Be careful with them though. I suggest you take a look to std::vector
before arrays, since arrays are error-prone, but I suppose your
teacher knows what she's doing.

Actually, I am 2 months ahead in all of my assignments. So I am
basically self teaching myself function use and everything else until
the class catches up. I was personally going to learn arrays next, but
will look at vectors first if that is the better approach. Thing is, I
know the basics of arrays already. I know nothing of vectors except
what I learned in Physics.... but I doubt that is what were talking
about here. :)
Both (if I understand correctly) :

// variables named
void f(int a, int b);

int main()
{
f(1, 2);
}

// ditto
void f(int a, int b)

OK, I think I see what you mean. Basically, instead of declaring a
function with

int This_Function(int, int, int);

use

int This_Function (int var1, int var2, int var3)

then write the code for the function starting with the same line.
Basically, take the function I am writing, take the first line of it,
and post that at the top of the code as well?
No. Depending on the company standards, you will do things differently,
but for programs like that, try to make it as clear as possible.

OK. I knew that different companies had different standards they like
their code to follow.... wasn't sure if there was a globally
"gentlemans agreement" kind of deal outside of the ANSI/ISO that
everyone adhered to.
No. C++ can be used in toasters, you know.

Bummer. I knew it was diverse... but toasters....? :)
Or have a Google on std::vector. Do not stop on details like
the weird std:: notation. Try to understand the whole.

I understand the std:: notation. At this level, I perfer to just call
it into the global namespace using "using namespace std;" and not do
all the extra typing. We are not going to use our own namespaces in
this class so I don't bother to much with it. Unfortunantly, beacuse
of that, I have less knowledge on what the whole namespace thing is
all about except for those responses a month ago when I had asked
about them. Thanks for the link, will check it out.
do
{
cout << "Do you want to continue(Y/N)?";
cin >> continue;

} while ( std::toupper(continue) != 'Y' );

If you are not familiar with this version of while, you can
stay with the preceeding one, it is quite good. Or try this

while ( true ) // infinit loop
{
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if ( std::toupper(continue) == 'Y' )
break; // exits the loop
}

Ah, so you can use break to end any loop, not just a switch? I didn't
know that. That will help a great deal.
Hehe.. depends on the people.

That ok. I read the FAQ section on that. I always take things with a
grain of salt and try not to get up in arms at negative replies. Life
is to short to induce a heart attack... especially when your only 25.
:)
 
D

da Vinci

Use a string, and use getline. You can look at the first position of
a string using the [] indexing operator, but remember the positions
are numbered from zero.

Ah, thats right. Thanks for the correction.
No, because `continue' is a keyword.

Ah, I knew that! Just slipped my mind when making the example. That
just shows I need to pay more attention to the little details and not
take things for granted in coding. :)
What would be simpler, though, is:

while (true)
{
std::cout << "Do you want to continue (Y/N)?\n";

std::string response;
std::getline (std::cin, response);

if (response [0] != 'Y') break;
}

Yes, I like this method much better. It was in Johnathans last post
that I learned you can "break" from any type of loop and not just
switch.

Thanks!!
 
B

Buster

Basically, take the function I am writing, take the first line of it,
and post that at the top of the code as well?

Mostly, yes. In a 'real' program you might put the declarations
in a header file (and/or in a namespace or possible a class...)
but if you want to send your teacher a single compilable file
and you need to refer to a function before it is defined then
copy-paste is a nice easy way to do the forward declaration.

[...]
Ah, so you can use break to end any loop, not just a switch? I didn't
know that. That will help a great deal.

I usually have to look it up. Let's see (from Borland online help):
"Use the break statement within loops to pass control to the first
statement following the innermost switch, for, while, or do block."

That last word always got me. It doesn't have to be a block, it can
be a statement, as in

for (int i = 0; i < 10; ++ i) if (std::rand () < 10) break;
That ok. I read the FAQ section on that. I always take things with a
grain of salt and try not to get up in arms at negative replies. Life
is to short to induce a heart attack... especially when your only 25.
:)

Amen.

Regards,
Buster.
 
J

Jonathan Mcdougall

cout << "\n\n\nSelection: ";
Again, Eh?

I prefer using std::endl since it is clearer than a bunch of \n.

What's more, since std::cout is buffered, std::end flushes it. That
does not mean the output will automatically get on the stdout though,
since operating systems usually provide some buffering themselves
(this applies to all streams). To make sure the data gets written,
you should use implementation-specific functions which deal with
the operating system's buffer.

But usually, flushing the buffer is enough. Note that std::cout
flushes the buffer when it is destroyed (ie at the end of the
program), but you may have no time to see the output (in case
of outputting to stdout when there is a screen on your system)
so make sure you flush the buffer yourself when you are
finished writing.

Finally, '\n' may flush the buffer or not, this is not defined
(just as it may flush it with any character).

Interesting note that I found in my researches, \n is
translated into its equivalent depending on the platform.
For example, when writing to a text file, Windows needs a
\r\n and Mac gets a \r.


Jonathan
 
W

WW

Jonathan Mcdougall wrote:
[SNIP]
I prefer using std::endl since it is clearer than a bunch of \n.

What's more, since std::cout is buffered, std::end flushes it. That
[SNIP]

Sometimes it is better to not use std::endl but make a flush at the end.
For example if you are printing in a loop, there can be serious difference
in time.
 
M

Mike Wahler

I prefer using std::endl since it is clearer than a bunch of \n.

I disagree. Any C++ programmer will know what '\n' means.
As readily as what e.g. 'int' means. I suppose it can
vary among people, but I find '\n' simpler reading.
What's more, since std::cout is buffered, std::end flushes it.

Yes, I know. That's 'std::endl' :)
That
does not mean the output will automatically get on the stdout though,

Where did you hear that? What do you think 'flush' means,
in the context of an output stream?
since operating systems usually provide some buffering themselves
(this applies to all streams).

Now you're outside the language. Nothing to do with
flushing a stream. And your generalization 'all streams'
is incorrect.
To make sure the data gets written,
you should use implementation-specific functions which deal with
the operating system's buffer.

I vehemently disagree. If this were the case, C++ would
fail in its role as a platform independent language.
But usually, flushing the buffer is enough.

"Usually", flushing a buffer is not necessary at all.
Note that std::cout
flushes the buffer when it is destroyed

During 'cout's destruction, it (the stream) is flushed.
(ie at the end of the
program), but you may have no time to see the output

Huh?

So you're saying that if I run:

#include <iostream>
int main()
{
std::cout << "Hello world\n";
return 0;
}

... that the output might disappear immediatly after
appearing on the output device to which 'cout' is
attached? I don't think so. An IDE might interfere
with a 'screen' output, but that's outside the language.
(in case
of outputting to stdout when there is a screen on your system)

Absolutely not true.
so make sure you flush the buffer yourself when you are
finished writing.

Unnecessary, unless you require that the output appear
*immediately* after inserting data into the stream.
And it's not even necessary if a 'cout' insertion
is followed by a 'cin' extraction, since by defintion,
a 'cin' extraction will first cause 'cout' to be flushed
if necessary (look up 'tie()').
Finally, '\n' may flush the buffer or not,

'\n' is not an operation. It's a value, that's all.
It cannot 'do' anything. A function might behave
in a particular way upon encountering it, this
behavior perhaps causing a flush.
this is not defined
(just as it may flush it with any character).

Interesting note that I found in my researches, \n is
translated into its equivalent depending on the platform.

That is common knowledge. It's a feature of a 'text mode'
stream, well documented.
For example, when writing to a text file, Windows needs a
\r\n and Mac gets a \r.

I know this.

No offense, but I think you're suffering from a few
misconceptions.

-Mike
 
J

Jonathan Mcdougall

I prefer using std::endl since it is clearer than a bunch of \n.
I disagree. Any C++ programmer will know what '\n' means.
As readily as what e.g. 'int' means. I suppose it can
vary among people, but I find '\n' simpler reading.

This is a personal opinion. I like the look of

std::cout << "hey" << std::endl;

better than

std::cout << "hey\n";
Where did you hear that? What do you think 'flush' means,
in the context of an output stream?

Ok let me rephrase that :)

Flushing will send the stream's content to stdout, but the operating
system is free to do whatever it wants with it.
Now you're outside the language.

I know.
Nothing to do with
flushing a stream. And your generalization 'all streams'
is incorrect.
Explain.


I vehemently disagree.

woaa :)
If this were the case, C++ would
fail in its role as a platform independent language.

I think we agree that after a buffer has been flushed, the
operating system is free to do whatever it wishes to,
like waiting for another operation to terminate before
completing writing the data.

Using implementation-specific functions will give you
more control on the way the data reaches
destination. That's all I meant.
"Usually", flushing a buffer is not necessary at all.

What do you mean?
Huh?

So you're saying that if I run:
No.

#include <iostream>
int main()
{
std::cout << "Hello world\n";
return 0;
}

.. that the output might disappear immediatly after
appearing on the output device to which 'cout' is
attached? I don't think so. An IDE might interfere
with a 'screen' output, but that's outside the language.

That is what I talked about, sorry, I was probably not
explicit enough.
Absolutely not true.

What is not true?
Unnecessary, unless you require that the output appear
*immediately* after inserting data into the stream.

That is what I meant, yes.
And it's not even necessary if a 'cout' insertion
is followed by a 'cin' extraction, since by defintion,
a 'cin' extraction will first cause 'cout' to be flushed
if necessary (look up 'tie()').

Who's talking about cin?
'\n' is not an operation. It's a value, that's all.
It cannot 'do' anything. A function might behave
in a particular way upon encountering it, this
behavior perhaps causing a flush.

To quote myself :
(just as it may flush it with any character).

That is common knowledge. It's a feature of a 'text mode'
stream, well documented.

So what?
I know this.

That post was not intended to teach you things you know, it
was primarly for the op. But thanks for the corrections.
No offense, but I think you're suffering from a few
misconceptions.

Perhaps.


Jonathan
 
M

Mike Wahler

Jonathan Mcdougall said:
This is a personal opinion.
Agreed.

I like the look of

std::cout << "hey" << std::endl;

better than

std::cout << "hey\n";

But note that the code one writes is quite often
read by others. This is imo good reasons to
follow 'convention'. Consistency Is Good(tm).

Also note that since 'endl' does cause a flush,
you're possibly slowing down the program, often
unnecessarily. This might not be noticable in some
cases, but could significantly impact performance
in others. E.g. writing 'endl' to an ofstream
attached to a file on a floppy disk, or other
inherently 'slow' device, such as a printer.
Ok let me rephrase that :)

Flushing will send the stream's content to stdout,

Flushing will send any data not yet written to the
device to which the stream is attached.
but the operating
system is free to do whatever it wants with it.

You're outside the language again. One cannot
comment further about 'behavior' at that point
from a language perspective.
I know.


Explain.

We're in a language perspective in this group. OS buffers
and their behavior are outside that domain.

"All streams" means all stream types, input, output,
or 'udpate' (input&output). Flushing is only defined
for output and 'update' streams.

Perhaps you were only being sloppy with terminology
and meant 'all output streams'. Which then would
omit 'update' streams. :)
woaa :)


I think we agree that after a buffer

make that 'stream', and by implication, that stream's
'stream buffer'. 'stream buffer' is a language construct,
not part of an OS.
has been flushed, the
operating system is free to do whatever it wishes to,

An OS has nothing to do with the description of the
flush operation of a C++ stream.
like waiting for another operation to terminate before
completing writing the data.

The device to which a stream is attached might be a 'real'
hardware device, or it might be one 'simulated' by an
OS (e.g. 'virtual device' such as a 'RAM-disk'). The
behavior of such devices and control of such by an OS
is outside of a discussion about C++ streams.
Using implementation-specific functions will give you
more control on the way the data reaches
destination. That's all I meant.

Well, yes, platform-specific features can give 'finer-grained'
control over a system, but that's nothing to do with C++
streams, which are an *abstraction* of external data.
What do you mean?

I mean that in many situations, the 'automatic' flushing
of an output stream will already occur, as in my 'cout'
followed by 'cin' example. Also it's often good enough
to simply let flushing happen automatically when an output
stream gets destroyed. Of course there can be exceptions
to this, depending upon the problem domain. "Usually"
can mean many things to various people. It usually :)
means "common to a person's experiences and observations."
That is what I talked about,

Which part of the paragraph I wrote after that code
are you talking about? Is your answer to the question
yes or no? Were you talking about the 'IDE interference'?
If so, C++ stream flushing has nothing to do with that.
sorry, I was probably not
explicit enough.

Effective communication can often be difficult. :)
What is not true?

Probably should have said "not necessarily true".

... that if std::cout is attached to a video display,
the data displayed after a flush might 'disappear'.
Nothing about the C++ streams will cause (or prevent) this
behavior. The data will be sent to the device (or if you
prefer, to the OS's interface for it). What happens after
that is outside the language.
That is what I meant, yes.

But you were talking about this happening at program
termination. I meant 'midstream' during execution
where data is being output.
Who's talking about cin?

I gave it as a common example. It's very common to
have code such as:

std::cout << "Your name: ";
std::cin >> name;

There's no need to flush cout to ensure the prompt
appears before the input request occurs. This is
done automatically by default, via 'tie()'.

Contrast C's stdin and stdout, where a prompt
using 'printf()' needs a subsequent flush
before an input request e.g. with 'scanf()',
to ensure the prompt appears first.
To quote myself :
(just as it may flush it with any character).

I suppose I could have misunderstood your meaning.

No, there is nothing preventing the stream buffer
being flushed at whatever point the library implementor
(or a coder using a custom stream buffer) determines is
appropriate (this could be upon insertion of a character
with a certain value such as '\n' or some other value,
but there's no requirement for it).

One condition under which a flush with *always* happen
is when a character (with *any* value) is inserted,
but the stream buffer is already full. The stream
buffer must first be flushed in order to make room
for the new character.

It just seemed to me that you felt you were pointing
out something not obvious to most C++ programmers.
That post was not intended to teach you things you know, it
was primarly for the op.

And anyone else reading. I try to intercept incorrect
or unclear information being imparted here. It also
gives others a chance to point out when I err myself.
But thanks for the corrections.

You're welcome. Please be assured that my motives
are educational, not attempts to appear 'smarter'
than you or anyone else. You should have seen the
confusion I sometimes suffered when learning C, so
many years ago. :)

Hope you've found my comments helpful.

-Mike
 
J

Jonathan Mcdougall

Be careful with them though. I suggest you take a look to std::vector
Actually, I am 2 months ahead in all of my assignments. So I am
basically self teaching myself function use and everything else until
the class catches up.

Unfortunately, that is quite frequent with school. The best thing you
can do now is to help people. That will make you learn a lot. There
is nothing better than a stupid question to make you doubt about your
knowledge, forcing you to search for the answer.

Maybe silly, but easy questions are often harder to answer than
difficult ones.
I was personally going to learn arrays next, but
will look at vectors first if that is the better approach. Thing is, I
know the basics of arrays already. I know nothing of vectors except
what I learned in Physics.... but I doubt that is what were talking
about here. :)

He. _Simply put_, a vector is an array which grows dynamically.
But since it does all the memory management itself, it is much
easier and safer to use.
OK, I think I see what you mean. Basically, instead of declaring a
function with

int This_Function(int, int, int);

use

int This_Function (int var1, int var2, int var3)

then write the code for the function starting with the same line.
Basically, take the function I am writing, take the first line of it,
and post that at the top of the code as well?

If you want to put declaration in the same file, yes. If not, do
the same thing, but paste it in another file :)
OK. I knew that different companies had different standards they like
their code to follow.... wasn't sure if there was a globally
"gentlemans agreement" kind of deal outside of the ANSI/ISO that
everyone adhered to.

No, there are a couple of guidelines and you will learn them as you
post here.
I understand the std:: notation. At this level, I perfer to just call
it into the global namespace using "using namespace std;" and not do
all the extra typing.

Namespaces are not only "extra typing". The basically avoid name
clashes. For example, having a class named 'vector' in your program
would clash with the standard vector class. Putting them into
namspace avoids that.

The thing is, you have absolutly no idea what classes are in the
namespace std in the headers you are including, so you could end
with weird compile errors.
We are not going to use our own namespaces in
this class so I don't bother to much with it.

The good thing is that most librairies are in a namespace themselves,
so your code is quite protected from that, unless you dump all the
names with the using declaration.
Unfortunantly, beacuse
of that, I have less knowledge on what the whole namespace thing is
all about except for those responses a month ago when I had asked
about them. Thanks for the link, will check it out.

Buy a book! If you never did programmation before, I recommend
something like "Thinking in C++" by Eckel (have a google, his book
is online). If not, get Accelerated C++ bu Koenig and Moo and
eventually the c++ bible "The C++ programming language" by
Stroustrup.
Ah, so you can use break to end any loop, not just a switch? I didn't
know that. That will help a great deal.

'break' breaks out of switch, do, while and for.

The most important thing is : stick up with this newsgroup, post, read,
comment, ask. That way you will learn much more than what you can do
in class or at home. Believe me. People here bark a lot, but they
don't bite :)


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top