Click to See Complete Forum and Search --> : Re: Press any key to continue...


Nickolaus
03-12-2000, 11:09 PM
There's a much easier way to do this:

#include <fcntl.h>

system( "pause" ); //does exactly what you want

-Nickolaus

"Mobi2" <radmer@mobilixnet.dk> wrote:
>
>Just use the getch() function....
>
>like this:
>
>#include<iostream.h>
>#include<conio.h>
>
>main()
>{
> char ch;
> for(;;)
> {
> cout<<"Press any key to continue...";
> getch();
> }
>return 0;
>}
>
>this loops forever!!!!!!!!! but i think you get the point!!
>
>Mobi2
>
>
>"John" <sra090@hotmail.com> wrote:
>>
>>I would like to have a function that'll cout<<"Press any key to continue...";
>>and then when a user presses any key (including Enter and Spacebar) it
proceeds
>>to the next function I write below. Is this possible? How can I do this?
>

John
03-13-2000, 03:33 AM
Ok gentlemen, thank you for your help. But I still can't get the result I
was hoping for. So here's very simple code I wrote. Please tell me what's
wrong with this.



#include <iostream.h>
#include<conio.h>

//function prototypes
void press_any_key();
void one();
void two();
void three();

void main()
{
press_any_key();
one();
press_any_key();
two();
press_any_key();
three();
}

void press_any_key()
{
char any_key;

cout << "\n";
cout << "Press any key to continue.";
any_key = getch();
cout << "\n\n";
}

void one() {cout << "One\n";}
void two() {cout << "Two\n";}
void three() {cout << "Three\n";}

Lavadome
03-13-2000, 03:44 PM
Your program basically works as is aside from the void main().. here's some
decent code, more C++:

#include <iostream>
using namespace std;

// function prototypes
void press_any_key();
void one();
void two();
void three();

int main()
{
press_any_key();
one();
press_any_key();
two();
press_any_key();
three();
return 0;
}

void press_any_key()
{
char ch;

cout << "\nPress any key to continue.";
cin.get(ch);
cout << "\n\n";
cin.clear();
}

void one() {cout << "One\n";}
void two() {cout << "Two\n";}
void three() {cout << "Three\n";}

Hope this helps
Charles Evans

John <sra090@hotmail.com> wrote in message news:38cc99d2$1@news.devx.com...
>
> Ok gentlemen, thank you for your help. But I still can't get the result I
> was hoping for. So here's very simple code I wrote. Please tell me
what's
> wrong with this.
>
>
>
> #include <iostream.h>
> #include<conio.h>
>
> file://function prototypes
> void press_any_key();
> void one();
> void two();
> void three();
>
> void main()
> {
> press_any_key();
> one();
> press_any_key();
> two();
> press_any_key();
> three();
> }
>
> void press_any_key()
> {
> char any_key;
>
> cout << "\n";
> cout << "Press any key to continue.";
> any_key = getch();
> cout << "\n\n";
> }
>
> void one() {cout << "One\n";}
> void two() {cout << "Two\n";}
> void three() {cout << "Three\n";}

John
03-14-2000, 06:20 AM
Thank you for your help. I was able to get the same result by doing this:


#include <iostream.h>

//function prototypes
void press_any_key();
void one();
void two();
void three();

void main()
{
press_any_key();
one();
press_any_key();
two();
press_any_key();
three();
}

void press_any_key()
{
char any_key;

cout << "\n";
cout << "Press any key to continue.";
cin.get(any_key);
cout << "\n\n";
}

void one() {cout << "One\n";}
void two() {cout << "Two\n";}
void three() {cout << "Three\n\n\n\n";}


but still, only the 'Enter' key will make it proceed. Any other keys such
as abcd... or spacebar won't make it proceed to the next step.
Any suggestions?




"Lavadome" <lavadome95@yahoo.com> wrote:
>Your program basically works as is aside from the void main().. here's some
>decent code, more C++:
>
>#include <iostream>
>using namespace std;
>
>// function prototypes
>void press_any_key();
>void one();
>void two();
>void three();
>
>int main()
>{
> press_any_key();
> one();
> press_any_key();
> two();
> press_any_key();
> three();
> return 0;
>}
>
>void press_any_key()
>{
> char ch;
>
> cout << "\nPress any key to continue.";
> cin.get(ch);
> cout << "\n\n";
> cin.clear();
>}
>
>void one() {cout << "One\n";}
>void two() {cout << "Two\n";}
>void three() {cout << "Three\n";}
>
>Hope this helps
>Charles Evans

duane
03-14-2000, 05:46 PM
<snip>
>but still, only the 'Enter' key will make it proceed. Any other keys such
>as abcd... or spacebar won't make it proceed to the next step.
>Any suggestions?


About the only function that I know is getch() found in conio.h, but conio.h
is not available in all platforms.

Someone already suggested
system("pause");

But I don't remember if this works in Unix/Linux. It should work in all
DOS apps though...

Maybe someone knows a more portable way...

John
03-14-2000, 06:40 PM
I've tried system("pause"); but kept getting compiler error. Do you think
maybe this has got something to do with my Windows 2000?




"duane" <duane@nitrex.com> wrote:
>
><snip>
>>but still, only the 'Enter' key will make it proceed. Any other keys such
>>as abcd... or spacebar won't make it proceed to the next step.
>>Any suggestions?
>
>
> About the only function that I know is getch() found in conio.h, but conio.h
>is not available in all platforms.
>
>Someone already suggested
>system("pause");
>
>But I don't remember if this works in Unix/Linux. It should work in all
>DOS apps though...
>
>Maybe someone knows a more portable way...

Nickolaus
03-15-2000, 12:14 AM
"John" <sra090@hotmail.com> wrote:
>
>I've tried system("pause"); but kept getting compiler error. Do you think
>maybe this has got something to do with my Windows 2000?
>


Did you use #include <fcntl.h> in the header?

John
03-15-2000, 03:23 AM
Yes, I did. Uhh can you say "Duh?"





"Nickolaus" <watts.77@osu.edu> wrote:
>
>
>
>"John" <sra090@hotmail.com> wrote:
>>
>>I've tried system("pause"); but kept getting compiler error. Do you think
>>maybe this has got something to do with my Windows 2000?
>>
>
>
>Did you use #include <fcntl.h> in the header?
>

James Curran
03-15-2000, 12:05 PM
"John" <sra090@hotmail.com> wrote in message
news:38ce125a$1@news.devx.com...
> Thank you for your help. I was able to get the same result by doing this:
> but still, only the 'Enter' key will make it proceed. Any other keys such
> as abcd... or spacebar won't make it proceed to the next step.
> Any suggestions?

Well, the cin.get() & the getch() are getting the first character you
type, but they are buffered, so they wait until you've typed in entire line
before preceding. For example:

char a = getch();
char b = getch();
char c = getch();
char nl = getch();

if run, the program will stop for input at the first getch(). If you
were to type "abc" and then Enter, it would not stop again, and a would =
'a', b == 'b', c=='c' and nl == '\n'.


--
Truth,
James Curran
http://www.NJTheater.com
http://www.NJTheater.com/JamesCurran

Sylvain Bujold
03-15-2000, 12:49 PM
"duane" <duane@nitrex.com> wrote:
>
><snip>
>>but still, only the 'Enter' key will make it proceed. Any other keys such
>>as abcd... or spacebar won't make it proceed to the next step.
>>Any suggestions?
>
>
> About the only function that I know is getch() found in conio.h, but conio.h
>is not available in all platforms.
>
>Someone already suggested
>system("pause");
>
>But I don't remember if this works in Unix/Linux. It should work in all
>DOS apps though...
>
>Maybe someone knows a more portable way...

Maybe you can try a
cin.ignore(256,'\n');
cin.get(); //Or getch();
if you're using C++.

I had problems with reading string or something like that and I had to empty
the buffer with cin.ignore and I think I used it for a thing like you want
to do but it's a long time ago so maybe this does not have any sense :) I
don't have C++ compiler so I can't try it..

Sylvain Bujold