Is there any command like Pause or Nope in C++?

O

osmium

Jackie said:
I just want the programme to stop for a while.

Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");
 
A

Alvin

Jackie said:
I just want the programme to stop for a while.
Thanks.

Do you want the program to stop until you hit enter? If so, use cin like:

string str;
cin >> str;

That will stop the program till you press the Enter key.

Alvin
 
T

Tom

Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");

In C type logic ... I create a dummy string and use gets() to pause a
program.

#include <stdio.h>

char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);

-----------------

gets(junk); is good for clearing erroneous key strokes before
prompting for keyboard input in programs as well.

If erroneous keystrokes occur and the pause is critical ... you should
test for a null string. The following echos the erroneous keystrokes:

if(junk[0])
{
printf("routine_name(%d): junk = %s\n", __LINE__, junk);
gets(junk);
}

The "__LINE__" macro tells you the exact line and by including the
name of the routine in your printf statement you can find this pause
point easily.

Setting a break point would work too. But I usually don't debug using
break points. I like print statements. I often write them to log files
and turn the pause feature on and off with a #define.

#define pause 1
if(pause) gets(junk);

My apologies for not having sufficient C++ skills to make the code
clean and gorgeous. Fortunately C is fully supported in C++. My bet is
a C++ guru in here will teach us both a better way. :))
 
V

Victor Bazarov

Tom said:
Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");

In C type logic ... I create a dummy string and use gets() to pause a
program.

#include <stdio.h>

char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);


That is a *HUGE* security risk. What if I type more than 80 characters?
I suddely make your program _read my input_ into the memory that wasn't
designated for it.

No! It's _awful_. Never use it. NEVER!

V
 
J

Jacek Dziedzic

Alvin said:
Jackie wrote:




Do you want the program to stop until you hit enter? If so, use cin like:

string str;
cin >> str;

That will stop the program till you press the Enter key.

No, not really. Try it yourself. It will stop the program
till you press _something_ and then the Enter key.

- J.
 
T

Tom

Tom said:
:

I just want the programme to stop for a while.

Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");

In C type logic ... I create a dummy string and use gets() to pause a
program.

#include <stdio.h>

char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);


That is a *HUGE* security risk. What if I type more than 80 characters?
I suddely make your program _read my input_ into the memory that wasn't
designated for it.

No! It's _awful_. Never use it. NEVER!

V

Victor --

I believe you missed the "erroneous" part of the discussion?
The method I proposed is one I have used for years and was learned
from a C textbook. Typically I set the size of junk[] to 240
characters and use it for all types of input and output. But how do
you justify that size without other usages?

Check out the Microsoft help on gets(). You will see they use a size
of 81 for that example.

If you "get" 80 characters from a erroneous pool of xxxx characters
.... what remains is (xxxx - 80) erroneous characters. I suppose those
erroneous characters are a *HUGE* security risk and they are _awful_
too. Perhaps we should destroy all computers ... the risk is just
great for mankind.

I took the effort to read several of your replies. Your knowledge of
C++ is light years beyond mine as easily observed; however, you seem
to have made yourself a self appointed cop in the group?

And your preferred solution to the original question is .... ???

Please impress me with "how" to do things better and quit slamming
folks for existing outside your definition of intelligence. Go ahead,
teach me! I can learn from you. And just maybe ... you can learn a
thing or two as well?

-- Tom
 
D

Dakka

Tom said:
Tom said:
:

I just want the programme to stop for a while.
Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");

In C type logic ... I create a dummy string and use gets() to pause a
program.

#include <stdio.h>

char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);

That is a *HUGE* security risk. What if I type more than 80 characters?
I suddely make your program _read my input_ into the memory that wasn't
designated for it.
No! It's _awful_. Never use it. NEVER!

V

Victor --

I believe you missed the "erroneous" part of the discussion?
The method I proposed is one I have used for years and was learned
from a C textbook. Typically I set the size of junk[] to 240
characters and use it for all types of input and output. But how do
you justify that size without other usages?

Check out the Microsoft help on gets(). You will see they use a size
of 81 for that example.

If you "get" 80 characters from a erroneous pool of xxxx characters
... what remains is (xxxx - 80) erroneous characters. I suppose those
erroneous characters are a *HUGE* security risk and they are _awful_
too. Perhaps we should destroy all computers ... the risk is just
great for mankind.

I took the effort to read several of your replies. Your knowledge of
C++ is light years beyond mine as easily observed; however, you seem
to have made yourself a self appointed cop in the group?

And your preferred solution to the original question is .... ???

Please impress me with "how" to do things better and quit slamming
folks for existing outside your definition of intelligence. Go ahead,
teach me! I can learn from you. And just maybe ... you can learn a
thing or two as well?

-- Tom

For what its worth, I second this.
V - take a few deep breaths before you fire off next time. Proficiency
in C++ does not entitle you to bad manners.
--dakka
 
L

Luke Meyers

Tom said:
Tom said:
char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);

That is a *HUGE* security risk. What if I type more than 80 characters?
I suddely make your program _read my input_ into the memory that wasn't
designated for it.

I believe you missed the "erroneous" part of the discussion?

No, you just missed the point. He's right, it's an enormous security
risk and it's awful. You need to spend less time being defensive and
more time worrying about writing correct code.
The method I proposed is one I have used for years and was learned
from a C textbook.

Marvelous. It's wrong.
If you "get" 80 characters from a erroneous pool of xxxx characters
... what remains is (xxxx - 80) erroneous characters. I suppose those
erroneous characters are a *HUGE* security risk and they are _awful_
too. Perhaps we should destroy all computers ... the risk is just
great for mankind.

Uh, are you seriously not familiar with the concept of a buffer
overflow? You allocate an array of 80 characters, then pass that array
(as a pointer) to a function which will read in as many characters as
the user cares to type. The 81st character clobbers some random chunk
of memory, and you get undefined behavior; in many cases, this is an
easy security exploit.
I took the effort to read several of your replies. Your knowledge of
C++ is light years beyond mine as easily observed; however, you seem
to have made yourself a self appointed cop in the group?

Warning people not to heed dangerous and incorrect advice is a good
thing. Why is this personal for you?
Please impress me with "how" to do things better and quit slamming
folks for existing outside your definition of intelligence.

Nobody slammed you. The dangerous and ill-founded recommendation you
voiced received a well-deserved thrashing, but that, again, is a good
thing.
Go ahead,
teach me! I can learn from you. And just maybe ... you can learn a
thing or two as well?

Enough melodrama. This is a technical newsgroup, please make sound
technical arguments. Nobody's convinced of anything by hissy fits.

Luke
 
J

Jim Langston

Jacek Dziedzic said:
No, not really. Try it yourself. It will stop the program
till you press _something_ and then the Enter key.

- J.

This is what I also use, std::cin >> str;
but if you are not worried about platform compatability you could use
whatever your compiler allows to see if a key has been pressed. I know that
microsoft compilers generally have a kbhit used like:
while ( !kbhit() );

I've always wished this to be part of the standard cause it's just so
useful.
 
W

W Marsh

Tom said:
:

I just want the programme to stop for a while.

Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");


In C type logic ... I create a dummy string and use gets() to pause a
program.

#include <stdio.h>

char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);


That is a *HUGE* security risk. What if I type more than 80 characters?
I suddely make your program _read my input_ into the memory that wasn't
designated for it.

No! It's _awful_. Never use it. NEVER!

V

Victor --

I believe you missed the "erroneous" part of the discussion?
The method I proposed is one I have used for years and was learned
from a C textbook.

Wow, you've been programming for years and you STILL write code this
bad?
Typically I set the size of junk[] to 240
characters and use it for all types of input and output. But how do
you justify that size without other usages?

Check out the Microsoft help on gets(). You will see they use a size
of 81 for that example.

You will also see this comment on the line using gets:

// Danger: No way to limit input to 20 chars.
// Much preferable: fgets( line, 21, stdin );
// but you'd have to remove the trailing '\n'

If you make a habit of this sort of thing, please consider not giving
people advice.
 
A

Alvin

Jacek said:
No, not really. Try it yourself. It will stop the program
till you press something and then the Enter key.

- J.

In that case, getc() or getchar() will do the trick, but that's isn't C++.
That's why I suggested the cin way instead. :)

Alvin
 
J

Jack Klein

Your best bet is probably the system() function since
it lets you interact with the OS. For example, in Windows, try
system("pause");

In C type logic ... I create a dummy string and use gets() to pause a
program.

#include <stdio.h>

char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);

[snip]

No, gets() is bad. It is the only function in the entire C and C++
standard libraries that is impossible to use safely.

See specifically: http://c-faq.com/stdio/getsvsfgets.html
 
T

Tom

Tom said:
Tom wrote:
char junk[81];
printf("routine_name(%d):"
" hit return to continue the paused program ...\n", __LINE__);
gets(junk);

That is a *HUGE* security risk. What if I type more than 80 characters?
I suddely make your program _read my input_ into the memory that wasn't
designated for it.

I believe you missed the "erroneous" part of the discussion?

No, you just missed the point. He's right, it's an enormous security
risk and it's awful. You need to spend less time being defensive and
more time worrying about writing correct code.
The method I proposed is one I have used for years and was learned
from a C textbook.

Marvelous. It's wrong.
If you "get" 80 characters from a erroneous pool of xxxx characters
... what remains is (xxxx - 80) erroneous characters. I suppose those
erroneous characters are a *HUGE* security risk and they are _awful_
too. Perhaps we should destroy all computers ... the risk is just
great for mankind.

Uh, are you seriously not familiar with the concept of a buffer
overflow? You allocate an array of 80 characters, then pass that array
(as a pointer) to a function which will read in as many characters as
the user cares to type. The 81st character clobbers some random chunk
of memory, and you get undefined behavior; in many cases, this is an
easy security exploit.
I took the effort to read several of your replies. Your knowledge of
C++ is light years beyond mine as easily observed; however, you seem
to have made yourself a self appointed cop in the group?

Warning people not to heed dangerous and incorrect advice is a good
thing. Why is this personal for you?
Please impress me with "how" to do things better and quit slamming
folks for existing outside your definition of intelligence.

Nobody slammed you. The dangerous and ill-founded recommendation you
voiced received a well-deserved thrashing, but that, again, is a good
thing.
Go ahead,
teach me! I can learn from you. And just maybe ... you can learn a
thing or two as well?

Enough melodrama. This is a technical newsgroup, please make sound
technical arguments. Nobody's convinced of anything by hissy fits.

Luke


Bravo !!

I stand corrected. Although I have never experienced (knowingly) the
buffer overflow problem (I guess I seldom fall asleep into the
keyboard) ... it is a possible problem. Here's some proof >>

#include <stdio.h>

void main(int argc, char* argv[])
{
char junk[6];
printf("enter 10 characters and hit return\n");
gets(junk);
printf("junk = %s\n");
}

-----------

Keyboard Input >> 1234567890

Output >>

junk = 1234567890

AND the following crash:

Run-Time Check Failure #2 - Stack around the variable 'junk' was
corrupted.

-----------

Very interesting how characters 67890 were read into memory not
assigned. Certainly overwriting memory that had not been allocated.

Here's a proposed solution that allows plenty of bogus key taps.

-----------------
#include <stdio.h>
#include <ctype.h>

void main(int argc, char* argv[])
{
char alpha, junk[81];
printf("enter erroneous garbage if you like ..."
" return key continues program.\n");
if(isalpha(alpha = getchar()))
{
while(isalpha(alpha = getchar()));
gets(junk);
}
printf("\nreturn key was entered.\n"
"main(%d): program now paused ...\n", __LINE__);
gets(junk);
printf("... program continues\n");
}

-----------------
Note the need for two "gets(junk);"
The first acts only if there are erroneous characters present.
This is a mess!! But it seems to work.

And now ...

How about a Guru's C++ style solution for pausing with/without
erroneous garbage in the input buffer?

-- Tom
 
R

red floyd

Alvin said:
Do you want the program to stop until you hit enter? If so, use cin like:

string str;
cin >> str;

The problem is if the operator types something like "hello world". Then
cin >> str will read "hello", but leave the "world" out there.

Better would be:

std::string str;
std::getline(cin,str);
 
J

Jacek Dziedzic

Alvin said:
Jacek Dziedzic wrote:




In that case, getc() or getchar() will do the trick, but that's isn't C++.
That's why I suggested the cin way instead. :)

Yes, I get it. I pause in the same way. I was only being
picky about the fact that it does not, in fact, wait for
the Enter key but for a string and then the Enter key.

cheers,
- J.
 
M

Marcin Kalicinski

And now ...
How about a Guru's C++ style solution for pausing with/without
erroneous garbage in the input buffer?

std::string junk;
std::cin >> junk;
 
D

Default User

Marcin said:
std::string junk;
std::cin >> junk;

So what happens when "hello world is entered"? Some other solutions
presented so far are much better.

I've been amazed at how many answers betray a fundamental lack of
knowledge about C++ (and C) I/O.



Brian
 
M

Marcus Kwok

red floyd said:
The problem is if the operator types something like "hello world". Then
cin >> str will read "hello", but leave the "world" out there.

Better would be:

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

I second this suggestion. Also, it seems not to require the user to
type anything other than the carriage return, plus it avoids the buffer
overrun issue.

[Slightly OT]
I have used this technique in several of my programs that are
command-line driven in Windows: if the user mistakenly double-clicks the
program to run it, I can display a usage message without the command
prompt box disappearing immediately, using only standard facilities.
[/OT]
 
N

Neo

I just want the programme to stop for a while.
Thanks.

gee.. he just wanted the program to synchonously stop for a while with or
without user interruption.

If ur using turbo c++ .. there is a function in #include<dos.h> called..
delay(int millisec);

Should be what ur looking for. I'm not sure if its in the c++ std.

Nice day
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top